【问题标题】:npm run build cannot find index.html in public subfoldernpm run build 在公共子文件夹中找不到 index.html
【发布时间】: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) 中得到了正确反映

标签: node.js reactjs npm


【解决方案1】:

您正在使用window os,window os的路径使用反斜杠,但在您的节点应用程序中,您使用正斜杠定义路径,节点有api来处理这个,path.resolve。停止使用 path.join

【讨论】:

  • 您好。我做了这些编辑,但不幸的是我仍然收到同样的错误。仍然找不到 Index.html。如果有帮助,此示例项目与我的项目结构非常相似:[链接]github.com/rishipr/mern-plaid。目前,您运行该项目的方式是使用在 package.json 中定义为 "dev": "concurrently \"npm run server\" \"npm run client\"" 的 npm run dev。客户端子文件夹中有第二个 package.json 和我的完全一样。如果您可以成功构建此项目,我相信我将能够实施相同的解决方案。
  • 你能用上面提到的更新代码更新问题吗?也确实运行 npm build 来构建 index.html?
  • 所以我能够通过在我的 package.json 中包含以下内容来创建一个构建:"build": "cd client && react-scripts build",。不幸的是,我让构建工作的目标是能够在 Heroku 上进行部署。当我将此构建推送到 Heroku 时,我收到一条错误消息,指出存在“无效的主机标头”......我将尝试解决另一件事!
猜你喜欢
  • 2018-01-22
  • 1970-01-01
  • 2020-04-05
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-09
相关资源
最近更新 更多