簡體   English   中英

npm start 是怎么運行的,應該是npm run start? (創建 React 應用程序)

[英]How npm start works, it should be npm run start? (Create React App)

在 Create React App 中,我們使用npm start啟動我們的應用程序,但對於構建,我們使用npm run build它應該是npm run startnpm start是如何工作的。 它是任何默認的 npm 腳本命令嗎?

有一組默認內置的 npm 腳本,無需“運行”關鍵字即可執行。這些是

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”之后) 要完全理解這些腳本,請參閱此處的文檔

除此之外,您還可以定義可以運行的自定義腳本

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

這些用戶定義的自定義腳本應該使用“npm run...”來執行。

需要在這些腳本上運行的指令在 package.json 文件的腳本部分下定義。 在下面顯示的 package.json 中,“start”和“test”是內置的、npm 可識別的命令。 “build”、“myinit”、“deletefolder”、“hellovnoitkumar”是自定義的自定義腳本。

此 package.json 支持的 npm 執行是

  • npm 啟動(內置)
  • npm 測試(內置)
  • npm 運行構建(自定義)
  • npm 運行 myinit(自定義)
  • npm run deletefolder(自定義)
  • npm 運行 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""
  }
}

npm 有許多內置命令,您可以在沒有“運行”字樣的情況下運行,例如啟動、測試、發布等。另一方面,用戶定義的腳本需要與“運行”字樣一起使用。 您也可以將內置腳本與“運行”一起使用,這將是相當平等的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM