我在es6
中编写了一个非常简单的Node.js模块export default 'abc';
我希望能够在转换到ES5后在另一个Node.js脚本中使用它:
var helper = require('./dist/helper.js');
console.log(helper)
出于某种原因 - 在翻译之后我得到了
“{}”日志
我想说“abc”......
我哪里错了?
我的webpack.conf:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry:[
'./es6/helper.js'
],
target: 'node',
output:{
publicPath: '/',
filename: './dist/helper.js'
},
debug:'true',
devtool: 'source-map',
module: {
loaders: [
{
test: /\.js$/,
include: path.join(__dirname, 'es6'),
loader: 'babel-loader',
query: {
presets: ['es2015']
}
}
]
},
devServer: {
contentBase: "./es6"
}
}
答案 0 :(得分:0)
答案是
添加libraryTarget:'commonjs2',
在webpack.conf中:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry:[
'./es6/helper.js'
],
target: 'node',
output:{
libraryTarget:'commonjs2',
publicPath: '/',
filename: './dist/helper.js'
},
debug:'true',
devtool: 'source-map',
module: {
loaders: [
{
test: /\.js$/,
include: path.join(__dirname, 'es6'),
loader: 'babel-loader',
query: {
presets: ['es2015']
}
}
]
},
devServer: {
contentBase: "./es6"
}
}