如何使用webpack在npm软件包子文件夹中导入模块? [英] How would I import a module within an npm package subfolder with webpack?
问题描述
让我们说node_modules
中有一个名为foo的软件包,我想通过webpack&通天塔...
Lets say theres a package in node_modules
called foo and I want to import a module within a library such as foo/module
via webpack & babel...
import Foo from 'foo';
有效
import SomeOtherModule from 'foo/module';
失败,并显示以下内容:
import SomeOtherModule from 'foo/module';
fails with the following:
未找到模块:错误:无法解析模块'foo/module' /Users/x/Desktop/someproject/js
Module not found: Error: Cannot resolve module 'foo/module' in /Users/x/Desktop/someproject/js
这使得webpack似乎在错误的位置而不是node_modules
Which makes make it seem like webpack is looking for the file in the wrong place instead of node_modules
我的webpack.config看起来像这样:
My webpack.config looks like this:
var webpack = require('webpack');
var path = require('path');
module.exports = {
entry: ['babel-polyfill','./js/script.js'],
output: {
path: __dirname,
filename: './build/script.js'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
query: {
cacheDirectory: true,
presets: ['es2015']
}
}
],
},
plugins: [
new webpack.NoErrorsPlugin()
],
stats: {
colors: true
},
devtool: 'source-map'
};
推荐答案
它应与import 'foo/module';
一起使用.如果期望的话,它将解析文件./node_modules/foo/module.js
或./node_modules/foo/module/index.js
,而不会解析不是类似于./node_modules/foo/node_modules/module/index.js
的文件(在这种情况下,最好通过npm安装模块).
It should work with import 'foo/module';
. It will resolve file ./node_modules/foo/module.js
or ./node_modules/foo/module/index.js
and not something like ./node_modules/foo/node_modules/module/index.js
if it expected (in that case you better to install module via npm).
这篇关于如何使用webpack在npm软件包子文件夹中导入模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!