Here's an extensive and detailed list of Node.js CLI commands:
-
Check Node.js version:
node -v
-
Check npm version:
npm -v
-
Update Node.js (using nvm - Node Version Manager):
nvm install node # Install latest version nvm install <version> # Install specific version nvm use <version> # Switch to specific version
-
List installed Node.js versions (using nvm):
nvm ls
-
List available Node.js versions (using nvm):
nvm ls-remote
-
Run a Node.js script:
node script.js
-
Run a script with additional arguments:
node script.js arg1 arg2
-
Run a script with environment variables:
NODE_ENV=production node script.js
-
Run Node.js in REPL (Read-Eval-Print Loop) mode:
node
-
Run Node.js with debugging:
node --inspect script.js
-
Initialize a new Node.js project:
npm init
-
Initialize a new Node.js project with default settings:
npm init -y
-
Install a package locally:
npm install package-name
-
Install a package globally:
npm install -g package-name
-
Uninstall a package:
npm uninstall package-name
-
List installed packages:
npm list
-
Update all packages to their latest versions:
npm update
-
Check for outdated packages:
npm outdated
-
Check for security vulnerabilities in installed packages:
npm audit
-
Audit and fix vulnerabilities:
npm audit fix
-
Generate a package-lock.json file:
npm install
-
Run scripts defined in package.json:
npm run script-name
-
View details of a specific package:
npm view package-name
-
Run a package without installing it:
npx package-name
-
Run a specific version of a package:
npx package-name@version
-
Run a command from a package:
npx package-name command
-
Run Node.js with V8 Inspector:
node --inspect-brk script.js
-
Run Node.js with Trace:
node --trace-warnings script.js
-
Run Node.js with Profiling:
node --prof script.js
-
Generate a heap snapshot:
node --inspect script.js
-
Start debugging with the
inspect
flag:node --inspect script.js
-
Start debugging with the
inspect-brk
flag (breaks before user code runs):node --inspect-brk script.js
-
Debug in Chrome DevTools: Open
chrome://inspect
in Chrome and click "Open dedicated DevTools for Node". -
Debug with Visual Studio Code: Configure
.vscode/launch.json
and use the "Run and Debug" pane.
-
Measure performance with the built-in profiler:
node --prof script.js
-
Analyze the generated V8 log:
node --prof-process isolate-0xnnnnnnnnnnnn-v8.log
-
Create a new package template:
npm init
-
Link a local package for development:
npm link
-
Publish a package to npm registry:
npm publish
-
View package publish history:
npm view package-name
-
Access package information from the npm registry:
npm info package-name
-
Run tests defined in package.json (e.g., with Mocha):
npm test
-
Run tests with a specific framework (e.g., Jest):
npx jest
-
Run tests with a specific environment variable:
TEST_ENV=production npx mocha
-
Run ESLint for linting JavaScript code:
npx eslint filename.js
-
Fix linting issues automatically:
npx eslint filename.js --fix
-
Check code formatting with Prettier:
npx prettier --check filename.js
-
Format code with Prettier:
npx prettier --write filename.js
-
Set environment variables temporarily:
ENV_VAR=value node script.js
-
Use
.env
files withdotenv
package:npm install dotenv
In
script.js
:require('dotenv').config(); console.log(process.env.ENV_VAR);
This comprehensive list includes fundamental and advanced Node.js CLI commands, package management, debugging, performance monitoring, testing, and code quality tools. It covers common practices for managing Node.js environments, developing and testing applications, and ensuring code quality.