简体   繁体   中英

webpack configuration warnings and errors: "module parse failed"

I'm trying to build a simply application using the MEAN stack, and I'm running into an issue with Webpack. When I run "webpack" from the console, I get the following warnings and errors:

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.

So my questions are:

1. Should Webpack even be trying to load files like README.md and LICENSE? Why would it care about those?

2. I have a json-loader hooked up and looking for .json files, so why am I still getting warnings and errors about those?

Here's my Webpack config file:

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'],
    },
};

question 1: I think there is something like following in your codebase. namely, require a module whose name is in a variable. In this case, webpack don't know exactly which module you are requiring, so it will load all file.

const moduleName = xxx const module = require(moduleName)

question 2: you need to add { test: /\\.json$/, loader: "json" } in module/loaders for loading json file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM