2017-01-29 29 views
0

我已经成功使用webpack为我的节点服务器构建了express,但我似乎发现错误不是解决方案。希望可以有人帮帮我。谢谢!

当API被称为

TypeError: $JSON.stringify.apply is not a function 
    at stringify (webpack:///./~/core-js/library/fn/json/stringify.js?:13:26) 
    at eval (webpack:///./app/schedules/Route.js?:34:48) 
    at Layer.handle [as handle_request] (/Users/username/Project/project- server/node_modules/express/lib/router/layer.js:95:5) 

的航线代码

.post(function (req, res) { 
    let url_id = (req.params.id) ? req.params.id : null; 
    let schedule = JSON.stringify(req.body.data); 
    ... 

的WebPack配置部分

const path = require('path'); 
const webpack = require('webpack'); 
const fs = require('fs'); 

var nodeModules = {}; 
fs.readdirSync('node_modules') 
.filter(function(x) { 
    return ['.bin'].indexOf(x) === -1; 
}) 
.forEach(function(mod) { 
    nodeModules[mod] = 'commonjs ' + mod; 
}); 

module.exports = { 
    target: 'node', 
    devtool: 'eval', 
    context: __dirname + "/", 
    entry: './server', 
    output: { 
     path: path.join(__dirname, 'dist'), 
     filename: 'app.js', 
     //publicPath: '/static/', 
    }, 
    module: { 
     loaders: [ 
      { 
       loader: 'babel-loader', 
       query: { 
        presets: ["es2015", "react", "stage-2"], 
        plugins: ["transform-runtime","transform-class-properties"] 
      } 
     }, 
     { 
      loader: 'json-loader', 
      test: /\.json$/ 
     } 
    ], 
    plugins: [ 
     new webpack.optimize.UglifyJsPlugin({ 
      compressor: { 
       warnings: false, 
      }, 
     }), 
     new webpack.DefinePlugin({ 
      'process.env.NODE_ENV': JSON.stringify('production') 
     }), 
     new webpack.IgnorePlugin(/\.(css|less)$/), 
     new webpack.BannerPlugin('require("source-map-support").install();', { raw: true, entryOnly: false }) 
    ] 
    }, 
    node: { 
     fs: "empty", 
     net: 'empty', 
     tls: 'empty', 
     dns: 'empty' 
    }, 
    externals: nodeModules 
}; 

回答

0

似乎有关webpack为节点目标包装JSON对象的方式。它发生在我身上,并在他们的GitHub上打开了一个issue

我知道这不是最干净的解决方案 - 至少可以说 - 但同时你可以在你的代码上使用eval('JSON').stringify,它会工作得很好。

编辑:exclude: /node_modules/添加到我的webpack配置文件为babel-loader配置为我修复它。

相关问题