我的 react 应用程序的生产版本突然出现问题。它们在开发中工作正常,但是当我构建它们时,我收到一个控制台错误:
/static/css/main.f6418f8a.chunk.css:1 无法加载资源:
在网::ERR_FILE_NOT_FOUND
1.13eeb203.chunk.js:1 无法加载资源:net::ERR_FILE_NOT_FOUND
main.1e6014ca.chunk.js:1 无法加载资源: 网::ERR_FILE_NOT_FOUND
/favicon.ico:1 无法加载资源:net::ERR_FILE_NOT_FOUND
以下命令之后收到此错误:
create-react-app whatiswrong
npm run build
然后在我的浏览器中打开索引.html就像我为我构建的任何其他 react 应用程序所做的那样。
将"homepage": "./"
添加到package.json中。
从:https://github.com/facebook/create-react-app/issues/6275#issuecomment-457813277
- 打开你的 package.json 文件。
- 将此条目添加到其中
"homepage":"pathtoyourprojectbuild"
- 重新运行命令
npm run build
。
这里的问题是,当您运行 npm run build 命令时,您的 package.json 中没有添加主页条目,它是假设项目目录(您运行命令的位置(托管在服务器根目录下构建的。
屏幕截图命令行
D:git_repomy-app>npm run build
> my-app@0.1.0 build D:git_repomy-app
> react-scripts build
Creating an optimized production build...
Compiled successfully.
File sizes after gzip:
44.57 KB buildstaticjsmain.8a29685f.js
**The project was built assuming it is hosted at root directory.
You can control this with the homepage field in your package.json.**
The build folder is ready to be deployed.
D:git_repomy-app>
你是如何运行你的应用的?您只是在浏览器中打开索引.html还是使用serve -s build
等?
如果您通过第一个选项执行此操作,那么您应该考虑尝试serve -s build