DEV Community

Cover image for Basic NPM Commands
BiKodes
BiKodes

Posted on

Basic NPM Commands

After setting up n node.js development environment, you need to know some basic commands of node package manager npm. The followings are the most commonly used ones.

npm stands for node package manager.

Installing npm

npm comes with the node.js. You can install node.js from their official website https://nodejs.org/en/download/
After installing node, You can check the version of node and npm by running the following commands:

node -v
Enter fullscreen mode Exit fullscreen mode
npm -v
Enter fullscreen mode Exit fullscreen mode

Flags

-S is the same as --save, and -D is the same as --save-dev.

npm init
Enter fullscreen mode Exit fullscreen mode

Initializes the package.json file of a project.

Use the -y flag to skip the questions and to create a package.json using default values.

npm install
Enter fullscreen mode Exit fullscreen mode

Installs the dependencies listed in package.json.

Shorthand: npm i

npm install <package>
Enter fullscreen mode Exit fullscreen mode

Installs a package from the npm registry.

Shorthand: npm i <package>

npm install -g <package>
Enter fullscreen mode Exit fullscreen mode

Installs a package globally on your machine.

npm install <package-name@version-number>
Enter fullscreen mode Exit fullscreen mode

Install a specific package version rather than the most latest version.

npm uninstall <package>
Enter fullscreen mode Exit fullscreen mode

Like the command itself suggests, it uninstalls packages that are already installed.

Shorthand: npm uninstall

npm uninstall -g <package>
Enter fullscreen mode Exit fullscreen mode

Uninstalls a global package from your machine.

npm outdated
Enter fullscreen mode Exit fullscreen mode

List the outdated packages for upgrade

npm update <package>
Enter fullscreen mode Exit fullscreen mode

Updates the specified package to the latest version available. If package is not specified, it updates every package.

npm cache verify
Enter fullscreen mode Exit fullscreen mode

Check how many cache entries are available.

Shorthand: npm up <package>

npm cache clean —force
Enter fullscreen mode Exit fullscreen mode

Clears the npm cache

npm audit
Enter fullscreen mode Exit fullscreen mode

Performs a security check for all the available project packages.

npm update -g <package>
Enter fullscreen mode Exit fullscreen mode

Updates a global package to the latest version available.

npm list
Enter fullscreen mode Exit fullscreen mode

Lists all the installed packages and their versions, along with their dependencies in a tree structure.

npm list depth <number>
Enter fullscreen mode Exit fullscreen mode

To get packages of a certain depth.
The packages installed by you will be in the depth 0. Its dependencies will be in the depth 1 and further dependencies will be in the depth 2 and so on.

npm view <package> <version>
Enter fullscreen mode Exit fullscreen mode

Shows available details about the specified package. If the version is not set, it defaults to the latest version.

npm run <script>
Enter fullscreen mode Exit fullscreen mode

Executes the specified script, if found as a property of the script object in package.json.

npm help
Enter fullscreen mode Exit fullscreen mode

npm CLI has built in help command

npm help <term>Flags
Enter fullscreen mode Exit fullscreen mode

-S is the same as --save, and -D is the same as --save-dev.

Tries to show the appropriate documentation page for the term provided.

npm <command> -h
Enter fullscreen mode Exit fullscreen mode

To get help for a particular command.

npm help-search <command>
Enter fullscreen mode Exit fullscreen mode

To search npm documentation for help.

npm ci
Enter fullscreen mode Exit fullscreen mode

This installs packages from package-lock.json without updating the dependency tree.

npm rebuild
Enter fullscreen mode Exit fullscreen mode

Rebuild packages

npm version major
Enter fullscreen mode Exit fullscreen mode

Find a major version

npm version minor
Enter fullscreen mode Exit fullscreen mode

Find a minor version

npm version patch
Enter fullscreen mode Exit fullscreen mode

Find the patch version

Conclusion

This list is not exhaustive. I welcome any criticism,advice and contribution involving this work in the comments below.

Top comments (0)