npm-commands.md

## [npm](https://www.npmjs.com/) v3.10 - :black_medium_small_square: #### Update npm itself ``` npm install -g npm # Downgrade to a specific version npm install -g npm@2 ``` #### Check npm version ``` npm --version ``` #### Install a package ``` # Local npm install package-name # Local (Shorthand) npm i package-name # Local + make an entry in package.json as dependency npm install package-name --save # Local + make an entry in package.json as dev dependency npm install --save-dev pkg # Install specific version of a package npm install package-name@1.2.3 # Global npm install --global package-name # Global (Shorthand) npm install -g package-name # Global (Extreme Shorthand) npm i -g package-name ``` #### Un-install a package ``` # Local npm uninstall package-name # Global npm uninstall package-name -g ``` ### Get package info ``` # Home page npm home package_name # Github repo npm repo package_name ``` #### Check for outdated packages in package.json ``` # Local npm outdated # Global npm outdated -g # Production only npm outdated --prod ``` #### List installed packages ``` # Local with tree npm ls # Local - only parent npm ls --depth=0 # Global - only parent npm ls -g --depth=0 # List production packages only npm ls --prod ``` #### Remove un-used packages from node_modules folder ``` npm prune # Remove all devDependencies from node_modules npm prune --production ``` #### Update all packages listed in package.json ``` npm update ``` #### Update a single package ``` npm update package_name ``` #### Remove duplicate packages from node_modules ``` npm dedupe ``` #### List packages in cache ``` npm cache ls ``` #### Clean npm cache ``` npm cache clean -f ``` #### :bulb: Bump version number in package.json and create a git [tag](https://git-scm.com/book/en/v2/Git-Basics-Tagging) automatically ``` npm version 1.2.3 ``` #### Lockdown package versions for production ``` npm shrinkwrap # Also include devDependencies npm shrinkwrap --dev ``` #### Run npm in production (will not download devDependencies) ``` npm install --only=production ``` #### Install a package from github ``` npm install git://github.com/user-name/package-name.git#v0.1.0 # OR npm install user/repo#v1.0.1 ``` #### Install a package from local cache ``` npm install --cache-min 999999 package-name ``` ### View package info from its package.json file ``` npm view package_name property_in_json ``` ### npm install -g without sudo ``` mkdir ~/.npm-global npm config set prefix '~/.npm-global' chown -r user_name '~/.npm-global' ``` ### Some npm global configs ``` npm config set save-prefix ~ npm config set save-exact true npm config set engine-strict true npm config set ignore-scripts npm config set init.author.name your_name npm config set init.author.email your_email ``` ### Enable Auto completion ``` npm completion >> ~/.bashrc ``` * Package.json extended [docs](https://docs.npmjs.com/files/package.json) * Package.json [cheatsheet](http://browsenpm.org/package.json) * [Validate](http://package-json-validator.com/) package.json * npm alternative - [yarn](https://github.com/yarnpkg/yarn)
Useful npm commands and tricks

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.