admin管理员组文章数量:1390192
I'm trying to find some information about Webpack and relative imports when working with ES6 and Babel.
I have an import line in a simple entry point in my app:
// app.es6
import * as sayHello from 'sayHello';
console.log(sayHello());
sayHello.es6
is the same directory as app.es6
. (The contents of sayHello.es6
is not significant).
When I run the Webpack piler via the mand line:
$ webpack
I get the following error:
ERROR in ./app/app.es6
Module not found: Error: Cannot resolve module 'sayHello' in /Users/username/Code/projectname/app
This is solved by setting the path to relative:
// app.es6 - note the ./ in the import
import * as sayHello from './sayHello';
console.log(sayHello());
This is a bit of a pain because it's different to the example es6 code on Babel website under the Modules section.
Here is my Webpack config:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: [
'babel-polyfill',
'./app/app.es6'
],
output: {
publicPath: '/',
filename: './dist/app.js'
},
debug: true,
devtool: 'source-map',
module: {
loaders: [
{
test: /\.js|\.es6$/,
exclude: /node_modules/,
loader: 'babel-loader',
query:
{
presets:['es2015']
}
}
]
},
resolve: {
extensions: ['', '.js', '.es6'],
},
};
Question: Does any one know why there is this difference? Or where the relevant information regarding ES6 module imports is in the Webpack documentation?
I'm trying to find some information about Webpack and relative imports when working with ES6 and Babel.
I have an import line in a simple entry point in my app:
// app.es6
import * as sayHello from 'sayHello';
console.log(sayHello());
sayHello.es6
is the same directory as app.es6
. (The contents of sayHello.es6
is not significant).
When I run the Webpack piler via the mand line:
$ webpack
I get the following error:
ERROR in ./app/app.es6
Module not found: Error: Cannot resolve module 'sayHello' in /Users/username/Code/projectname/app
This is solved by setting the path to relative:
// app.es6 - note the ./ in the import
import * as sayHello from './sayHello';
console.log(sayHello());
This is a bit of a pain because it's different to the example es6 code on Babel website under the Modules section.
Here is my Webpack config:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: [
'babel-polyfill',
'./app/app.es6'
],
output: {
publicPath: '/',
filename: './dist/app.js'
},
debug: true,
devtool: 'source-map',
module: {
loaders: [
{
test: /\.js|\.es6$/,
exclude: /node_modules/,
loader: 'babel-loader',
query:
{
presets:['es2015']
}
}
]
},
resolve: {
extensions: ['', '.js', '.es6'],
},
};
Question: Does any one know why there is this difference? Or where the relevant information regarding ES6 module imports is in the Webpack documentation?
Share Improve this question edited Jan 22, 2017 at 12:55 d4nyll 12.7k6 gold badges57 silver badges70 bronze badges asked Sep 10, 2016 at 20:02 Richard FernandezRichard Fernandez 5992 gold badges8 silver badges19 bronze badges 5-
So you want to get rid of
./
in import? – Andrew Li Commented Sep 10, 2016 at 20:06 - Basically, yeah. I was following the Babel module ES6 example and this one threw me. – Richard Fernandez Commented Sep 10, 2016 at 20:08
- Maybe use resolve.modules? Use path to resolve – Andrew Li Commented Sep 10, 2016 at 20:11
-
Where is the example you're looking at that says to leave out the
./
? Babel doesn't have any special logic here, it's the same as arequire
call../sayHello
says to look for a file relative to the current one,sayHello
says to load a module fromnode_modules
calledsayHello
. The example is also wrong because withimport * as sayHello from './sayHello';
sayHello
can only ever be an object, there is no way it can be a function, in spec-pliant ES6. – loganfsmyth Commented Sep 10, 2016 at 20:48 - The example where I leave out the ./ is in the first example. Also, I didn't realise it can only be an object. (something new learnt.) – Richard Fernandez Commented Sep 10, 2016 at 23:03
1 Answer
Reset to default 3This is by design, without prefixing it indicates the module should be loaded from node_modules directory. You can set up an alias in your webpack config which would remove the need for relative module imports
resolve: {
alias: {
sayHello: '/absolute/path/to/sayHello'
},
extensions: ['', '.js', '.es6']
}
Then Webpack would fill in the gaps in your import statements and allow you to omit the relative path and import * as sayHello from 'sayHello';
would work throughout your project
本文标签: javascriptES6 Webpack Relative importsModule not found Error Cannot resolve moduleStack Overflow
版权声明:本文标题:javascript - ES6 Webpack Relative imports - Module not found: Error: Cannot resolve module - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744736691a2622373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论