npm start 是如何工作的,应该是 npm run start 吗?(创建反应应用程序)

Vno*_*mar 5 javascript npm create-react-app

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

Deh*_*oos 5

有一组默认内置的 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.
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

此 package.json 支持的 npm 执行是

  • npm 启动(内置)
  • 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""
  }
}
Run Code Online (Sandbox Code Playgroud)