可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm having trouble trying to figure out the reason for this error when trying to start karma:
bash-3.2$ NODE_ENV=test karma start INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/ INFO [launcher]: Starting browser PhantomJS INFO [PhantomJS 1.9.8 (Mac OS X)]: Connected on socket FONtQ1oNxIxRecRxYgnS with id 73511945 PhantomJS 1.9.8 (Mac OS X) ERROR ReferenceError: Can't find variable: require at /public/components/angular-cookies/index.js:1
The index.js file contains:
require('./angular-cookies'); module.exports = 'ngCookies';
Please help!!
回答1:
One way to fix this is to npm install karma-browserify --save-dev
. That should work and you will have to do that if the actual file you need uses require.
index.js
is not the actual file you need. angular-cookies.js
is.
I find that using browserify makes debugging test errors more difficult because the stack trace you get from karma would refer to line numbers from temporary .browserify bundles that already got deleted by the time your test is done.
A simple solution is to point files
in karma.conf.js
directly to angular-cookies.js
instead of using wildcards. This way, index.js
and its usage of require will not cause unnecessary issue.
回答2:
require is nodejs feature try install this karma-browserify, it allow you to use "require"
回答3:
Try adding browserify as a
preprocessor: 'test/**/*.js': ['browserify']
and add it to your frameworks