使用babel-register

时间:2016-08-25 00:31:32

标签: javascript node.js babel

我最近在我的代码中添加了babel-register,以便能够在Node / Express中使用ES6,并且遇到了以下问题。当我尝试通过webpack构建时,会抛出以下错误:

webpack building...
/Users/Developer/research-app/node_modules/mongoose/lib/index.js:115
  var queryStringStart = uri.indexOf('?');
                             ^

TypeError: uri.indexOf is not a function
    at checkReplicaSetInUri (/Users/Developer/research-app/node_modules/mongoose/lib/index.js:115:30)
    at Mongoose.connect (/Users/Developer/research-app/node_modules/mongoose/lib/index.js:237:40)
    at Object.<anonymous> (/Users/Developer/research-app/server/server.js:43:10)
    at Module._compile (module.js:425:26)
    at loader (/Users/Developer/research-app/node_modules/babel-register/lib/node.js:146:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/Developer/research-app/node_modules/babel-register/lib/node.js:156:7)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/Users/Developer/research-app/server/index.js:4:1)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:457:10)

所以看起来babel正试图转移node_modules ......虽然我认为默认它被忽略了?无论哪种方式,我该如何解决这个问题?

.babelrc

{
  "presets": [
    "es2015",
    "stage-0",
    "react"
  ],
  "env": {
    "development": {
      "presets": [
        "react-hmre"
      ]
    }
  },
  "plugins": [
    "transform-runtime",
    "transform-class-properties"
  ]
}

webpack.dev.config.js

var path = require('path');
var webpack = require('webpack');
var config = require('./webpack.config.js');

module.exports = Object.assign({}, config, {
  devtool: 'eval',
  entry: [
    'webpack-hot-middleware/client',
    './src/index.js'
  ],
  debug: true,
  plugins: [
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ]
});

webpack.config.js

var path = require('path');
var webpack = require('webpack');

module.exports = {
  output: {
    path: path.join(__dirname, 'public'),
    publicPath: '/public/',
    filename: 'bundle.js'
  },
  resolve: {
    extensions: ['', '.js', '.json']
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
      {
        test: /\.js$/,
        exclude: /(node_modules|tests.webpack.js)/,
        loader: "eslint-loader"
      },
      {
        test: /\.scss?$/,
        loader: 'style!css!sass'
      },
      {
        test: /\.(png|jpg)$/,
        loader: 'url-loader?limit=200000'
      }
    ]
  }
};

server.js

require('babel-register');
require('./server');

0 个答案:

没有答案