Angular 2 npm test - 无法加载“webpack”

时间:2017-09-08 16:02:17

标签: angular karma-webpack

我尝试运行npm test

时发现错误
08 09 2017 16:50:50.240:ERROR [preprocess]: Can not load "webpack"!
  TypeError: Cannot read property 'plugin' of undefined
    at PathsPlugin.apply (/Users/m/Sites/budget-angular2/node_modules/@ngtools/webpack/src/paths-plugin.js:75:18)
    at Resolver.apply (/Users/m/Sites/budget-angular2/node_modules/tapable/lib/Tapable.js:375:16)
    at /Users/m/Sites/budget-angular2/node_modules/enhanced-resolve/lib/ResolverFactory.js:249:12
    at Array.forEach (native)
    at Object.exports.createResolver (/Users/m/Sites/budget-angular2/node_modules/enhanced-resolve/lib/ResolverFactory.js:248:10)
    at WebpackOptionsApply.process (/Users/m/Sites/budget-angular2/node_modules/webpack/lib/WebpackOptionsApply.js:282:46)
    at webpack (/Users/m/Sites/budget-angular2/node_modules/webpack/lib/webpack.js:36:48)
    at new Plugin (/Users/m/Sites/budget-angular2/node_modules/karma-webpack/lib/karma-webpack.js:63:18)
    at invoke (/Users/m/Sites/budget-angular2/node_modules/di/lib/injector.js:75:15)
    at Array.instantiate (/Users/m/Sites/budget-angular2/node_modules/di/lib/injector.js:59:20)
    at get (/Users/m/Sites/budget-angular2/node_modules/di/lib/injector.js:48:43)
    at /Users/m/Sites/budget-angular2/node_modules/di/lib/injector.js:71:14
    at Array.map (native)
    at Array.invoke (/Users/m/Sites/budget-angular2/node_modules/di/lib/injector.js:70:31)
    at Injector.get (/Users/m/Sites/budget-angular2/node_modules/di/lib/injector.js:48:43)
    at instantiatePreprocessor (/Users/m/Sites/budget-angular2/node_modules/karma/lib/preprocessor.js:55:20)
    at /Users/m/Sites/budget-angular2/node_modules/karma/lib/preprocessor.js:106:17
    at Array.forEach (native)
    at /Users/m/Sites/budget-angular2/node_modules/karma/lib/preprocessor.js:103:27
    at module.exports (/Users/m/Sites/budget-angular2/node_modules/karma/node_modules/isbinaryfile/index.js:28:12)
    at /Users/m/Sites/budget-angular2/node_modules/karma/lib/preprocessor.js:84:7
    at /Users/m/Sites/budget-angular2/node_modules/graceful-fs/graceful-fs.js:78:16
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:446:3)
08 09 2017 16:50:50.257:WARN [karma]: No captured browser, open http://localhost:9876/
08 09 2017 16:50:50.264:INFO [karma]: Karma v1.2.0 server started at http://localhost:9876/
08 09 2017 16:50:50.264:INFO [launcher]: Launching browser Chrome with unlimited concurrency
08 09 2017 16:50:50.265:ERROR [karma]: Found 1 load error

我的package.json文件中包含以下内容:

"jasmine-core": "2.5.2",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"karma-webpack": "^2.0.4",

我看过一些帖子是karma-webpack版本,但推荐的版本似乎比我在这里更老。我确实必须手动npm install karma-webpack --save-dev,因为它从快速启动中丢失了,但它现在应该可用了,对吗?

3 个答案:

答案 0 :(得分:1)

karma-webpack具有webpack的对等依赖关系。我没有在你列出的package.json中看到webpack(除非那不是完整列表)。您还需要安装webpack

npm install --save-dev webpack

答案 1 :(得分:1)

您需要升级到更新版本的angular CLI。您可以使用ng new my-app

删除已创建的项目文件夹

卸载旧的 angular-cli

npm uninstall angular-cli -g

安装新的 @ angular-cli

npm install @angular/cli -g

再次开始新项目

ng new my-app

然后,运行测试

ng test

答案 2 :(得分:-1)

我找到了解决方案

将它们添加到您的package.json依赖项中

{

"karma-sourcemap-loader": "^0.3.7",

 "karma-webpack": "^2.0.3",

}

,然后npm i 然后-:npm run test