Should I install npm package locally or globally?

If I am beginner with npm, then this question will definitely come to my mind. To get an answer to this, you should ask the following questions to yourself:

  • How will I use the installed npm package?
  • Will I use it as a dependency in my module or will I use it as a command line tool?

 

Install locally

 

If you are going to include the package (say “slick-carousel”) using statement like “require” in node.js and then use it in your module.

npm install <package_name>

 

Install globally

 

If you are going to use the package as a command line tool (say build tools like “grunt-cli”, “gulp-cli”)

> npm install -g <package>
> npm install --global <package>

Installing globally allows you to use the package from command line in any directory.

By default the npm packages are installed locally.

 

How to verify that npm package has been installed successfully?

 

After you run the npm install command, it will create a “node_modules” directory in your current directory if not already present.
Check that “node_modules” is present and that it contains a directory for the package(s) you installed.

 

Which version of the package will be installed?

 

If you have created a package.json file in your current local directory, then npm will install the latest version that satisfies the semantic version rule declared in package.json otherwise it will install the latest version of the package available.

“package.json” is a JSON format file that list all your project dependencies. You can find more details related to it at https://docs.npmjs.com/files/package.json

 

Install a specific version of package

 

npm install [<@scope>/]<name>@<version>

To install the version of the package that is referenced by the specified tag:

npm install [<@scope>/]<name>@<tag>

Here <scope> is optional.

e.g.: To upgrade to the latest version of npm, run:

npm install [email protected] -g

 

For package installation you can also use the alias: npm i

> npm i <package_name>

 

Hope this helps you.

Leave a Comment

Back to top