【问题标题】:Getting 'sh: react-scripts: command not found' error when trying to deploy MERN app to Now (zeit.co)尝试将 MERN 应用程序部署到 Now (zeit.co) 时出现“sh: react-scripts: command not found”错误
【发布时间】:2020-04-22 23:38:14
【问题描述】:

我正在尝试将 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"
    ]
  }
}

为了确保,我尝试在我的 client 文件夹上运行 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 应用经验的人可以帮助我,我将不胜感激。

【问题讨论】:

    标签: node.js reactjs mern react-scripts vercel


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 2018-05-30
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多