`npm start` 和 `npm run start` 之间有什么区别吗?

241

我已经检查了 npm startnpm run start 这两个命令,它们都可以完美地运行。我使用的是 create-react-app 来创建应用程序。但是,为了对 CSS 模块进行配置更改,我运行了 npm eject,但它抛出了一个错误。

但是 npm run eject 就可以工作吗?我很困惑为什么 npm eject 不能正常工作。我能够做出配置更改吗?

下面是我的 package.json:

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
4个回答

439

npm testnpm startnpm restartnpm stop 都是 npm run xxx 的别名。

对于您定义的其他所有脚本(scripts),您需要使用 npm run xxx 语法。

有关更多信息,请参见https://docs.npmjs.com/cli/run-script上的文档。


33
所谓“别名”,就是指 npm testnpm run test 的作用完全相同,npm startnpm run start 的作用也是完全相同,其他命令同理。 - AKX
7
为什么"start"是别名,而"build"不是? - user1912383
10
你需要向npm的作者询问。我猜npm start被频繁使用,因此需要一个别名。 - AKX
1
实际上,在运行start脚本之前和之后,它还会运行prestartpoststart脚本。https://docs.npmjs.com/misc/scripts - hashlash
5
npm run *同样适用。npm run foo会运行prefoofoopostfoo - AKX
显示剩余5条评论

33

npm startnpm run start 的简写形式。所以,它们是同一个意思。


16

需要注意的一个有趣的事情是,
如果在package.json文件中,scripts对象没有"start"属性,从命令行运行npm startnpm run start默认会执行node server.js

但如果package.json中的scripts对象有"start"属性,则它将覆盖node server.js并执行"start"属性中的命令。

详见 - https://docs.npmjs.com/cli/v7/commands/npm-start#description


3

我分享这个信息是因为有时候可以帮助到别人。 在Jenkins流水线中,你应该使用带有"run"的npm命令。 例如:npm run start 如果不加"run",那么命令将无法执行。


1
实际上,它是任何缺少像标准输出(真正的tty)这样的东西的最小环境,例如在最小或精简容器中。在这些情况下,许多别名实际上不存在;例如,在使用Jenkins、Tavis-CI或GitLab-CI时,在其运行程序和容器的各个版本中,即使使用自安装的Node,您也会发现npm test失败并显示未找到该命令的错误,因此您需要设置npm run test。这是一个很好的观点,所以我给你点了赞 - 显然这不是问题的答案。 - Rik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接