Javascript 找不到模块:错误:无法解析模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34968808/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:07:46  来源:igfitidea点击:

Module not found: Error: Cannot resolve module

javascriptwebpackbabeljs

提问by Knows Not Much

I have created a simple project which uses babel and webpack. I have checked it in here

我创建了一个使用 babel 和 webpack 的简单项目。我已经在这里检查过了

https://github.com/abhitechdojo/MovieLensReact.git

https://github.com/abhitechdojo/MovieLensReact.git

In my root folder I have two files script1.js and script2.js. My webpack.config.js looks like

在我的根文件夹中,我有两个文件 script1.js 和 script2.js。我的 webpack.config.js 看起来像

module.exports = {
    entry : {
        main: [
            'script1.js', 'script2.js'
        ]
    },
    output : {
        filename: 'public/main.js'
    },
    "module" : {
        "loaders" : [
            {
                "test": /\.jsx?/,
                "exclude": /node_modules/,
                loader: 'babel',
                query: {
                    presets: ['es2015', 'react']
                }               
            }
        ]
    }
}

but when I run webpack. it cannot find any javascript files

但是当我运行 webpack 时。它找不到任何 javascript 文件

ERROR in multi main
Module not found: Error: Cannot resolve module 'script1.js' in /Users/abhishek.srivastava/MyProjects/MovieLensReact
 @ multi main

ERROR in multi main
Module not found: Error: Cannot resolve module 'script2.js' in /Users/abhishek.srivastava/MyProjects/MovieLensReact
 @ multi main

回答by Pierre Emmanuel Lallemant

In nodejs, when you call require("script1.js")it won't search in the current folder.

在 nodejs 中,调用require("script1.js")时不会在当前文件夹中搜索。

You have to use require("./script2.js"), to specify that the file is in the current folder.

您必须使用require("./script2.js"), 指定文件位于当前文件夹中。

In your case, modify the config file with main: ['./script1.js', './script2.js'].

在您的情况下,使用main: ['./script1.js', './script2.js'].