Skip to content

Instantly share code, notes, and snippets.

@zybroxz
Last active July 28, 2017 20:34
Show Gist options
  • Save zybroxz/31647eddc3d07ed74bdf3112eae50304 to your computer and use it in GitHub Desktop.
Save zybroxz/31647eddc3d07ed74bdf3112eae50304 to your computer and use it in GitHub Desktop.
NPM Cheatsheet
Update npm (needs running as administrator. ie sudo, or in windows run the shell as administrator)
npm i npm@latest -g
Install global package
npm i gulp -g
Install and save to dependencies in package.json
npm i lodash -S
Save to devDependencies
npm i karma -D
Install from Github
npm install https://github.com/jaredhanson/passport
Install from a gist
If a Gist has a package.json then it can be installed using
npm install gist:7d867cda127e64d38f28 --save
Remove a package
npm uninstall karma -S
Remove global package
npm uninstall gulp -g
List local packages
npm list
npm list --depth 1
npm list --depth 0
List global packages
npm list --g true
npm list --g true --depth 0
npm list --g true --depth 1
Pruning - If a package shows as extraneous this means that it is not included in your package.json. Prune will remove any packages not in package.json
npm prune
npm prune <package_name>
npm prune --production - this will remove all devDependencies
Versioning - semver
1.8.3 = Major.Minor.Patch
Patch is a bug fix or performance improvement tc
Minor is new feature / functionality
Major is a breaking change
installing versions of packages
npm i [email protected]
npm i [email protected] - Latest 1.8 version
npm i [email protected] as above
npm i [email protected] - latest version of 1
When installing and saving to the package.json the package will (for example) show "^1.1.0" this means that if a newer version becomes available and npm install is run against this package then the newest version will be added. To specify a specific version and enforce that in package.json use the --save-exact arg
npm i [email protected] -S --save-exact
“~1.5.1” in the package.json file will install the latest version of 1.5
Update packages/dependencies
npm update
npm update -g - update all global dependencies
npm update -g gulp - update specific global dependency
Update single dependency
npm update underscore
View the repository of a package
npm repo express
Running scripts - https://docs.npmjs.com/misc/scripts
Package.json has a script section which can contain a reference to a file
Scripts: {
test: "node test.js",
start: "node index.js",
uglify: "gulp compress"
}
npm test - this will run the tests in test.js
npm start - this will run the start script - index.js
nom run uglify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment