
Node.js is a backend Javascript runtime environment that is light and highly scalable. It is used to build real-time applications such as video streaming applications. To make development easier, Node js developers created a node package manager (npm). This package manager acts as the Node.js command line. It is used for installing modules and initializing projects.
In this tutorial, you will learn how to install npm on Windows and how to use it.
Follow the steps below to download and install the Node.js .msi file. The Node.js .msi file includes the node package manager. You don’t have to download them separately like before.
Go to the Nodej.s website and download the Long Term Support (LTS) version of Node.js. The LTS version has features that have abundant documentation and it is stable in terms of security and performance when compared to the Node.js current version.
Navigate to the Download folder in the file manager and click the .msi package to start the installation procedure.
Accept the terms in the License Agreement.
Add a different directory if you want but you can just leave the default location set by Node.js.
Select the Node.js features you want to install or remove by clicking on the drop-down list. You can leave everything on default if you don’t have any changes to make.
Check the box to install essential tools required by Node.js and npm.
Finish the installation process by clicking on the install button to install Node.js.
Use the npm -v command to check the version of the node package manager you just installed. You will get the version number if it has been successfully installed.
Use node -v command to check if Node.js has been installed successfully. This command will also show the version number if Node.js has been successfully installed.
The npm init command is used to create a Node.js project. The npm init command will create a package where the project files will be stored. All the modules you download will be stored in the package.
The npm init command will also create the package.json file, and prompt you to add the following project information when creating a project:
The information will be stored in the package.json file. The package.json file contains the important details and metadata of your project such as package versions.
Here is an example of a package.json file:
The package.json file is stored in your current prescribed directory but you can also move it to your desired destination.
If you want to skip the questions asked when creating a project, use this command:
The above command will initialize the project and skip all the required details required by the package.json file. You can set these configuration details later when you’re ready to add them. But these are important details that should never be forgotten to be added.
You can use the following commands to install additional information:
The node package manager allows you to set default config options for the npm init command.
Here are some of the commands you can use to set default config options:
The following command sets your default email address.
The following command sets your default author name.
The following command sets your project’s license.
Use the dependencies attribute to manually add dependencies to the package.json file by referencing the name and version of the dependency using any text editor such as Microsoft Visual studio:
Use the devDependencies attribute to manually add devDependencies name and version to the package.json file:
You can check and keep track of all installed using the npm list command. The npm list command will generate a list of all installed packages.
The command will output all installed packages:
The npm install command is used to install modules such as Express. To use this command just add the name of your module after the install keyword.
If you don’t want to install a specific module you can go ahead and install modules and project dependencies listed in the package.json file using the following command.
If you are installing a module that hasn’t been listed in the package.json file. You can use the following command to install and add the module to the package.json file as a project dependency.
You can also use the --save-dev flag which adds the module as a devDependencies. Development dependencies (devDependencies) are used for development purposes only, they are not required during runtime.
If you want all of your applications to use a specified module, install the module globally by using the--global flag so that all Node.js applications in your system can access the module:
Security vulnerabilities found in packages often cause service outages and data loss. Inspecting and auditing your Node.js package dependencies using the npm audit command could help you identify security vulnerabilities and fix them before they cause data loss.
The npm audit command is only supported in npm version 6.0.0 and later versions only.
The npm audit command sends details about the package’s dependencies and devDependencies for inspection to your default registry. A report will be sent back which contains results of your package dependencies, devDependencies, bundledDependencies, and optionalDependencies security state.
Follow the following steps to audit your package dependencies:
npm audit command and press enter to start the security auditing process.Here is a list of essential commands that you will use after you install Node.js and the node package manager.
0 Comments
Be the first to comment and share your perspective with the community.