Node, NPM common used commands

Below are some of the most commonly used commands when working with Node.js, including managing and publishing packages. This post may be constantly updated.

Initiating Project

CommandDescriptionExample
npm initCreate a package.json file under the current directory(will prompt to input several default information)
npm init

or

(no prompt, will set required information to its default value)
npm init --yes

Managing Packages

Before working on packages, make sure you are aware on how Semantic Versioning (semver) works. To check, you can use https://semver.npmjs.com/

~1.2.3 = tilde indicates that npm will install the most recent patch

^1.2.3 = caret symbol indicates that npm will install the most recent minor version to this package

CommandDescriptionExample
npm installInstall all dependencies defined under the package.json file

install command with <package name> will install the package in the local project, unless the flag -g is specified
npm install

or

(short hand)
npm i

or

npm i express

or

(to install specific version)
npm i express@4.17.0

(to install latest version)
npm i express@latest
npm outdateddisplay which package will be updated if npm update will execute. It will be updated to the version indicated under “Wanted” columnnpm outdated
npm updateUpdate dependenciesnpm update
npm show <package name> versionsList all available versions on the specified packagenpm show express versions
npm lsList down all package installed in local project and its version npm ls

Debugging

CommandDescriptionExample
node –inspect-brkRun the node.js script in debug mode
node --inspect-brk <script to use>

After executing the command, use Google Chrome browser then go to chrome://inspect/ This will open up the “inspect” tool to inspect the script you are using

Publishing Node.js Packages

Commands below requires an account on https://www.npmjs.com/

CommandDescriptionExample
npm loginLogin to npmjs.com to publish node.js packagesnpm login

Note: will prompt for npmjs.com user account
npm publishPublish the specified node.js packagenpm publish