Skip to content

Instantly share code, notes, and snippets.

@dcleao
Last active March 1, 2024 21:27
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save dcleao/c1bec5e288cd3c2c05be31c00446ca55 to your computer and use it in GitHub Desktop.
Save dcleao/c1bec5e288cd3c2c05be31c00446ca55 to your computer and use it in GitHub Desktop.
NPM Commands, Scripts, Life-cycle Phases

NPM Commands, Scripts, Life-cycle Phases

The following describes the behaviour of several npm commands, particularly w.r.t. the scripts that are run in each, for NPM version 6.5.0.

npm build <other-package-folder>

  1. npm run preinstall
  2. link binaries (node-gyp)
  3. for each bin command in other package:
    1. symlink ./node_modules/.bin/<bin-name> -> ./node_modules/<other-package>/<local-bin-path>
  4. if (config["rebuild-bundle"]) [=true]
    1. for each <bundledDependency> in bundledDependencies
      1. npm build <bundledDependencyFolder> (without rebuild bundles)
  5. npm run install
  6. npm run postinstall

npm install

  1. npm run preinstall
  2. for each <dependency> in dependencies, devDependencies, optionalDependencies
    1. npm install <dependency>
  3. npm build . (without scripts)
  4. npm run install
  5. npm run postinstall
  6. npm run prepublish (deprecated since npm 5)
  7. npm run prepare
  8. creates/updates package-lock.json or npm-shrinkwrap.json file

npm install <dependency>

  1. if <dependency> is another package's identifier
    1. fetch package into ./node_modules
  2. if <dependency> is another package's folder
    1. symlink ./node-modules/<other-package-name> -> <other-package-folder> (if is top-level?)
  3. <other-package>: npm run preinstall
  4. for each <dependency2> in <other-package>'s dependencies, devDependencies, optionalDependencies
    1. npm install <dependency2>
  5. <other-package>: npm run prepare, if <dependency> is of type <git-remote-url-package>
  6. <other-package>: npm build . (without scripts)
  7. <other-package>: npm run install
  8. <other-package>: npm run postinstall
  9. save package as a dependency in package.json, if top-level and [--save], --save-dev, ...

npm link (install + link own bins globally)

  1. npm install
  2. for each bin command in current package:
    1. symlink /usr/local/bin/<bin-name> -> /usr/local/lib/node_modules/<package-name>/<local-bin-path>
  3. symlink /usr/local/lib/node_modules/<package-name> -> .

npm link <other-package-folder>

  1. <other-package>: npm link
  2. symlink ./node-modules/<other-package-name> -> /usr/local/lib/node_modules/<other-package-name>

npm uninstall <other-package> (or npm unlink <other-package>)

  1. <other-package>: npm run preuninstall
  2. remove symlinks of dependency bins
  3. <other-package>: npm run uninstall
  4. <other-package>: npm run postuninstall
  5. remove package dependency in package.json, if [--save], --save-dev, ...

npm pack

  1. npm run prepublish (deprecated since npm 5)
  2. npm run prepare
  3. npm run prepack
  4. create the <package-name>-<package-version>.tgz file.
  5. npm run postpack

npm publish

  1. npm run prepublish (deprecated since npm 5)
  2. npm run prepare
  3. npm run prepublishOnly
  4. npm pack (without running prepublish and prepare scripts; already run)
  5. publish to NPM registry
  6. npm run postpublish

npm test

  1. npm run pretest
  2. npm run test
  3. npm run posttest

npm start

  1. npm run prestart
  2. npm run start
  3. npm run poststart

npm restart

  1. npm run prerestart
  2. npm run restart
  3. npm run postrestart

npm stop

  1. npm run prestop
  2. npm run stop
  3. npm run poststop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment