npm/Yarn - Tips


(how to) install package globally

npm

$ npm install -g semistandard
/usr/local/bin/semistandard -> /usr/local/lib/node_modules/semistandard/bin/cmd.js
$ which semistandard
/usr/local/bin/semistandard

Yarn

$ yarn global add semistandard
$ which semistandard
/usr/local/bin/semistandard

(how to) reinstall all packages

npm

$ rm -rf node_modules && npm install

Yarn

$ rm -rf node_modules && yarn

(how to) remove not used packages

npm

not used packages appear after removing packages from package.json manually.

$ npm prune

Yarn

yarn install command removes not used packages as well:

https://yarnpkg.com/lang/en/docs/cli/prune/

The prune command isn’t necessary. yarn install will prune extraneous packages.

(how to) run script or locally installed executable

npm

$ npm run-script <bin_or_script>

Yarn

$ yarn run <bin_or_script>

(how to) reset all (and clean cache)

  1. https://gist.github.com/jarretmoses/c2e4786fd342b3444f3bc6beff32098d

message from npm start:

  1. Clear watchman watches: watchman watch-del-all.
  2. Delete the node_modules folder: rm -rf node_modules && npm install.
  3. Reset Metro Bundler cache: rm -rf $TMPDIR/react-* or npm start -- --reset-cache.
  4. Remove haste cache: rm -rf $TMPDIR/haste-map-react-native-packager-*.

it might be necessary to clean cache, say, when build fails. in many cases it’s enough just to reset cache => try npm start --reset-cache command first.

npm

# _~/.zshenv_

alias npm_reset='\
  watchman watch-del-all &&
  rm -rf "$TMPDIR/react-native-packager-cache-*" &&
  rm -rf "$TMPDIR/metro-bundler-cache-*" &&
  rm -rf "$TMPDIR/haste-map-react-native-packager-*" &&
  rm -rf node_modules &&
  npm cache clean --force &&
  npm install
  '
$ npm_reset
$ react-native start --reset-cache

Yarn

# _~/.zshenv_

alias yarn_reset='\
  watchman watch-del-all &&
  rm -rf "$TMPDIR/react-native-packager-cache-*" &&
  rm -rf "$TMPDIR/metro-bundler-cache-*" &&
  rm -rf "$TMPDIR/haste-map-react-native-packager-*" &&
  rm -rf node_modules &&
  yarn cache clean &&
  yarn install
  '
$ yarn_reset
$ react-native start --reset-cache

(how to) update a package

npm

update to the latest version allowed by semantic version in package.json:

$ npm update foo

install specific version:

$ npm install --save foo@1.2.3

Yarn

update to the latest version allowed by semantic version in package.json:

$ yarn upgrade foo

install specific version:

$ yarn install foo@1.2.3