npm scripts
npm command
https://docs.npmjs.com/cli-documentation/
CLI commands
- > access Set access level on published packages
- > adduser Add a registry user account
- > audit Run a security audit
- > bin Display npm bin folder
- > bugs Bugs for a package in a web browser maybe
- > build Build a package
- > bundle REMOVED
- > cache Manipulates packages cache
- > ci Install a project with a clean slate
- > completion Tab Completion for npm
- > config Manage the npm configuration files
- > dedupe Reduce duplication
- > deprecate Deprecate a version of a package
- > dist-tag Modify package distribution tags
- > docs Docs for a package in a web browser maybe
- > doctor Check your environments
- > edit Edit an installed package
- > explore Browse an installed package
- > help-search Search npm help documentation
- > help Get help on npm
- > hook Manage registry hooks
- > init create a package.json file
- > install-ci-test Install a project with a clean slate and run tests
- > install-test Install package(s) and run tests
- > install Install a package
- > link Symlink a package folder
- > logout Log out of the registry
- > ls List installed packages
- > npm javascript package manager
- > org Manage orgs
- > outdated Check for outdated packages
- > owner Manage package owners
- > pack Create a tarball from a package
- > ping Ping npm registry
- > prefix Display prefix
- > profile Change settings on your registry profile
- > prune Remove extraneous packages
- > publish Publish a package
- > rebuild Rebuild a package
- > repo Open package repository page in the browser
- > restart Restart a package
- > root Display npm root
- > run-script Run arbitrary package scripts
- > search Search for packages
- > shrinkwrap Lock down dependency versions for publication
- > star Mark your favorite packages
- > stars View packages marked as favorites
- > start Start a package
- > stop Stop a package
- > team Manage organization teams and team memberships
- > test Test a package
- > token Manage your authentication tokens
- > uninstall Remove a package
- > unpublish Remove a package from the registry
- > update Update a package
- > version Bump a package version
- > view View registry info
- > whoami Display npm username
其中最常用的是install
npm install (with no args, in package dir) npm install [<@scope>/]<name> npm install [<@scope>/]<name>@<tag> npm install [<@scope>/]<name>@<version> npm install [<@scope>/]<name>@<version range> npm install <git-host>:<git-user>/<repo-name> npm install <git repo url> npm install <tarball file> npm install <tarball url> npm install <folder>
This command installs a package, and any packages that it depends on. If the package has a package-lock or shrinkwrap file, the installation of dependencies will be driven by that, with an
npm-shrinkwrap.json
taking precedence if both files exist. See package-lock.json and npm-shrinkwrap.A
package
is:
- a) a folder containing a program described by a
package.json
file- b) a gzipped tarball containing (a)
- c) a url that resolves to (b)
- d) a
<name>@<version>
that is published on the registry (seenpm-registry
) with (c)- e) a
<name>@<tag>
(seenpm-dist-tag
) that points to (d)- f) a
<name>
that has a “latest” tag satisfying (e)- g) a
<git remote url>
that resolves to (a)
从其中的描述可以看出, npm的包管理设计还是很精妙。
所有的程序都是包的形式存在, 如果包是库的形式, 提供出去被其他程序使用,可以发布(对应publish命令)到registry(npm仓库)上,或者使用pack命令打包为tar文件,放到网上制定URL上,或者GitHub上。
如果自己作为程序使用, 则为本地包的形式, 安装其依赖的包,并可以运行。
pack命令
npm pack [[<@scope>/]<pkg>...] [--dry-run]
DESCRIPTION
For anything that’s installable (that is, a package folder, tarball, tarball url, name@tag, name@version, name, or scoped name), this command will fetch it to the cache, and then copy the tarball to the current working directory as
<name>-<version>.tgz
, and then write the filenames out to stdout.If the same package is specified multiple times, then the file will be overwritten the second time.
If no arguments are supplied, then npm packs the current package folder.
npm scripts
https://docs.npmjs.com/misc/scripts
npm supports the “scripts” property of the package.json file, for the following scripts:
- prepublish: Run BEFORE the package is packed and published, as well as on local
npm install
without any arguments. (See below)- prepare: Run both BEFORE the package is packed and published, on local
npm install
without any arguments, and when installing git dependencies (See below). This is run AFTERprepublish
, but BEFOREprepublishOnly
.- prepublishOnly: Run BEFORE the package is prepared and packed, ONLY on
npm publish
. (See below.)- prepack: run BEFORE a tarball is packed (on
npm pack
,npm publish
, and when installing git dependencies)- postpack: Run AFTER the tarball has been generated and moved to its final destination.
- publish, postpublish: Run AFTER the package is published.
- preinstall: Run BEFORE the package is installed
- install, postinstall: Run AFTER the package is installed.
- preuninstall, uninstall: Run BEFORE the package is uninstalled.
- postuninstall: Run AFTER the package is uninstalled.
- preversion: Run BEFORE bumping the package version.
- version: Run AFTER bumping the package version, but BEFORE commit.
- postversion: Run AFTER bumping the package version, and AFTER commit.
- pretest, test, posttest: Run by the
npm test
command.- prestop, stop, poststop: Run by the
npm stop
command.- prestart, start, poststart: Run by the
npm start
command.- prerestart, restart, postrestart: Run by the
npm restart
command. Note:npm restart
will run the stop and start scripts if norestart
script is provided.- preshrinkwrap, shrinkwrap, postshrinkwrap: Run by the
npm shrinkwrap
command.
与install命令对应 install 的 script hook。
- preinstall: Run BEFORE the package is installed
- install, postinstall: Run AFTER the package is installed.
阮一峰教程
http://www.ruanyifeng.com/blog/2016/10/npm_scripts.html