【问题标题】:How npm start works, it should be npm run start? (Create React App)npm start 是如何工作的,应该是 npm run start 吧? (创建反应应用程序)
【发布时间】:2019-11-26 05:22:43
【问题描述】:

在 Create React App 中,我们以 npm start 启动我们的应用程序,但对于构​​建,我们使用 npm run build 它应该是 npm run startnpm start 是如何工作的。它是任何默认的 npm 脚本命令吗?

【问题讨论】:

  • npm run start 非常常见,以至于 npm 实现了一个快捷方式来避免你输入 run

标签: javascript npm create-react-app


【解决方案1】:

有一组默认内置的 npm 脚本可以在没有“run”关键字的情况下执行。这些是

install, preinstall, preuninstall, postuninstall
prepublish, prepare, prepublishOnly, prepack, postpack, 
publish,preversion, version, postversion, 

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.

有些甚至在给定命令后自动运行(安装后 - 在“npm install”之后)。要完全理解这些脚本,请参考文档here

除此之外,您还可以定义可以运行的自定义脚本

  • 终端支持的任何命令
  • npm 支持的任何命令。

这些用户定义的自定义脚本应该使用“npm run ...”来执行。

需要在这些脚本上运行的指令在 package.json 文件的脚本部分下定义。在下面显示的 package.json 中,“start”和“test”是内置的、npm 可识别的命令。 “build”、“myinit”、“deletefolder”、“hellovnoitkumar”是自定义脚本。

这个 package.json 支持的 npm 执行是

  • npm start(内置)
  • npm 测试(内置)
  • npm run build(自定义)
  • npm run myinit(自定义)
  • npm run deletefolder(自定义)
  • npm run hellovnoitkumar(自定义)

示例 package.json

//npm start, npm test
//npm run build, npm run myinit, npm run deletefolder, npm run hellovnoitkumar
//*Note that you also can define what each built in npm command does (npm start, npm test).*
{
  "name": "my-webapp",
  "version": "0.1.0",
  "private": true,
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-scripts": "^2.1.5",
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "myinit" : "npm install && npm run build && npm start",
    "deletefolder": "rm -rf documents",
    "hellovnoitkumar": "echo "hello vnoit kumar""
  }
}

【讨论】:

    【解决方案2】:

    npm 有许多内置命令,您可以在没有“运行”字样的情况下运行,例如启动、测试、发布等。另一方面,用户定义的脚本需要与“运行”字词一起使用。您也可以使用带有“运行”的内置脚本,这将是相当的。

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 2020-10-26
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      相关资源
      最近更新 更多