gpt4 book ai didi

javascript - 找不到主模块中的 WEBPACK5 错误 : Error: Can't resolve './src'

转载 作者:行者123 更新时间:2023-12-04 03:44:47 24 4
gpt4 key购买 nike

您只想使用 webpack5 分别构建 Express 和 gql 服务器相关文件。
但是,在构建时会发生错误。

ERROR in main
Module not found: Error: Can't resolve './src' in'/Users/leedonghee/Dropbox/Project/observer/Homepage/v2/server


我的文件不在任何地方使用 ./src 。
目录
server(root)
├─ dist
├─ model(mongoose & gql)
│ ├─ schema.js
│ └─ main-sliders
│ ├─ model.js
│ ├─ resolver.js
│ └─ schema.graphql
├─ node_modules
├─ index.js
├─ webpack.config.js
├─ .babelrc
├─ .env
├─ package.json
├─ package-lock.json
└─ ...
包.json
{
"name": "server",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"dev:server": "NODE_ENV=development nodemon --exec babel-node index.js",
"build": "webpack --mode production --progress",
"start:server": "NODE_ENV=production node ./build/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"core-js": "^3.8.1",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-graphql": "^0.12.0",
"graphql": "^15.4.0",
"graphql-tools": "^7.0.2",
"merge-graphql-schemas": "^1.7.8",
"mongoose": "^5.11.7",
"path-browserify": "^1.0.1",
"regenerator-runtime": "^0.13.7"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/node": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"babel-loader": "^8.2.2",
"clean-webpack-plugin": "^3.0.0",
"nodemon": "^2.0.6",
"webpack": "^5.10.3",
"webpack-cli": "^4.2.0",
"webpack-graphql-loader": "^1.0.2"
}
}

webpack.config.js
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const path = require("path");

module.export = {
entry: "index.js",
mode: "production",
target: "node",
resolve: {
modules: ["node_modules"],
extensions: [".js", ".json"],
fallback: { path: require.resolve("path-browserify") },
},
output: {
publicPath: "/",
path: path.resolve(__dirname, "build"),
filename: "index.js",
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
{
test: /\.graphql?$/,
use: [
{
loader: "webpack-graphql-loader",
options: {
minify: true,
},
},
],
},
],
},
plugins: [new CleanWebpackPlugin()],
};


index.js
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import mongoose from "mongoose";
const { graphqlHTTP } = require("express-graphql");
import schema from "./models/schema";
import "core-js/stable";
import "regenerator-runtime/runtime";

const app = express();
// env --------------------------------------------
dotenv.config();
const { PROD_PORT, DEV_PORT, DB_NAME, DB_URI, NODE_ENV } = process.env;
const port = NODE_ENV === "development" ? DEV_PORT : PROD_PORT;

// DB Setting --------------------------------------------
mongoose.Promise = global.Promise; // db서버 비동기처리
mongoose.connect(DB_URI, {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true,
});
mongoose.connection.once("open", () =>
console.log(`🗄 Connected to "${DB_NAME}" MongoDB server.`)
);
mongoose.connection.on("error", (error) => console.error(`🚫 Error: :`, error));

// Get React Build File --------------------------------------------
const homepage = require("path").join(__dirname, "..", "homepage", "build");
app.use(express.static(homepage));
app.get("/", (req, res) => {
res.sendFile("index.html", { homepage });
});

app.use(
"/graphql",
cors(),
graphqlHTTP({
schema: schema,
graphiql: NODE_ENV === "development" ? true : false,
})
);

app.listen(port, () => {
console.log(
`\n🚀 Server running on http://localhost:${port}\n${
NODE_ENV === "development" ? "⚙️ Development" : "📡 Production"
} mode`
);
});

.babelrc
{
"presets": [
["@babel/preset-env" ,
{
"useBuiltIns" : "usage" ,
"corejs" : 3
}]
]
}

最佳答案

我有同样的问题。 “找不到模块:错误:无法解析 './src' in”。我也是 webpack 的新手,不知道 'src' 是否是必需的目录。但是,我在我的项目文件夹中创建了 src 目录,其中存在 package.json 和 webpack.config.js,然后将我的 index.js 移动到“src”目录中。
此外,如果您查看 the basic setup 的文档和 configuration ,它们都假定 src 作为 index.js 的源文件夹。可能有一些高级主题,您可以在没有“src”的情况下摆脱困境。虽然我还没有达到那一步。
编辑:看起来您不需要 src 目录。您可以简单地定义自己的entry point在你的 webpack.config.js 文件中。

关于javascript - 找不到主模块中的 WEBPACK5 错误 : Error: Can't resolve './src' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65353873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com