Javascript 找不到模块无法解析

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

Module not found can't resolve

javascriptwebpack

提问by Mr.Smithyyy

I've tried looking at some of the answers to this similar question but none of the solutions I have attempted from there have worked.

我试过查看这个类似问题的一些答案,但我从那里尝试的解决方案都没有奏效。

I'm trying to create a function in an external javascript file and I would like to use that function in my main javascript file.

我正在尝试在外部 javascript 文件中创建一个函数,并且我想在我的主 javascript 文件中使用该函数。

File Structure

文件结构

webpack.config.js
app
-- index.js
-- test.js

webpack.config.js

webpack.config.js

module.exports = {
    devtool: "cheap-module-source-map",
    entry: "./app/index.js",
    output: {
        filename: "public/bundle.js"
    },
    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel-loader',
                query:
                {
                    presets: ['es2015']
                }
            }
        ]
    }
};

test.js

测试.js

export default function test(message) {
    console.log(message);
}

index.js

索引.js

"use strict"

import test from './app/test.js';

When attempting to run the webpack command I get the error:

尝试运行 webpack 命令时出现错误:

ERROR in ./app/index.js

Module not found: Error: Can't resolve './app/test.js'

I've tried changing the file path to a relative one with no luck and I've also tried adding multiple entry points in the webpack config file but still no luck.

我尝试将文件路径更改为相对路径,但没有运气,我也尝试在 webpack 配置文件中添加多个入口点,但仍然没有运气。

回答by Joe Lissner

Posted as answer so that it can be marked as answered, from comment:

发布为答案,以便可以将其标记为已回答,来自评论:

Your import path is wrong, it looks like you should change import test from './app/test.js';to import test from './test.js';

您导入路径是错误的,它看起来像你应该改变import test from './app/test.js';,以import test from './test.js';