在webpack构建期间无意中解析了JSON文件

时间:2016-12-09 17:27:30

标签: javascript typescript webpack babeljs

每当我使用webpack构建我的项目时,我会遇到错误,似乎webpack正在从我的node_modules加载json文件。我只希望jsjsxtstsx个文件得到解决。我不明白为什么会这样。

这些是一些错误

ERROR in ./~/tough-cookie/package.json
Module parse failed: /mnt/d/Source/samples/cooper/node_modules/tough-cookie/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:9)
    at Parser.pp$4.raise (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:2221:15)
    at Parser.pp.unexpected (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:603:10)
    at Parser.pp.semicolon (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:581:61)
    at Parser.pp$1.parseExpressionStatement (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:966:10)
    at Parser.pp$1.parseStatement (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:730:24)
    at Parser.pp$1.parseBlock (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:981:25)
    at Parser.pp$1.parseStatement (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:709:33)
    at Parser.pp$1.parseTopLevel (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:638:25)
    at Parser.parse (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:516:17)
    at Object.parse (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:3098:39)
    at Parser.parse (/mnt/d/Source/samples/cooper/node_modules/webpack/lib/Parser.js:902:15)
    at DependenciesBlock.<anonymous> (/mnt/d/Source/samples/cooper/node_modules/webpack/lib/NormalModule.js:104:16)
    at DependenciesBlock.onModuleBuild (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
    at nextLoader (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
    at /mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
    at Storage.provide (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:52:20)
    at CachedInputFileSystem.readFile (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:140:24)
    at DependenciesBlock.onLoadPitchDone (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:255:7)
    at DependenciesBlock.loadPitch (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:182:27)
    at DependenciesBlock.doBuild (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:241:4)
    at DependenciesBlock.build (/mnt/d/Source/samples/cooper/node_modules/webpack/lib/NormalModule.js:84:14)
    at Compilation.buildModule (/mnt/d/Source/samples/cooper/node_modules/webpack/lib/Compilation.js:126:9)
    at /mnt/d/Source/samples/cooper/node_modules/webpack/lib/Compilation.js:309:10
    at /mnt/d/Source/samples/cooper/node_modules/webpack/lib/NormalModuleFactory.js:58:13
    at NormalModuleFactory.applyPluginsAsyncWaterfall (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/tapable/lib/Tapable.js:75:69)
    at onDoneResolving (/mnt/d/Source/samples/cooper/node_modules/webpack/lib/NormalModuleFactory.js:38:11)
    at onDoneResolving (/mnt/d/Source/samples/cooper/node_modules/webpack/lib/NormalModuleFactory.js:121:6)
    at /mnt/d/Source/samples/cooper/node_modules/webpack/lib/NormalModuleFactory.js:116:7
    at /mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/async/lib/async.js:726:13
    at /mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/async/lib/async.js:52:16
    at async.forEachOf.async.eachOf (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/async/lib/async.js:236:30)
    at _parallel (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/async/lib/async.js:717:9)
 @ ./~/tough-cookie/lib/cookie.js 41:14-40

ERROR in ./~/hawk/package.json
Module parse failed: /mnt/d/Source/samples/cooper/node_modules/hawk/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:9)
    at Parser.pp$4.raise (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:2221:15)
    at Parser.pp.unexpected (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:603:10)
    at Parser.pp.semicolon (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:581:61)
    at Parser.pp$1.parseExpressionStatement (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:966:10)
    at Parser.pp$1.parseStatement (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:730:24)
    at Parser.pp$1.parseBlock (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:981:25)
    at Parser.pp$1.parseStatement (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:709:33)
    at Parser.pp$1.parseTopLevel (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:638:25)
    at Parser.parse (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:516:17)
    at Object.parse (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/acorn/dist/acorn.js:3098:39)
    at Parser.parse (/mnt/d/Source/samples/cooper/node_modules/webpack/lib/Parser.js:902:15)
    at DependenciesBlock.<anonymous> (/mnt/d/Source/samples/cooper/node_modules/webpack/lib/NormalModule.js:104:16)
    at DependenciesBlock.onModuleBuild (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
    at nextLoader (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
    at /mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
    at Storage.provide (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:52:20)
    at CachedInputFileSystem.readFile (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:140:24)
    at DependenciesBlock.onLoadPitchDone (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:255:7)
    at DependenciesBlock.loadPitch (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:182:27)
    at DependenciesBlock.doBuild (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:241:4)
    at DependenciesBlock.build (/mnt/d/Source/samples/cooper/node_modules/webpack/lib/NormalModule.js:84:14)
    at Compilation.buildModule (/mnt/d/Source/samples/cooper/node_modules/webpack/lib/Compilation.js:126:9)
    at /mnt/d/Source/samples/cooper/node_modules/webpack/lib/Compilation.js:309:10
    at /mnt/d/Source/samples/cooper/node_modules/webpack/lib/NormalModuleFactory.js:58:13
    at NormalModuleFactory.applyPluginsAsyncWaterfall (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/tapable/lib/Tapable.js:75:69)
    at onDoneResolving (/mnt/d/Source/samples/cooper/node_modules/webpack/lib/NormalModuleFactory.js:38:11)
    at onDoneResolving (/mnt/d/Source/samples/cooper/node_modules/webpack/lib/NormalModuleFactory.js:121:6)
    at /mnt/d/Source/samples/cooper/node_modules/webpack/lib/NormalModuleFactory.js:116:7
    at /mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/async/lib/async.js:726:13
    at /mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/async/lib/async.js:52:16
    at async.forEachOf.async.eachOf (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/async/lib/async.js:236:30)
    at _parallel (/mnt/d/Source/samples/cooper/node_modules/webpack/node_modules/async/lib/async.js:717:9)
 @ ./~/hawk/lib/utils.js 14:11-37
...

我有以下webpack-config.js

var path = require("path")

module.exports = {
    entry: {
        index: "index.tsx"
    },
    output: {
        path: path.resolve(__dirname, "build"),
        filename: "[name].js"
    },
    resolve: {
        root: [
            path.resolve(__dirname, "src")
        ],
        extensions: ["", ".ts", ".tsx", ".js", ".jsx"]
    },
    module: {
        loaders: [
            { 
                test: /\.tsx?$/,
                loaders: [
                    "babel-loader",
                    "ts-loader"
                ]
            },
            {
                test: /\.jsx?$/,
                loaders: [
                    "babel-loader"
                ]
            }
        ]
    },
    plugins: [
    ],
    target: "electron-renderer",
    node: {
        __dirname: false,
        __filename: false
    }
}

2 个答案:

答案 0 :(得分:1)

您需要在Webpack配置中加载Lazy<T>,因为正在使用的json-loader模块加载parses its package.json file

tough-cookie

答案 1 :(得分:0)

尝试从加载器中排除node_modules文件夹。在所有加载器的exclude属性上使用以下正则表达式

        { 
            test: /\.jsx?$/,
            loaders: ['react-hot', 'babel-loader'],
            exclude: /(node_modules|bower_components)/,
        },

also see the webpack documentation here