【发布时间】:2020-05-10 09:15:28
【问题描述】:
当我尝试构建我的应用的生产版本时,我继续收到以下错误。 Npm run build 正在公开寻找我的 index.html 文件,但它认为我的公共文件夹在我的根目录中,而它在客户端子文件夹中。
Could not find a required file.
Name: index.html
Searched in: C:\Users\wharfchillin\wharf-chillin-app\public
我的公用文件夹位于我的应用程序的子文件夹中:
client
--->public
------>index.html
我已尝试通过多种方式在我的 server.js 文件中明确说明这一点: 更新
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");
const path = require('path');
const users = require("./routes/api/users");
const plaid = require("./routes/api/plaid");
const app = express();
const publicPath = path.resolve(__dirname);
// Bodyparser middleware
app.use(
bodyParser.urlencoded({
extended: false
})
);
app.use(bodyParser.json());
app.use(express.static(publicPath));
app.use('*', (req, res) => {
res.sendFile(path.resolve(publicPath));
});
// production mode
if(process.env.NODE_ENV === 'production')
app.use(express.static(path.resolve(publicPath)));
console.log('test')
console.log(publicPath)
任何建议都将不胜感激。
【问题讨论】:
-
是不是因为
=登录path.join(__dirname = 'client/build/index.html')?您还可以通过控制台注销__dirname以验证节点从何处运行。此外,如果您可以提供一个示例项目进行测试,那就太好了。 -
在收到下面的答复后,我对 server.js 进行了一些更新,并删除了该行代码,但仍然存在同样的问题(请参阅帖子中更新的 server.js)。一个非常相似的示例项目是:[link]github.com/rishipr/mern-plaid。目前,您运行该项目的方式是使用在 package.json 中定义为
"dev": "concurrently \"npm run server\" \"npm run client\""的 npm run dev。客户端子文件夹中有第二个 package.json 和我的完全一样。如果您可以成功构建此项目,我相信我将能够实施相同的解决方案。 -
我也尝试使用控制台日志语句,我可以将 publicPath 设置为 index.html 所在的正确文件位置(我什至尝试将其设置为确切的路径),但是我仍然遇到在构建过程中找不到 index.html 的问题。这让我觉得无论我在 publicPath 上尝试什么并不能真正解决我的问题。
-
你使用的是什么版本的 react-scripts?尝试更新它
-
我正在运行最新版本,这在 package.json (6.13.4) 中得到了正确反映