2016-03-15 38 views
0

我在ES6是否可以要求轉發到es5 Node.js模塊?

export default 'abc'; 

寫一個非常簡單的Node.js模塊,我希望能夠在其他的Node.js腳本中使用它來transpiling ES5後:

var helper = require('./dist/helper.js'); 
console.log(helper) 

爲某些原因 - transpilation後我得到

「{}」 登錄

我想它說 「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

您可能需要使用默認的導出:'VAR幫手=需要('./dist/helper.js')。default;' - [更多信息](http://stackoverflow.com/questions/33505992/babel-6-changes-how-it-exports-default) – CodingIntrigue

+0

是的,我試過這個 - 日誌是「未定義」 – happyZZR1400

回答

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" 
     } 
    }