Javascript 未找到入口模块:错误:无法解析“./src/index.js”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45255190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Entry module not found: Error: Can't resolve './src/index.js'
提问by JavaScript Learner
I was installing a react startup app and added Webpack, but it says Can't resolve './src/index.js'.
我正在安装一个 react 启动应用程序并添加了 Webpack,但它显示Can't resolve './src/index.js'.
My Files Path and Package.json Contents
Webpack.config.jsContents
Webpack.config.js内容
var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var path = require('path');
module.exports = {
context: path.join(__dirname, "public"),
devtool: debug ? "inline-sourcemap" : false,
entry: "./src/index.js",
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2016', 'stage-0'],
plugins: ['react-html-attrs', 'transform-decorators-legacy', 'transform-class-properties'],
}
}
]
},
output: {
path: __dirname + "/public/",
filename: "build.js"
},
plugins: debug ? [] : [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
],
};
回答by Kermit
Your base URL is path.join(__dirname, "public"), and your entry is ./src/index.js. Webpack tries to find ./src/index.jsin publicdir; obviously it does not exist. You should modify entryto ../src/index.js.
您的基本 URL 是path.join(__dirname, "public"),您的条目是./src/index.js。Webpack 尝试./src/index.js在publicdir 中查找;显然它不存在。您应该修改entry为../src/index.js.
回答by Lokesh Pandey
The other way I find out to fix this problem is to use path.resolve().
我发现解决此问题的另一种方法是使用path.resolve().
const path = require('path');
module.exports = {
mode: "production",
entry: path.resolve(__dirname, 'src') + 'path/to/your/file.js',
output: {
/*Webpack producing results*/
path: path.resolve(__dirname, "../src/dist"),
filename: "app-bundle.js"
}
}
This will make sure, webpack is looking for entry point in srcdirectory.
这将确保 webpack 正在src目录中寻找入口点。
By the way it's the default entry point. You can also change this entry point to your suitable location. Just replace the srcdirectory with the other directory you want to use.
顺便说一下,它是默认入口点。您还可以将此入口点更改为您合适的位置。只需将该src目录替换为您要使用的其他目录即可。
回答by Greg
My webpack.config.js was named Webpack.config.js and the new cli was looking for something case-sensitive.
我的 webpack.config.js 被命名为 Webpack.config.js 并且新的 cli 正在寻找区分大小写的东西。
回答by Nick
I had the same problem and found that it was caused by having installed create-react-appglobally in the past using npm install -g create-react-app.
我遇到了同样的问题,发现它是由于create-react-app过去使用npm install -g create-react-app.
As create-react-app should now not be installed globally, I needed to uninstall it first using npm uninstall -g create-react-appand then install it in my project directory with npx create-react-app *my-app-name*.
由于 create-react-app 现在不应该全局安装,我需要先使用卸载它npm uninstall -g create-react-app,然后使用npx create-react-app *my-app-name*.
回答by Khauri
The entry path is relative to the context. It's looking for a file in public/src/when you want it to look for a path in just /src. Looking at the rest of your webpack.config.jsit doesn't seem like you need the context line at all.
入口路径是相对于上下文的。它寻找一个文件public/src/时,你想让它在短短的路径/src。看看你的其余部分,你webpack.config.js似乎根本不需要上下文行。

