shfshanyue / Daily-Question

互联网大厂内推及大厂面经整理,并且每天一道面试题推送。每天五分钟,半年大厂中
https://q.shanyue.tech
4.93k stars 510 forks source link

【Q697】npm 执行命令传递参数时,为何需要双横线 #719

Open shfshanyue opened 3 years ago

shfshanyue commented 3 years ago

如在npm script 中有以下命令:

{
  "start": "serve"
}

其中 serve 可通过 --port 指定端口号:

$ npm start -- --port 8080

# 而在 yarn 时无需传递参数
$ yarn start --port 8080

那为什么 npm 执行命令传递参数时,为何需要双横线

iceycc commented 3 years ago

https://github.com/npm/npm/pull/5518 npm脚本执行时会开启一个shell,执行后面指定的脚本命令或文件, -- 是为了给后面shell脚本命令传递参数,类似node环境的process.argv的吧。