无法在webpack之后加载JSON:意外的令牌<位于位置0的JSON中

问题描述:

当我使用webpack-dev-server时,一切正常。 但随着“的WebPack”命令在服务器上部署我的应用程序时,我得到了以下错误:无法在webpack之后加载JSON:意外的令牌<位于位置0的JSON中

SyntaxError: Unexpected token < in JSON at position 0 
    at Object.parse (native) 
    at Object.<anonymous> (http://localhost:3389/bundle.js:24629:18) 
    at XMLHttpRequest.respond (http://localhost:3389/bundle.js:24499:32) 

我相信我所收到的是XML而不是JSON。我不知道该怎么办。我正在使用来自'd3-request'的请求加载我的JSON文件。我的webpack.config.js看起来像:

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

module.exports = { 
    context: path.join(__dirname, './app'), 
    entry: { 
    js: './index.js', 
    html: './index.html', 
    }, 
    output: { 
    path: path.join(__dirname, './static'), 
    filename: 'bundle.js', 
    }, 
    module: { 
    preLoaders: [ 
     { test: /\.json$/, loader: 'json'}, 
    ], 
    loaders: [ 
     { 
     test: /\.html$/, 
     loader: 'file?name=[name].[ext]' 
     }, 
     { 
     test: /\.css$/, 
     loaders: [ 
      'style-loader', 
      'css-loader?modules&sourceMap&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]', 
     ] 
     }, 
     { 
     // transpile es6/jsx code to es5 
     test: /\.(js|jsx)$/, 
     exclude: /node_modules/, 
     loaders: [ 
      'babel-loader' 
     ] 
     }, 
     { test: /\.json$/, 
     loader: "json-loader" 
     } 
    ], 
    }, 
    resolve: { 
    extensions: ['', '.js', '.jsx'] 
    }, 
    plugins: [ 
    new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.bundle.js'), 
    new webpack.DefinePlugin({'process.env': { 
     NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development') 
    }}) 
    ], 
    devServer: { 
    contentBase: './app', 
    hot: true 
    } 
} 
+0

你为什么要设置预加载器?任何特定的原因? –

+0

我试图解决这个问题。我看到其他人使用这个设置。它没有改变。 – Nauhc

+0

非常仔细地查看'bundle.js:24629:18'。有时检查产生的包有助于这些。 –

原来是服务器端的快速代码。我不小心让Express从我的index.html文件中输入错字。 webpack没什么问题。