我正在尝试创建一个小的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 build(运行webpack --display-error-details --mode production)时,webpack抛出一个大错误,提示“找不到模块:错误:无法解析'my \ project \ dir'中的'./endpoints'。 \ src'。
我的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']
}
};
我可以看到以前曾问过类似的问题,并且列出的解决方案似乎对我不起作用,所以我认为我应该将其发布,以防万一我出现了菜鸟错误。如果需要更多信息,请说!对不起,如果它是一堆文字。谢谢。
正确的导入将是:
import endpoint1 from 'endpoint1';
通过使用resolve.modules
告诉Webpack在该文件夹中查找非相对路径。模块名称为“ enpoint1”。
但是实际上,您只应该对整个项目中使用的库执行此操作,对于端点而言,相对导入将是合适的:
import endpoint1 from "./endpoints/endpoint1";
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句