webpack配置警告和错误:“模块解析失败”

时间:2016-04-20 03:14:56

标签: javascript json node.js webpack

我正在尝试使用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'],
    },
};

1 个答案:

答案 0 :(得分:1)

问题1: 我认为你的代码库中有类似的内容。即,需要一个名称在变量中的模块。在这种情况下,webpack并不确切知道您需要哪个模块,因此它将加载所有文件。

const moduleName = xxx const module = require(moduleName)

问题2: 您需要在{ test: /\.json$/, loader: "json" }中添加module/loaders以加载json文件。