npm command
Here’s a list of commonly used npm commands, including those related to configuration. These commands will help you manage your Node.js projects, packages, and npm configurations effectively.
1. Basic npm Commands
-
npm config ls -l -
npm init: Initialize a new Node.js project and create apackage.jsonfile.npm init- Use
npm init -yto automatically create apackage.jsonwith default values.
- Use
-
npm install(ornpm i): Install all dependencies listed inpackage.json.npm installnpm install <package-name>: Install a specific package locally.npm install -g <package-name>: Install a package globally.
-
npm uninstall <package-name>: Uninstall a package.npm uninstall <package-name>- Use
-gto uninstall a global package.
- Use
-
npm update: Update all outdated packages to their latest versions.npm update -
npm outdated: Check for outdated packages.npm outdated -
npm list(ornpm ls): List installed packages.npm list- Use
-gto list globally installed packages. - Use
--depth=0to list only top-level packages.
- Use
-
npm run <script>: Run a script defined in thescriptssection ofpackage.json.npm run build -
npm test: Run the test script defined inpackage.json.npm test -
npm start: Run the start script defined inpackage.json.npm start -
npm publish: Publish a package to the npm registry.npm publish -
npm unpublish: Remove a package from the npm registry.npm unpublish <package-name>
2. npm Configuration Commands
-
npm config set <key> <value>: Set a configuration value.npm config set prefix ~/.npm-global -
npm config get <key>: Get the value of a configuration setting.npm config get prefix -
npm config delete <key>: Delete a configuration setting.npm config delete prefix -
npm config list: List all npm configuration settings.npm config list -
npm config edit: Edit the.npmrcconfiguration file directly in a text editor.npm config edit
3. Package Management and Information
-
npm info <package-name>: Show information about a package.npm info react -
npm view <package-name>: View details about a package (similar tonpm info).npm view lodash -
npm search <terms>: Search for packages in the npm registry.npm search express -
npm audit: Perform a security audit of your project’s dependencies.npm audit -
npm audit fix: Automatically fix vulnerabilities found in the audit.npm audit fix -
npm cache clean --force: Clear the npm cache.npm cache clean --force
4. Global npm Commands
-
npm root -g: Display the directory where global packages are installed.npm root -g -
npm list -g --depth=0: List globally installed packages.npm list -g --depth=0 -
npm link: Symlink a package folder into the global package directory.npm link- Use
npm link <package-name>to link a global package as a local dependency.
- Use
-
npm unlink <package-name>: Remove a symlink created bynpm link.npm unlink <package-name>
5. Scripts and Automation
-
npm run <script>: Run a script defined in thescriptssection ofpackage.json.npm run build -
npm init <initializer>: Initialize a project using a specific initializer.npm init react-app my-app -
npx <command>: Run a command from a package that isn't installed globally.npx create-react-app my-app
6. Package Lock and Dependency Management
-
npm ci: Install dependencies frompackage-lock.json, ensuring a clean state.npm ci -
npm shrinkwrap: Lock down the versions of a project's dependencies.npm shrinkwrap -
npm dedupe: Reduce duplication in thenode_modulesdirectory.npm dedupe
7. Useful Options
-
npm --version(ornpm -v): Check the npm version.npm --version -
npm help <command>: Get help on a specific npm command.npm help install -
npm init -y: Automatically generate apackage.jsonfile with default values.npm init -y
Summary
These commands should cover most of your needs when working with npm and Node.js. Whether you're managing dependencies, configuring your environment, or automating tasks, this list provides a solid foundation for efficient project management.