Skip to content

Instantly share code, notes, and snippets.

@jednano
Last active February 1, 2021 00:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jednano/024b78d9993f2c1f729e1ca364ad94ec to your computer and use it in GitHub Desktop.
Save jednano/024b78d9993f2c1f729e1ca364ad94ec to your computer and use it in GitHub Desktop.
How to npm

How to npm

Before you can use the npm command, you'll need to install Node.js.

Installation

You can install Node.js directly or to manage multiple versions, try Volta!

Local usage (recommended)

Before you can install packages in your current folder, you will need a package.json file. The easiest way to create one is to run npm init, which starts a wizard with a series of prompts in your CLI (command line). Once you're finished, a package.json will be created in the same directory.

Installing a package

If you want to install a package named foo, simply run npm install foo or npm i foo. If foo exists in the npm registry, it will install the package in ./node_modules/foo and update package.json with the version range that was installed:

    "foo": "^1.2.3",

Additionally, a package-lock.json file is created with the exact version that was installed. If someone were to join the project at a later time, they could simply run npm ci to install the same exact versions that are specified in the package-lock.json file. This ensures everyone is using the same version and you don't run into bugs that work fine on someone else's machine.

These versions follow Semantic Versioning.

Updating packages

TODO

  • npm ci installs only the versions in package-lock.json w/o updating package.json or package-lock.json
  • npm install or npm i installs whatever dependencies are listed in package.json and updates the package-lock.json file accordingly (if there is a newer version within the range specified)
  • npx foo will look for ./node_modules/.bin/foo and run it if it exists
  • if it doesn't, it will install the latest version on the fly and run it
  • npm install -g foo will install foo globally, regardless of what folder you're in
  • subsequently, running just foo from anywhere will run node_modules/.bin/foo in your global npm path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment