For a project that I am working on I have been using a hodgepodge of JavaScript libraries. The main logic of my code is broken down into multiple commonjs modules. I use google closure to combine the modules into one output js file which I use within my AngularJS application.
The problem I am having is trying to perform tests with testacular. There error I receive is Uncaught ReferenceError: require is not defined
. It is happening because, unlike google closure, testacular doesn't understand commonjs modules. There are a couple work arounds I can do, but I was hoping to make it work without having to restructure my code.
Has anyone else run into a similar problem? I'm open for trying different things; I just don't want anything hacky.
javaclassstreamreader.spec.js:
"use strict"
var JavaClassStreamReader = require('../javaclassstreamreader.js').JavaClassStreamReader;
describe('javaclassstreamreader', function() {
it('reader can be constructed', function() {
var dataView = {
byteLength : 0
};
//FIXME load dataView
var reader = new JavaClassStreamReader(dataView);
expect(reader.dataView).toBe(dataView);
expect(reader.offset).toBe(0);
expect(reader.maxOffset).toBe(0);
});
});
javaclassstreamreader.js:
function JavaClassStreamReader(dataView, initialOffset, maxBytesToRead) {
this.dataView = dataView;
this.offset = initialOffset || 0;
this.maxOffset = this.offset + (maxBytesToRead || this.dataView.byteLength);
}
//... code trucated ...
To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.
This usually happens because your JavaScript environment doesn't understand how to handle the call to require() function you defined in your code. Here are some known causes for this error: Using require() in a browser without RequireJS. Using require() in Node.
1) require()require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object. require() statement not only allows to add built-in core NodeJS modules but also community-based and local modules.
To include the Require. js file, you need to add the script tag in the html file. Within the script tag, add the data-main attribute to load the module. This can be taken as the main entry point to your application.
It seems there is/was an issue with Testacular.
Could you try the following:
npm cache clean
npm install -g [email protected]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With