Appearance
Node & NPM Cheat Sheet
Node.js CLI
| Command | Description | Examples/Options |
|---|---|---|
node [file] | Run a JavaScript file with Node.js. | node index.js |
node -v | Check Node.js version. | |
node -e "code" | Execute JavaScript code directly from the CLI. | node -e "console.log('Hello World!')" |
node --inspect [file] | Run Node.js in debugging mode. | |
node --inspect-brk [file] | Start Node.js with debugger and pause on the first line. | |
node --eval "code" | Evaluate a string as JavaScript code. | |
node --trace-warnings | Show full stack trace when warnings are thrown. | |
node --help | Get help information on Node.js CLI commands. |
NPM Package Management
| Command | Description | Examples/Options |
|---|---|---|
npm init | Create a package.json file interactively. | npm init -y (skip prompts and create default) |
npm install [pkg] | Install a package. | npm install express |
npm install [pkg] --save | Install a package and save it as a dependency. | |
npm install [pkg] --save-dev | Install a package and save it as a development dependency. | |
npm install | Install all packages listed in package.json. | |
npm uninstall [pkg] | Uninstall a package. | |
npm update [pkg] | Update a package to the latest version. | |
npm outdated | Check for outdated packages. | |
npm list | List all installed packages. | |
npm prune | Remove extraneous packages not listed in package.json. |
NPM Scripts
| Command | Description | Examples/Options |
|---|---|---|
npm run [script] | Run a script defined in package.json. | npm run build |
npm run [script] --silent | Run a script with no output. | |
npm test | Run tests defined in package.json (test script). | |
npm start | Run the start script defined in package.json. | |
npm stop | Run the stop script defined in package.json. | |
npm restart | Run the restart script defined in package.json. | |
npm run [script] -- --flag | Pass additional flags to npm scripts. | npm run build -- --prod |
Global vs Local Installation
| Command | Description | Examples/Options |
|---|---|---|
npm install -g [pkg] | Install a package globally (available system-wide). | npm install -g nodemon |
npm uninstall -g [pkg] | Uninstall a globally installed package. | |
npm install [pkg] | Install a package locally (in the current project directory). | npm install express |
npm uninstall [pkg] | Uninstall a locally installed package. | |
npm ls -g --depth=0 | List globally installed packages without showing dependencies. |
NPM Cache Management
| Command | Description | Examples/Options |
|---|---|---|
npm cache clean --force | Clean the NPM cache. | |
npm cache verify | Verify the contents of the cache and clean up unnecessary files. | |
npm cache add [pkg] | Add a package to the cache. | |
npm cache ls | List all cached packages. |
NPM Versioning and Publishing
| Command | Description | Examples/Options |
|---|---|---|
npm version [update_type] | Update the version of your package (patch, minor, major). | npm version patch |
npm publish | Publish your package to the NPM registry. | |
npm unpublish [pkg] | Unpublish a package (use caution, as it has limitations). | |
npm deprecate [pkg] | Deprecate a version of a package. |
NPM Audit and Security
| Command | Description | Examples/Options |
|---|---|---|
npm audit | Perform a security audit of your dependencies. | |
npm audit fix | Automatically fix security vulnerabilities where possible. | |
npm audit fix --force | Apply potentially breaking changes to fix vulnerabilities. | |
npm audit --json | Output audit results in JSON format. |
NPM Configuration
| Command | Description | Examples/Options |
|---|---|---|
npm config list | List NPM's current configuration settings. | |
npm config set [key]=[value] | Set a configuration value. | npm config set registry https://registry.npmjs.org/ |
npm config delete [key] | Delete a configuration value. | |
npm config get [key] | Get the value of a specific configuration. | npm config get registry |
npm set init.author.name | Set default values for package.json fields. |
Npx (NPM Package Runner)
| Command | Description | Examples/Options |
|---|---|---|
npx [package] | Run a package without installing it globally. | npx create-react-app my-app |
npx --no-install [package] | Run a package without installing and skip package lookup. | |
npx [package]@version | Run a specific version of a package. | npx create-react-app@4.0.3 my-app |
npx -p [package] [command] | Run a package and execute a command within it. | npx -p @angular/cli ng new my-app |
npx --ignore-existing | Run a command ignoring globally installed versions. | |
npx --help | Get help about npx commands. |