在Create React App中,我们从npm start
开始我们的应用程序,但是对于构建,我们使用npm run build
,它应该是npm run start
,但是npm start
的工作方式。是默认的npm脚本命令吗?
答案 0 :(得分:3)
有一组默认的内置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 run ...”执行。
在package.json文件的scripts部分下定义了需要在这些脚本上运行的指令。在下面显示的package.json中,“ start”和“ test”是内置的,npm可识别的命令。 “ build”,“ myinit”,“ deletefolder”,“ hellovnoitkumar”是自定义的自定义脚本。
此package.json支持的npm执行是
样本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""
}
}
答案 1 :(得分:1)
npm具有许多内置命令,您可以在不使用“运行”字的情况下运行它们,例如启动,测试,发布等。另一方面,用户定义的脚本需要与“运行”字一起使用。您还可以将内置脚本与“ run”一起使用,这将相当相等。