簡體   English   中英

npm 錯誤! 缺少腳本:“構建”

[英]npm ERR! Missing script: "build"

我正在嘗試部署一個 react 項目,但沒有使用 create-react-app 來啟動該項目,我使用了一個名為 createapp.dev 的站點,所以我不知道這是否是問題所在。 我包含了我的 package.json 並且錯誤的圖像如下“錯誤”

    "scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    "clean": "rm dist/bundle.js",
    "start": "react-scripts start",
    "build-prod": "parcel build src/index.html"
  },
  "dependencies": {
    "node-sass": "^7.0.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.3.0"
  },
  "devDependencies": {
    "@babel/core": "^7.18.5",
    "@babel/preset-env": "^7.18.2",
    "@babel/preset-react": "^7.17.12",
    "gh-pages": "^4.0.0",
    "parcel-bundler": "^1.12.5",
    "prettier": "^2.7.1"
  }
}

您的 package.json 中沒有名為“build”的腳本。 嘗試使用“build-prod”!

npm run build表示從您提供的列表中運行名為build的腳本。 您的列表沒有名為build的腳本,因此它失敗了。

您的腳本沒有指定build命令,這就是npm run build失敗的原因。

以下是您的package.json腳本中的觀察結果:

"scripts": {
    "predeploy": "npm run build", // will run the code before deploy
    "deploy": "gh-pages -d build", // gh-pages => github pages hence this is for github deployment
    "clean": "rm dist/bundle.js", // rm to remove the bundle.js
    "start": "react-scripts start", // to start the react-app
    "build-prod": "parcel build src/index.html" // to make build using parcel module bundler
  }

通常create-react-app使用Webpack作為它的模塊捆綁器,這里它使用Parcel 根據捆綁程序的不同,從頭到尾的代碼有不同的功能和語法以及方法。

正如您在package.json中定義的那樣,而不是npm run build try npm run build-prod這將使用src/index.html的 parcel 作為 root 創建構建。

通常npm run build將運行以下react-scripts build您可以嘗試將以下內容添加到scripts中,但我懷疑它是否會起作用,因為需要根據使用的模塊捆綁器創建構建。 您可以通過在腳本中添加以下內容來試一試。

"build": "react-scripts build",

如果它使用正確的模塊捆綁器規則進行正確構建,那么它將運行,否則它將繼續拋出錯誤。

暫無
暫無

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

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