ReferenceError: Can't find variable: require at node_modules/angular2-jwt/angular2-jwt.js:16
I have an angular2 version 2.0 application where some services depend on the angular2-jwt.js. When I run my tests I get ReferenceError: Can't find variable: require at node_modules/angular2-jwt/angular2-jwt.js:16
My Karma.conf.js has the following files
files: [
'node_modules/core-js/client/shim.min.js',
'node_modules/zone.js/dist/zone.js',
'node_modules/zone.js/dist/long-stack-trace-zone.js',
'node_modules/zone.js/dist/proxy.js',
'node_modules/zone.js/dist/sync-test.js',
'node_modules/zone.js/dist/jasmine-patch.js',
'node_modules/zone.js/dist/async-test.js',
'node_modules/zone.js/dist/fake-async-test.js',
'node_modules/systemjs/dist/system.src.js',
'node_modules/moment/moment.js',
'node_modules/reflect-metadata/Reflect.js',
'node_modules/angular2-jwt/angular2-jwt.js'
],
I use SystemJS to load the artifacts. My system.config.js is: `/*
- This config is only used during development and build phase only
- It will not be available on production * */
(function (global) { // ENV global.ENV = global.ENV || 'development';
// map tells the System loader where to look for things var map = { 'app': 'src/tmp/app', 'test': 'src/tmp/test' };
// packages tells the System loader how to load when no filename and/or no extension var packages = { 'app': {defaultExtension: 'js'}, 'test': {defaultExtension: 'js'}, 'rxjs': {defaultExtension: 'js'}, 'angular2-jwt': {defaultExtension: 'js'}, 'moment': {main: 'moment.js'}, 'primeng': {defaultExtension: 'js'}, 'ng2-translate': {defaultExtension: 'js'}, 'reflect-metadata': {main: 'Reflect.js', defaultExtension: 'js'} };
// List npm packages here var npmPackages = [ '@angular', 'rxjs', 'lodash', 'angular2-in-memory-web-api', 'moment', 'primeng', 'ng2-translate', 'reflect-metadata' ];
// Add package entries for packages that expose barrels using index.js var packageNames = [ // App barrels 'app/shared',
// 3rd party barrels
'lodash',
'angular2-in-memory-web-api'
];
// Add package entries for angular packages var ngPackageNames = [ 'common', 'compiler', 'core', 'forms', 'http', 'platform-browser', 'platform-browser-dynamic', 'router' ];
npmPackages.forEach(function (pkgName) { map[pkgName] = 'node_modules/' + pkgName; }); map['angular2-jwt'] = 'node_modules/angular2-jwt';
packageNames.forEach(function (pkgName) { packages[pkgName] = {main: 'index.js', defaultExtension: 'js'}; });
ngPackageNames.forEach(function (pkgName) { map['@angular/' + pkgName] = 'node_modules/@angular/' + pkgName + '/bundles/' + pkgName + '.umd.js'; map['@angular/' + pkgName + '/testing'] = 'node_modules/@angular/' + pkgName + '/bundles/' + pkgName + '-testing.umd.js'; });
var config = { map: map, packages: packages };
// filterSystemConfig - index.html's chance to modify config before we register it. if (global.filterSystemConfig) { global.filterSystemConfig(config); }
System.config(config);
})(this);`
Should I include somethinng else in the karma or is the error comming from the way angular2-jwt.js is compiled maybe?
thanks for your help
Try mapping like this https://github.com/auth0-samples/auth0-angularjs2-systemjs-sample/blob/master/01-Login/systemjs.config.js#L39
hi Ryan
actually when I do what you suggested and I try to run the app with npm start now I have in my browser
zone.js:1274 GET http://localhost:3000/npm:angular2-jwt/angular2-jwt.js/angular2-jwt.js 404 (Not Found)
By the way my prior file from my original post works perfectly when I do an npm start. Everything works fine.
But my problem was when I do an npm test. Then I get the error that I posted before
ReferenceError: Can't find variable: require at node_modules/angular2-jwt/angular2-jwt.js:16
Line 16 in angular2-jwt.js is where the first require is used. I could think that the angular2-jwt was not compiled to work with SystemJS or something similar because as you see in my KArma.conf.js there I reference the angular2-jwt.js. Could that be the origin of the problem maybe ?
@escardin did you hit any import issues when doing the karma stuff?
not really
this is my console stacktrace after doing npm test could it be possible that the compiled version of 'angular2-jwt.js' is giving the problem ?
21 09 2016 14:17:07.710:INFO [karma]: Karma v1.2.0 server started at http://localhost:9876/ 21 09 2016 14:17:07.711:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency 21 09 2016 14:17:07.716:INFO [launcher]: Starting browser PhantomJS 21 09 2016 14:17:07.922:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket /#_NGCoB0OAEc9Ev3zAAAA with id 22264639 PhantomJS 2.1.1 (Linux 0.0.0) ERROR ReferenceError: Can't find variable: require at node_modules/angular2-jwt/angular2-jwt.js:16
[14:17:08] Remapping coverage to TypeScript format...
[14:17:08] Test Done with exit code: 1
[14:17:08] 'unit-test' errored after 2.43 s
[14:17:08] Error: 1
at formatError (/home/ovidio/projects/belastingsportaal/projects/bp-web/bp-ui/node_modules/gulp/bin/gulp.js:169:10)
at Gulp.
@chenkie Nope. Pretty sure the tests would have failed or errored or something.
I think why: npm start is working and npm test is failing is because
npm start is using angular2-jwt.ts and is compiled in the browser. npm test is using karma and like I posted in my original post at the start of this thread: karma uses karma.conf.js that in turn in its files: [ ...., 'node_modules/angular2-jwt/angular2-jwt.js' This angular2-jwt.js is provided by angular2-jwt when you do an npm install.
This is giving the problem. Could it be possible that this provided file angular2-jwt.js is compiled for commonjs or something ? CommonJS uses require() and thats why when I do npm test I have the ReferenceError: Can't find variable: require
If this is true is it possible to have a compiled version of angular2-jwt.js that doesnt trhow that error.
thanks
You're compiling in the browser? blech.
Okay, so you need to import system or something, as angular2-jwt is compiled to a commonjs module from the ts file, and as such you need something to provide require(). The tests I wrote for angular2-jwt use webpack, which kind of takes care of all that dependency stuff for me. I would look at some seeds, or what the angular cli generates for systemjs and use that as a template for adding angular2-jwt.
well we compile in the browser for now using SystemJS and not Webpack. We compile in the browser in development mode like recomended by the angular2 site. Once we go to prod we will use a production mode not compiled in browse. But thats a detail, the problem steams that SystemJS is used and not Webpack. By chance could it be possible to have a angular2-jwt.ts compiled to js not for webpack but systemjs ?
You misunderstand me, it's just a normal common JS module. Plenty of people use it successfully without using webpack.
Additionally, I'm pretty sure that the docs on the angular.io site have you run the TS compiler in watch mode, and not compile in the browser. Compile in the browser is way slower, and really only suitable for plunks. I did just review the docs, and while the quick start used to compile in browser, it does not do so anymore.
For me the npm test causes that problem. When I do that karma runs the tests. In karma.conf.js in the files section I reference the node_modules/angular2-jwt/angular2-jwt.js. This .js file is furnished when I install angular2-jwt with npm install. Like you said its compiled as a commonjs module. And thats why I have this problem ReferenceError: Can't find variable: require.
But what solution do you propose. Should I compile myself the node_modules/angular2-jwt/angular2-jwt.ts ? or is there soemthing I could do at the karma level ?
I would look at this: https://github.com/mgechev/angular2-seed It's going to be overly complicated, but you should be able to glean sufficient info for adapting your own config file.
I would also read through this: https://angular.io/docs/ts/latest/guide/testing.html if you haven't already.
Hi @ovione ,
Did any progress with that? I've found this thread because of your error, but I'm using other Angular 2 Module (so I think that's not a problem with angular2-jwt).
Thank's
Is this still an issue? Please reopen or file a new issue if so.