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
Command | Description | Example |
---|---|---|
npm init | Create 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
Command | Description | Example |
---|---|---|
npm install | Install 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 outdated | display which package will be updated if npm update will execute. It will be updated to the version indicated under “Wanted” column | npm outdated |
npm update | Update dependencies | npm update |
npm show <package name> versions | List all available versions on the specified package | npm show express versions |
npm ls | List down all package installed in local project and its version | npm ls |
Debugging
Command | Description | Example |
---|---|---|
node –inspect-brk | Run 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/
Command | Description | Example |
---|---|---|
npm login | Login to npmjs.com to publish node.js packages | npm login Note: will prompt for npmjs.com user account |
npm publish | Publish the specified node.js package | npm publish |