[Webpack在我尝试导入包含模块的目录时出错

问题描述 投票:0回答:2

我正在尝试创建一个小的npm库,以使与API的接口更加整洁。我的文件夹结构如下...

dist/
  index.js
src/
  index.js
  endpoints/
    endpoint1.js
package.json
webpack.config.js

在我的src / index.js文件中。.>

import {endpoint1} from './endpoints'

module.exports = class lib {
 ...
}

当我npm运行构建

(运行webpack --display-error-details --mode production时),webpack抛出一个大错误,提示“找不到模块:错误:无法解决“ my / project \ dir \ src”中的“ ./endpoints”。

我的webpack.config.js文件当前看起来像...

const path = require('path');

module.exports = {
    mode: 'production',
    entry: path.join(__dirname, '/src/index.js'),
    output: {
        path: path.resolve('dist'),
        filename: 'index.js',
        libraryTarget: 'commonjs2'
    },
    module: {
        rules: [
            {
                test: /.js?$/,
                exclude: /(node_modules)/,
                use: 'babel-loader'
            }
        ]
    },
    resolve: {
        modules: [

            path.resolve(__dirname, 'src/endpoints')
        ],
        extensions: ['.js']
    }
};

我可以看到之前曾问过类似的问题,列出的解决方案似乎对我不起作用,所以我认为我应该将其发布,以防万一我出现了菜鸟错误。如果需要更多信息,请说!抱歉,如果它是一堆文字。谢谢。

我正在尝试创建一个小的npm库,以使与API的接口更加整洁。我的文件夹结构如下... dist / index.js src / index.js endpoints / endpoint1.js包。...

javascript npm webpack babel
2个回答
1
投票

正确的导入将是:


0
投票

import endpoint1 from "./endpoints/endpoint1"; 表示:从文件import {endpoint1} from './endpoints'导入以该文件中的名称./endpoints/index.js导出的内容。如果导入目录,则它引用该目录下的enpoint1,而不是所有其他文件。它在您的设置中不存在。

© www.soinside.com 2019 - 2024. All rights reserved.