Stay Hungry,Stay Foolish!

npm scripts

npm command

https://docs.npmjs.com/cli-documentation/

其中最常用的是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 (see npm-registry) with (c)
  • e) a <name>@<tag> (see npm-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 AFTER prepublish, but BEFORE prepublishOnly.
  • 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 no restart 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

 

posted @ 2020-01-01 20:32  lightsong  阅读(265)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel