site logo

by Frank Lin in Quick Tips
2018-04-14 3 minutes to read 1 views 0 comments

While learning node.js, install packages is a commonly operation. However, as a newbie, I might install the packages not in the right way. So taking this note to refresh my mind on the npm install.

There are two ways to install npm packages: locally or globally.

  • If you want to depend on the package from your own module, using something like node.js’ require, then you want to install locally. This is the default behavior of npm install (alias: npm i).
  • If you want to use a package as a command line tool, such as gulp CLI, then install the package globally.

Got the ideas? Make it further…

npm itself and npm install globally

npm is installed automatically with node.js, but sometimes it’s get updated while node.js not releasing new versions. So be sure that you have the latest version with:

npm i npm@latest -g

-g indicates this installation is globally on your machine.

If you want to try the next, unreleased version of npm, use this command:

npm i npm@next -g

Install other packages globally, just in the same way with npm install -g <package>, eg:

npm i -g gulp-cli

npm install locally

Install packages locally will create the node_modules directory under the current directory, eg:

npm i babel-preset-env

To confirm that npm i worked correctly, check to see that a node_modules directory exists and that it contains a directory for the package you just installed.

If there’s no package.json file in the local directory, the latest version of the package is installed. If package.json file exists, it just install the version specified in the package.json file.

npm install saves any specified packages into dependencies by default. Additionally, you can control where and how they get saved with additional flags:

  • -P or --save-prod: package will appear in your dependencies. This is the npm install default.
  • -D or --save-dev: package will appear in your devDependencies.
  • -O or --save-optional: package will appear in your optionalDependencies.
  • --no-save: this prevents saving to dependencies.

All above options will save dependencies to the package.json file.

npm update

local packages

First run this command:

npm outdated

This will check the registry to see if any installed packages (in the same directory as the package.json) are currently outdated.

To update the packages, just run npm update. After this, run npm outdated should not have any results.

global packages

To find out which global packages need to be updated, type:

npm outdated -g --depth=0

Need to update the global package, type:

npm update -g <package>

To update all global packages, run:

npm update -g .

npm uninstall

To remove a package from your <node_modules> directory, use:

npm uninstall <package>

To remove a package from the dependencies in package.json, you’ll need to use the --save flag:

npm uninstall --save <package>

If you installed a package as a devDependency (i.e. with --save-dev) then npm uninstall --save won’t remove it from package.json, you have to use --save-dev to uninstall it.

To uninstall a global package, just in a similar way:

npm uninstall -g <package>

Well, check more options you need at, happy coding!

node.js npm