npm config

npm config ls -l

查看npm全部配置

npm config set registry https://registry.npm.taobao.org

设置镜像源

npm config set

设置npm配置,*unix系统下,配置文件是 ~/.npmrc

npm conifig set prefix dir-name

设置prefix,prefix在6.0以前是表示全局模块的安装目录,但是6.0以后,它的意思更接近它的“前缀”本义了,表示全局目录的前缀,但不是完整的全局目录。*unix系统下,默认目录是 /usr/local

npm prefix

npm prefix

打印包含package.json文件或node_modules目录的最接近的父目录。

  1. # 假设有一个标准的node项目 foo
  2. cd ~/project/foo
  3. npm prefix # 输出 /Users/apron/project/foo

npm prefix -g

打印prefix的值,它决定全局模块和全局bin文件的安装目录的前缀。意思就是作为它们的父目录存在。

npm root

npm root

打印有效node_modules目录。其实就是将在当前目录基础上增加一层node_modules。

➜  / pwd
/
➜  / npm root
/node_modules


➜  /usr pwd
/usr
➜  /usr npm root
/usr/node_modules
➜  /usr

npm root -g

打印全局目录安装的完整地址。
它的值的在 prefix基础上加山 /lib/node_modeuls。 eg: /usr/local/lib/node_modules。
因为该值依赖prefix,所以该值不能设置,只能设置prefix,才能改变其值。

npm bin

npm bin

打印NPM将安装可执行文件的文件夹。其实就是将在当前目录基础上增加一层node_modules。

➜  / pwd
/
➜  / npm root
/node_modules/.bin


➜  /usr pwd
/usr
➜  /usr npm root
/usr/node_modules/bin

npm bin -g

打印 全局bin文件的安装地址。
它的值的在 prefix基础上加山 /bin。 eg: /usr/local/bin。
因为该值依赖prefix,所以该值不能设置,只能设置prefix,才能改变其值。
当然除了node以外,其他的bin文件是个软连接文件,指向各自的全局模块地址。

➜  bin ls -l node npm npx fkill
lrwxr-xr-x  1 apron  admin        36  7 30  2020 fkill -> ../lib/node_modules/fkill-cli/cli.js
-rwxr-xr-x  1 root   admin  73883024  4  7 13:07 node
lrwxr-xr-x  1 root   wheel        38  4  7 13:07 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxr-xr-x  1 root   wheel        38  4  7 13:07 npx -> ../lib/node_modules/npm/bin/npx-cli.js

run-scripts

当clone仓库后,安装依赖时,执行 npm install (注意无任何参数),事件触发顺序是:
prepublish - prepare -

{
    "name": "run-scripts test",
  "scripts":{
     "prepublish": "prepublish",
     "prepare": "prepare",
     "prepublishOnly": "prepublishOnly",
     "prepack": "prepack",
     "postpack": "postpack",
     "publish": "publish",
     "postpublish":"postpublish",
     "preinstall": "prepublish",
     "install": "prepublish"
  }
}

官方文档

npx alias npm x / npm exec

npm 7.0以后,新增了 exec子命令。npx不再是一个独立的二进制文件,npx是调用exec来实现的,使用方式还和以前兼容。

两者基本作用都是从本地或远程的npm包中运行一个命令。

怎样理解 本地包/远程包/一个命令 这3个名词?
本地包:本地安装的包。包括执行命令时所在文件夹下 node_modules目录的包以及全局安装的包。当然这些包是具有bin配置的包,不然你调用个啥。
远程包:如果本地没有安装所需包,去registry上现在到npm的缓存,并且将包包含的bin命令加入到PATH当中。
一个命令:可以是系统可调用的命令也可以是本地包或者远程包里的命令。位置参数默认是所执行的命令

npx: (node package execute),从名字上看,两者意思就是一样的。

功能既然一样,还提供2个,那说明还是有细微差别的。差别就是解释参数时有差别。
这里涉及到一个叫位置参数(potionsal argument)的概念。位置参数就是传参的时候,顺序不能变。
在执行命令时,通常需要2部分,一部分叫参数,一部分叫选项(option)。参数一般指操作对象或者操作源,是一个普通字符串,选项一般用来控制行为表现,由”-“ “—“ 开头那种形式。
举个例子:列出当前目录下的文件列表。
1.png
解释这么多就是为了讲清楚npx和 npm exec的差别。结论就是:
npx的选项必须放在位置参数的前头,而npm exec则没有前后的限制。

拿官方例子说明:

$ npx foo@latest bar --package=@npmcli/foo

最终执行的命令是:

$ foo bar --package=@npmcli/foo

由于—package选项出现在位置参数之后,因此它被视为执行命令的参数。

再看看npm exec执行相同命令的结果:

$ npm exec foo@latest bar --package=@npmcli/foo

在这种情况下,npm会首先解析—package选项,解析@npmcli/foo包。然后,它将在该上下文中执行以下命令:

$ foo@latest bar

建议使用双连字符来显式地告诉npm停止解析命令行选项和开关。因此,下面的命令等价于上面的npx命令:

$ npm exec -- foo@latest bar --package=@npmcli/foo

npx envinfo

查看环境信息, 方便你提交issues时,提供环境信息。
1.png

npx envinfo —preset jest

npx默认执行命令

If no -c or —call option is provided, then the positional arguments are used to generate the command string. If no —package options are provided, then npm will attempt to determine the executable name from the package specifier provided as the first positional argument according to the following heuristic:

  • If the package has a single entry in its bin field in package.json, or if all entries are aliases of the same command, then that command will be used.
  • If the package has multiple bin entries, and one of them matches the unscoped portion of the name field, then that command will be used.
  • If this does not result in exactly one option (either because there are no bin entries, or none of them match the name of the package), then npm exec exits with an error.

npx
npm-exec

.npmrc

https://blog.csdn.net/weixin_38384296/article/details/116403848

参考

npm cli 官方文档