NPM Commands Cheatsheet

NPM Commands Cheatsheet

·

5 min read

No intro needed here, I'll get straight to the point.

Package Management

  • npm init: Initializes a new npm project in the current directory and creates a package.json file.

  • npm install <package>: Installs a package and adds it to the dependencies section of package.json.

    • --save-dev: Installs a package and adds it to the devDependencies section of package.json.

    • --save-optional: Installs a package and adds it to the optionalDependencies section of package.json.

    • --only=dev: Installs only the packages listed in devDependencies.

    • --only=production: Installs only the packages listed in dependencies.

    • --no-save: Installs a package without adding it to dependencies, devDependencies, or optionalDependencies in package.json.

  • npm update <package>: Updates a package to the latest version.

  • npm update: Updates all packages to their latest version.

  • npm uninstall <package>: Uninstalls a package and removes it from dependencies, devDependencies, or optionalDependencies in package.json.

  • npm prune: Removes packages that are no longer listed in dependencies, devDependencies, or optionalDependencies in package.json.

  • npm pack: Creates a tarball of the package, including all files in the package.

  • npm version <update_type>: Updates the version number in the package.json file and creates a git tag. The update_type can be "patch", "minor", or "major" depending on the type of update.

  • npm shrinkwrap: Locks down the versions of a package's dependencies so that you can control exactly which versions of each dependency will be used when your package is installed.

  • npm shrinkwrap --dev: Also locks down the versions of a package's devDependencies

  • npm shrinkwrap --prod: Only locks down the versions of a package's dependencies, not devDependencies

  • npm shrinkwrap --legacy-bundling: Generates a shrinkwrap file that includes bundledDependencies, for npm versions < 5.

  • npm dedupe: Attempts to find and resolve duplicate packages in the current project.

  • npm cache clean: Clears the npm cache.

  • npm publish: Publishes the package to the npm registry so that it can be installed by others.

  • npm link: Symlinks a package folder. This is useful when developing a package locally and testing it in another project.

  • npm unlink <package>: remove the symlink for a local package

Script Management

  • npm run <script>: Runs a script defined in the scripts section of package.json.

Package Information

  • npm list: Lists all installed packages.

  • npm list --depth=0: Lists only top-level packages.

  • npm list --global: Lists all globally installed packages.

  • npm search <query>: Searches for packages containing query in their name or description.

  • npm outdated: Lists packages that have updates available.

  • npm info <package> : Show detailed information about the package.

  • npm explore <package>: opens the package folder in the default file explorer.

  • npm fund: Lists the funding information of the dependencies of the current project.

  • npm audit: Check the current project's dependencies for security issues.

  • npm audit fix: Attempts to fix any found security issues in the current project's dependencies.

  • npm outdated --global: show the outdated global packages.

  • npm update -g: update all globally installed packages.

  • npm ls: list the packages and their dependencies installed in your project.

  • npm ls -g --depth=0: list the globally installed packages

Registry Management

  • npm login: Logs in to the npm registry.

  • npm logout: Logs out of the npm registry.

  • npm whoami: Shows the currently logged-in user.

  • npm adduser: Creates a new user or logs in with an existing user.

  • npm owner add <username> <package>: Adds an owner to a package on the npm registry.

  • npm owner ls <package>: Lists the owners of a package on the npm registry.

  • npm owner rm <username> <package>: Removes an owner from a package on the npm registry.

  • npm access <public|restricted> <package> : Change the access level of a package to public or restricted

  • npm access ls-packages : Lists all the packages you have access to

  • npm access ls-collaborators <package> : lists the collaborators of a package

  • npm access grant <read-only|read-write> <scope:package> <username> : Grant read-only or read-write access to a package to a user.

  • npm access revoke <scope:package> <username>: Revoke a user's access to a package.

Configuration

  • npm config list: Lists all the configuration settings.

  • npm config set <key> <value> : set a configuration value

  • npm config delete <key> : delete a configuration value

  • npm config get <key> : get the value of a specific configuration key

  • npm config edit : opens the npm configuration file in the default editor

  • npm prefix: Display the path of the current global node_modules folder

  • npm root: Display the path of the current package's folder

  • npm root -g: Display the path of the global node_modules folder

  • npm profile get <key> : get the value of a specific profile key

  • npm profile set <key> <value> : set a profile value

  • npm profile delete <key> : delete a profile value

  • npm profile list : list all the profile values

Help

  • npm help: Shows a list of all the npm commands.

  • npm help <command>: Shows detailed help for a specific command.

  • npm help-search <keyword>: Search the npm help documentation for the keyword

  • npm repo <package>: Open the package homepage in the default browser

  • npm bugs <package>: Open the package's issue tracker in the default browser

  • npm docs <package>: Open the package documentation in the default browser

  • npm test <package>: Run the test command defined in the package's package.json

  • npm stop <package>: Run the stop command defined in the package's package.json

  • npm start <package>: Run the start command defined in the package's package.json

  • npm restart <package>: Run the restart command defined in the package's package.json

  • npm run-script <scriptname> : run the script defined in the package.json file

While this is a pretty comprehensive cheatsheet, these are not ALL the commands and options available, also new commands and options may be added in future updates. Hope this cheatsheet may be helpful to you. Happy coding!