npm install, concerns for me

npm install, concerns for me

make dependencies in the right place

Tutorials / Web Notes

2018.04.14

👣 #node #npm

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 behaviour 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>, for example:

npm i -g gulp-cli

npm install locally

Install packages locally will create the node_modules directory under the current directory, e.g.:

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 default setting of npm install.
  • -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 https://docs.npmjs.com/cli/install, happy coding!

THE END
Ads by Google

林宏

Frank Lin

Hey, there! This is Frank Lin (@flinhong), one of the 1.41 billion . This 'inDev. Journal' site holds the exploration of my quirky thoughts and random adventures through life. Hope you enjoy reading and perusing my posts.

YOU MAY ALSO LIKE

Setup an IKEv2 server with strongSwan

Tutorials

2020.01.09

Setup an IKEv2 server with strongSwan

IKEv2, or Internet Key Exchange v2, is a protocol that allows for direct IPSec tunnelling between networks. It is developed by Microsoft and Cisco (primarily) for mobile users, and introduced as an updated version of IKEv1 in 2005. The IKEv2 MOBIKE (Mobility and Multihoming) protocol allows the client to main secure connection despite network switches, such as when leaving a WiFi area for a mobile data area. IKEv2 works on most platforms, and natively supported on some platforms (OS X 10.11+, iOS 9.1+, and Windows 10) with no additional applications necessary.

Host Jekyll site on Google Firebase with free SSL

Tutorials

2016.07.02

Host Jekyll site on Google Firebase with free SSL

Firebase Hosting is a developer focused static web hosting provider that is super fast, secure and reliable. You can quickly and easily deploy your static websites to a global content-delivery network (CDN) with a single command.

TOC

Ads by Google