Skip to content

Instantly share code, notes, and snippets.

@0bie
Last active September 21, 2022 17:54
Show Gist options
  • Save 0bie/e15d220a25b2f2e59d1dfd8b4d95bd5e to your computer and use it in GitHub Desktop.
Save 0bie/e15d220a25b2f2e59d1dfd8b4d95bd5e to your computer and use it in GitHub Desktop.
npm beginner notes

npm commands

  • Update npm: npm install npm --global || npm i npm -g
  • New package: npm init --yes || npm init -y
  • Scopes: npm init --scope=myusername npm install @myusername/mypackage require('@myusername/mypackage')
  • Add dependencies: npm install --save package-name || npm i -s package-name
  • Add devDependencies: npm install --save-dev package-name || npm i -D package-name
  • Skip devDependencies: npm install --production
  • Add bundled dependencies: npm install --save --save-bundle package-name
  • Update dependencies: npm outdated && npm update
  • Run scripts: npm start npm stop npm restart npm test
  • Publish: npm publish package-name
  • Publish private: npm publish --access=restricted package-name
  • Publish public: npm publish --access=public package-name
  • Unpublish: npm unpublish package-name || npm unpublish package-name@version
  • Deprecate: npm deprecate package-name || npm deprecate package-name@version
  • SemVer: 1.5.6 || Breaking.Feature.Fix || Major.Minor.Patch
  • Update package.json version: npm version major || npm version minor || npm version patch
  • Remove npm errors from error log: npm run [script] -s
  • Check the location of the npm command: which npm
  • Check which files will output in your package: npm pack --dry
  • Multiple packages simultaneously (symlink):
in package-a:
run npm link

in package-b (which requires package-a):
run npm link package-a
  • dist-tags (distribution tags): npm dist-tag add package-name@version

Lifecycle hooks: will run automatically in response to events

  • Publish: prepublish publish postpublish
  • Install: preinstall install postinstall
  • Uninstall: preuninstall uninstall postuninstall
  • Version: preversion version postversion
  • Test: pretest test posttest
  • Stop: prestop stop poststop
  • Start: prestart start poststart
  • Restart: prerestart restart postrestart

Node security platform (nsp): consults a database of known securtiy vulnerablities when you run nsp check.

  • To install: npm install nsp -g
  • It can be useful if hooked to one of those lifecycle hooks above to prevent vulnerabilities in your packages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment