错误[preprocess]:无法加载" webpack"! (webpack@2.2.1; karma@1.4.1)

时间:2017-02-12 14:21:13

标签: javascript angular webpack jasmine karma-runner

我尝试使用 webpack@^2.2.1 karma@1.4.1 运行我的业力测试。但我得到的只是这个错误:

ERROR [preprocess]: Can not load "webpack"!
  Error
    at webpack (/Users/plano/S/plano/projects/scheduler.frontend.ng2/node_modules/webpack/lib/webpack.js:19:9)
    at new Plugin (/Users/plano/S/plano/projects/scheduler.frontend.ng2/node_modules/karma-webpack/lib/karma-webpack.js:65:18)
    at invoke (/Users/plano/S/plano/projects/scheduler.frontend.ng2/node_modules/di/lib/injector.js:75:15)
    ...

我找到的所有解决方案都是关于 karma@0.12.x 中的错误(例如this github issuethis stackoverflow quertion)。我在1.4.1,所以他们对我没有帮助。

This post告诉我从webpack配置中删除entry: {}。我的webpack配置中没有entry: {}

根据this stackoverflow answer,这是2.2.0-rc.4以来的webpack问题。所以我尝试了2.2.0-rc.3。什么都没有改变。

我的karma.conf.js:

'use strict';

module.exports = config => {
    config.set({
        autoWatch: true,
        browsers: ['Chrome', 'PhantomJS'],
        files: [
            '../node_modules/es6-shim/es6-shim.min.js',
            'karma.entry.js'
        ],
        frameworks: ['jasmine'],
        logLevel: config.LOG_INFO,
        phantomJsLauncher: {
            exitOnResourceError: true
        },
        port: 9876,
        preprocessors: {
            'karma.entry.js': ['webpack', 'sourcemap']
        },
        reporters: ['dots'],
        singleRun: true,
        webpack: require('../webpack/webpack.test.js'),
        webpackServer: {
            noInfo: true
        }
    });
};

我的webpack.test.js:

'use strict';

const path = require('path');
const webpack = require('webpack');

module.exports = {
    devtool: 'inline-source-map',
    module: {
        preLoaders: [
            { exclude: /node_modules/, loader: 'tslint', test: /\.ts$/ }
        ],
        loaders: [
            { loader: 'raw', test: /\.(css|html)$/ },
            { exclude: /node_modules/, loader: 'ts', test: /\.ts$/ },
            {
                test: /\.scss$/,
                exclude: /node_modules/,
                loaders: ['raw-loader', 'sass-loader'] // sass-loader not scss-loader
            }
        ]
    },
    resolve: {
        extensions: ['', '.js', '.ts'],
        modulesDirectories: ['node_modules'],
        root: path.resolve('.', 'src')
    },
    tslint: {
        emitErrors: true
    }
};  

2 个答案:

答案 0 :(得分:0)

解决。

我的 webpack.test.js 中有这样的选项:

f = open(file_name, 'w+')
f.write(json.dumps(new_output, indent=4, sort_keys=True, default=unicode))
f.close()

Webpack 2具有非常严格的配置。现在它看起来像这样:

tslint: {
    emitErrors: true
}

此处有更多信息:https://github.com/gonzofish/semaphore-ng2-webpack/pull/4#issuecomment-279374763

答案 1 :(得分:0)

当我忘记将webpack:选项添加到我的Karma配置中时,出现了此错误。