我正在尝试使用MEAN堆栈构建一个简单的应用程序,而我遇到了Webpack的问题。当我从控制台运行“webpack”时,我收到以下警告和错误:
WARNING in ./~/require_optional/package.json
Module parse failed: C:\Build\myapp\node_modules\require_optional\package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
WARNING in ./~/require_optional/README.md
Module parse failed: C:\Build\myapp\node_modules\require_optional\README.md Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
WARNING in ./~/require_optional/LICENSE
Module parse failed: C:\Build\myapp\node_modules\require_optional\LICENSE Unexpected token (1:40)
You may need an appropriate loader to handle this file type.
ERROR in ./~/constants-browserify/constants.json
Module parse failed: C:\Build\myapp\node_modules\constants-browserify\constants.json Unexpected token (2:12)
You may need an appropriate loader to handle this file type.
所以我的问题是:
1。 Webpack甚至应该尝试加载README.md和LICENSE等文件吗?为什么会关心这些?
2。我有一个json-loader连接并寻找.json文件,为什么我仍然收到关于那些的警告和错误?
这是我的Webpack配置文件:
webpack.config.js
var config = require('./environment/shared.js')
var debug = config.env !== 'production';
var webpack = require('webpack');
module.exports = {
context: __dirname,
devtool: debug ? 'inline-sourcemap' : null,
entry: './public/js/app.js',
output: {
path: __dirname + '/public/js',
filename: 'bundle.min.js',
},
plugins: debug ? [] : [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
],
module: {
loaders: [
{
test: '/\.json/',
loader: 'json',
},
{
test: '/\.node$/',
loader: 'node-loader',
},
],
},
resolve: {
extensions: ['', '.js', '.json', '.node'],
},
};
答案 0 :(得分:1)
const moduleName = xxx
const module = require(moduleName)
{ test: /\.json$/, loader: "json" }
中添加module/loaders
以加载json文件。