我正在 VS Code 中开发一个 React 应用程序。我使用 create-react-app 进行设置。我可以毫无问题地运行该项目
npm start
。当我尝试使用 Vercel 发布项目时,出现错误:
已经尝试再次删除node_modules和
npm install
。
Error! Command "npm run build" exited with 1
Error! Check your logs at https://...
您可以看到以下日志:
13:55:05.231 Retrieving list of deployment files...
13:55:06.975 Downloading 32 deployment files...
13:55:09.142 Analyzing source code...
13:55:11.289 Installing build runtime...
13:55:15.911 Build runtime installed: 4622.485ms
13:55:20.151 Looking up build cache...
13:55:21.237 Build cache not found
13:55:22.615 Detected package.json
13:55:22.616 Installing dependencies...
13:55:58.492 > [email protected] postinstall /vercel/path0/node_modules/babel-runtime/node_modules/core-js
13:55:58.492 > node -e "try{require('./postinstall')}catch(e){}"
13:55:58.604 > [email protected] postinstall /vercel/path0/node_modules/core-js
13:55:58.604 > node -e "try{require('./postinstall')}catch(e){}"
13:55:58.683 > [email protected] postinstall /vercel/path0/node_modules/core-js-pure
13:55:58.683 > node -e "try{require('./postinstall')}catch(e){}"
13:55:58.801 > [email protected] postinstall /vercel/path0/node_modules/ejs
13:55:58.802 > node ./postinstall.js
13:56:01.182 npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/webpack-dev-server/node_modules/fsevents):
13:56:01.182 npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
13:56:01.191 npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/watchpack-chokidar2/node_modules/fsevents):
13:56:01.191 npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
13:56:01.202 npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
13:56:01.202 npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
13:56:01.210 added 1985 packages from 812 contributors in 37.99s
13:56:02.556 137 packages are looking for funding
13:56:02.556 run `npm fund` for details
13:56:02.831 Running "npm run build"
13:56:03.100 > [email protected] build /vercel/path0
13:56:03.100 > react-scripts build
13:56:04.846 Creating an optimized production build...
13:56:23.548 Treating warnings as errors because process.env.CI = true.
13:56:23.548 Most CI servers set it automatically.
13:56:23.548 Failed to compile.
13:56:23.549 src/components/Counter.js
13:56:23.549 Line 21:6: React Hook useEffect has a missing dependency: 'props.timeEnds'. Either include it or remove the dependency array react-hooks/exhaustive-deps
13:56:23.577 npm ERR! code ELIFECYCLE
13:56:23.578 npm ERR! errno 1
13:56:23.581 npm ERR! [email protected] build: `react-scripts build`
13:56:23.581 npm ERR! Exit status 1
13:56:23.583 npm ERR!
13:56:23.583 npm ERR! Failed at the [email protected] build script.
13:56:23.583 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
13:56:23.592 npm ERR! A complete log of this run can be found in:
13:56:23.592 npm ERR! /vercel/.npm/_logs/2021-03-28T10_56_23_582Z-debug.log
13:56:23.606 Error: Command "npm run build" exited with 1
检查您的代码是否没有任何
warnings
。如果有警告,请尝试修复它们并再次部署,或者通过将 environment variable
CI
设置为 false
来忽略它们。它看起来像这样:
CI = false
如果您收到此错误:
CI='' npm run build
。
这为我解决了错误。使用 Vercel CLI 将 CI 环境变量设置为 false
转到您的项目目录,然后打开终端并输入
$ vercel env add CI
Vercel CLI 24.0.0
? What’s the value of CLI? false
? Add CLI to which Environments (select multiple, press a)? Production, Preview, Development
✅ Added Environment Variable CLI to Project <Project-name> [776ms]
现在,转到 Vercel 应用 => 设置 => 环境变量 您将看到
CLI
键的值设置为 false
转到 vercel.com 中的项目设置。 前往将军。 在“构建和开发设置”中,覆盖构建命令并写入
npm 运行构建
这解决了我的错误。
您只需检查项目文件夹中的 package.json,检查 json 对象中的构建脚本即可。
'script':
{
"dev": "next dev",
==> "build": "next build",
"start": "next start",
"lint": "next lint"
}
我是虔诚的
对于部署,如果遇到错误,请按照此过程操作...
就我而言,这是因为我的主题文件(
theme.ts
)位于pages
目录中。
Vercel 没有将其标记为红色,但检查完整日志后,我看到了
Build optimization failed: found page without a React Component as default export in
pages/theme
See https://nextjs.org/docs/messages/page-without-valid-component for more info.
这让我意识到我的
theme.ts
文件不应该在它所在的地方。把它移到外面pages
就成功了。
您可能已经解决了此错误,但对于可能遇到此错误的新人来说,它仍保留在这里。
vercel 构建工具日志非常详细,通常会显示错误所在。在我的示例中,我遇到了 eslint 未显示的错误,但我在 vercel 构建的日志中发现了问题所在。
在您的示例中,您面临的错误在日志中声明:
Failed to compile.
13:56:23.549 src/components/Counter.js
13:56:23.549 Line 21:6: React Hook useEffect has a missing dependency: 'props.timeEnds'. Either include it or remove the dependency array react-hooks/exhaustive-deps
这可能是它没有构建您的应用程序的原因。您应该尝试修复它,然后重新部署。
set
中的CI
到false
的替代方案也可以工作,但它可能会在部署您的应用程序时出现一些错误。前往 vercel.com 中的项目设置。
env
检查是否有任何错误,如果没有错误则可以运行 它对我有用