Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Karma: 'require' is not defined

My angular test environment was running just fine until a week ago when I started getting this error whenever I tried to run karma:

Chrome 53.0.2785 (Linux 0.0.0) ERROR
  Uncaught ReferenceError: require is not defined
  at node_modules/chai-as-promised/lib/chai-as-promised.js:2

This error first occurred in the CI platform. It only started to happen in my local environment when I cleaned and reinstalled all my app's dependencies so I can conclude that some dependency got an update and now something is missing.

I found found similar issues that recommended adding extra dependencies such as requirejs and browserify to the node dependencies list as well as to the karma config file but they didn't help.

What else could be causing this error? I have the following plugins loaded to karma:

    plugins: [
        'karma-jasmine',
        'karma-mocha',
        'karma-chrome-launcher',
        'karma-chai',
        'karma-chai-as-promised',
        'karma-jquery-chai',
        'karma-mocha',
        'karma-coverage',
        'karma-ng-html2js-preprocessor',
        'karma-ng-json2js-preprocessor',
        'karma-sinon',
        'karma-sinon-chai'
    ]
like image 301
ecc Avatar asked Oct 07 '16 13:10

ecc


1 Answers

The problem is that chai-as-promised 6.0 dropped support for SystemJS, so the way that karma-chai-as-promised pulls in chai-as-promised no longer works.

I ended up removing karma-chai-as-promised and manually hooking up chai-as-promised in a common test initialization file: chai.use(chai-as-promised).

like image 189
Randy Coulman Avatar answered Oct 25 '22 10:10

Randy Coulman