【发布时间】:2019-11-08 21:51:37
【问题描述】:
我正在尝试创建一个小型 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 run build(运行 webpack --display-error-details --mode production)webpack 抛出一个大错误,说 “Module not发现:错误:无法解析“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']
}
};
我可以看到之前有人问过类似的问题,并且列出的解决方案似乎对我不起作用,所以我想我会发布它以防我犯了新手错误。如果需要更多信息,请说!对不起,如果它是相当多的文字墙。谢谢。
【问题讨论】:
-
模块名称为“endpoint1”
-
应该将其发布为答案,乔纳斯·威尔姆斯。我认为这是有效的,因为它解决了他的问题
-
'./endpoints'指的是./endpoints/index.js,因为它是一个目录。您要么想导入'./endpoints/endpoint1',要么创建index.js,然后导出endpoint1 -
感谢您的快速回复!有没有办法我可以导入包含模块的目录?从目录中显式导入每个模块会有点麻烦...
-
@ToastedToast 我在编辑中提到了它
标签: javascript npm webpack babeljs