Skip to content

Node & NPM Cheat Sheet

Node.js CLI

CommandDescriptionExamples/Options
node [file]Run a JavaScript file with Node.js.node index.js
node -vCheck 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-warningsShow full stack trace when warnings are thrown.
node --helpGet help information on Node.js CLI commands.

NPM Package Management

CommandDescriptionExamples/Options
npm initCreate 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] --saveInstall a package and save it as a dependency.
npm install [pkg] --save-devInstall a package and save it as a development dependency.
npm installInstall all packages listed in package.json.
npm uninstall [pkg]Uninstall a package.
npm update [pkg]Update a package to the latest version.
npm outdatedCheck for outdated packages.
npm listList all installed packages.
npm pruneRemove extraneous packages not listed in package.json.

NPM Scripts

CommandDescriptionExamples/Options
npm run [script]Run a script defined in package.json.npm run build
npm run [script] --silentRun a script with no output.
npm testRun tests defined in package.json (test script).
npm startRun the start script defined in package.json.
npm stopRun the stop script defined in package.json.
npm restartRun the restart script defined in package.json.
npm run [script] -- --flagPass additional flags to npm scripts.npm run build -- --prod

Global vs Local Installation

CommandDescriptionExamples/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=0List globally installed packages without showing dependencies.

NPM Cache Management

CommandDescriptionExamples/Options
npm cache clean --forceClean the NPM cache.
npm cache verifyVerify the contents of the cache and clean up unnecessary files.
npm cache add [pkg]Add a package to the cache.
npm cache lsList all cached packages.

NPM Versioning and Publishing

CommandDescriptionExamples/Options
npm version [update_type]Update the version of your package (patch, minor, major).npm version patch
npm publishPublish 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

CommandDescriptionExamples/Options
npm auditPerform a security audit of your dependencies.
npm audit fixAutomatically fix security vulnerabilities where possible.
npm audit fix --forceApply potentially breaking changes to fix vulnerabilities.
npm audit --jsonOutput audit results in JSON format.

NPM Configuration

CommandDescriptionExamples/Options
npm config listList 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.nameSet default values for package.json fields.

Npx (NPM Package Runner)

CommandDescriptionExamples/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]@versionRun 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-existingRun a command ignoring globally installed versions.
npx --helpGet help about npx commands.