2

我在尝试运行时看到错误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,因为快速入门中缺少它,但它现在应该可用,对吧?

4

3 回答 3

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
于 2018-05-14T15:46:15.980 回答
1

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

npm install --save-dev webpack

于 2017-09-08T16:04:33.780 回答
-1

我找到了这个解决方案

将这些添加到您package.json的依赖项中

{

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

 "karma-webpack": "^2.0.3",

}

然后然后npm i -:npm run test

于 2018-07-13T09:26:04.577 回答