我的React应用的生产版本突然出现问题。它们在开发中工作正常,但是当我构建它们时,出现控制台错误:
/static/css/main.f6418f8a.chunk.css:1无法加载资源:
net :: 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
然后像在其他浏览器中一样,在浏览器中打开index.html。
答案 0 :(得分:0)
您如何运行应用?您是在浏览器中打开index.html还是使用serve -s build
之类的东西?
如果您是通过第一种方法这样做的,那么您应该考虑尝试serve -s build
答案 1 :(得分:0)
您需要将'homepage': '.'
添加到package.json中。
发件人: https://github.com/facebook/create-react-app/issues/6275#issuecomment-457813277
答案 2 :(得分:0)
"homepage":"path\to\your\project\build\"
npm run build
。这里的问题是当您运行npm run build命令而没有在package.json中添加主页条目时,它是假设项目目录(运行命令的位置)托管在服务器根目录下构建的。
D:\git_repo\my-app>npm run build
> my-app@0.1.0 build D:\git_repo\my-app
> react-scripts build
Creating an optimized production build...
Compiled successfully.
File sizes after gzip:
44.57 KB build\static\js\main.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_repo\my-app>