Bootstrap FreeKB - Node.js - Install a Node.js package using the npm install command
Node.js - Install a Node.js package using the npm install command

Updated:   |  Node.js articles

The npm install (Node Package Manager) command can be used to install a Node.js package. In this example, the cors package is installed.

npm install cors

 

In this example, the latest version of the dotenv package is installed.

npm install dotenv@latest

 

Here is how you can install a specific version of a package.

npm install express@4.17.3

 

If you do not use the -g or --global flag the package will be installed in your present working directory. In your present working directory there will be a package.json and package-lock.json file that contains the modules you have installed and a node_modules directorythat contains many files and sub-directories for the packages you have installed.

~]$ ls -l
drwxrwsr-x. 130 john.doe john.doe 12288 Jul  3 22:53 node_modules
-rw-rw-r--.   1 john.doe john.doe 64 Jul  3 22:53 package.json
-rw-rw-r--.   1 john.doe john.doe 66553 Jul  3 22:53 package-lock.json

 

The -g or --global flag can be used for a global install (all users).

npm install --global node-fetch

 

Or, you can add the package you want to install to package.json.

{
  "dependencies": {
    "cors": "^2.8.5",
    "dotenv": "^16.0.0",
    "express": "^4.17.3",
    "node-fetch": "^3.2.1"
  }
}

 

The npm install command should then install or update the packages listed in package.json.

~]$ npm install

added 64 packages, and audited 65 packages in 3s

10 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 9.5.1 -> 9.6.4
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.6.4
npm notice Run npm install -g npm@9.6.4 to update!
npm notice

 

Notice that by default there is a message about "looking for funding". This can be suppressed with the --no-fund flag.

npm install --no-fund

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter db0b1d in the box below so that we can be sure you are a human.