繁体   English   中英

尝试将 MERN 应用程序部署到 Now (zeit.co) 时出现“sh: react-scripts: command not found”错误

[英]Getting 'sh: react-scripts: command not found' error when trying to deploy MERN app to Now (zeit.co)

我正在尝试将一个 MERN 应用程序部署到 Now(zeit.co),但没有成功。 当 Now 在部署期间尝试构建时,它会收到“sh: react-scripts: command not found”错误并记录以下内容:

Downloading 56 deployment files...
Installing build runtime...
Build runtime installed: 422.936ms
Looking up build cache...
Installing dependencies...
> nodemon@2.0.2 postinstall /zeit/3a1e3f7b/node_modules/nodemon
> node bin/postinstall || exit 0
Love nodemon? You can now support the project via the open collective:
 > https://opencollective.com/nodemon/donate
npm WARN debts-app-api@1.0.0 No description
npm WARN debts-app-api@1.0.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.2 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

added 188 packages from 92 contributors in 2.415s
Running "npm run build"
> debts-app-api@1.0.0 build /zeit/3a1e3f7b
> npm run build --prefix client
> client@0.1.0 build /zeit/3a1e3f7b/client
> react-scripts build
sh: react-scripts: command not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! client@0.1.0 build: `react-scripts build`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the client@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! A complete log of this run can be found in:
npm ERR!     /zeit/.npm/_logs/2020-01-04T20_32_03_150Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! debts-app-api@1.0.0 build: `npm run build --prefix client`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the debts-app-api@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /zeit/.npm/_logs/2020-01-04T20_32_03_163Z-debug.log
Error: Exited with 1
    at ChildProcess.child.on (/zeit/69d347bcc38c1970/.build-utils/.builder/node_modules/@now/static-build/dist/index.js:32747:24)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at maybeClose (internal/child_process.js:925:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
worker exited with code 20 and signal null
done

我的应用程序的架构是:

应用架构

我用npm创建了app,使用npx create-react-app client --use-npm来创建React客户端,所以我不认为是yarn和npm的冲突。

我的服务器package.json是:

{
  (...)
  "scripts": {
    "start": "node index.js",
    "server": "nodemon index.js",
    "client": "npm run start --prefix client",
    "dev": "concurrently \"npm run server\" \"npm run client\"",
    "build": "npm run build --prefix client"
  },
  (...)
  }
}

我的client/package.json是 create-react-app 的默认值:

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.4.0",
    "@testing-library/user-event": "^7.2.1",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-scripts": "3.3.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

只是为了确保,我尝试在我的客户端文件夹上运行npm install ,但它没有做太多。 我检查了我的 package-lock.json,可以肯定的是,react-scripts 在那里,所以我希望当 Now 尝试从 github repo 构建时,它也应该显示出来,对吗? 我还查看了我的client/.gitignore ,但它只是 create-react-app 的默认文件:

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

我的服务器的.gitignore文件也没有任何可能导致问题的内容,它只是忽略了 node_modules 和 .env。

如果有更多部署 MERN 应用程序经验的人可以帮助我,我将不胜感激。

如果有人遇到同样的麻烦,我有一个洞察力。 一个简单的解决方案是将服务器的package.json构建脚本更改为:

"build": "npm install --prefix client && npm run build --prefix client"

导致问题的原因是 Now(zeit.co) 正在为服务器构建依赖项,它位于 app 文件夹的根目录,但没有为客户端文件夹构建依赖项。

它将运行脚本"build": "npm run build --prefix client" ,在root/package.json上找到。 这会将它重定向到运行root/client/package.json脚本,即它会尝试运行脚本"build": "react-scripts build"并崩溃,因为它找不到 react-scripts。

进行上述更改,它现在会在尝试运行"build": "react-scripts build"之前安装客户端依赖项"build": "react-scripts build"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM