My Project structure looks like this -
src
|- main.js
|- math.js
math.js
is just an AMD module and I am requiring it in main.js
I have installed require.js
using npm and requiring it in main.js
//main.js
var rjs = require('requirejs');
rjs.config({
//Pass the top-level main.js/index.js require
//function to requirejs so that node modules
//are loaded relative to the top-level JS file.
nodeRequire: require,
});
rjs(["./math.js"], function(math) {
console.log(math.add(2, 3));
});
//math.js
define("math", function() {
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
return a / b;
}
return { add, subtract, multiply, divide };
});
While running r.js ./src/main.js
, I am getting the below error.
rjs.config is not a function
at /Users/dheerajmahra/Desktop/learning/different-module-formats/src/main.js:3:5
at /Users/dheerajmahra/Desktop/learning/different-module-formats/src/main.js:14:2
at Script.runInThisContext (vm.js:96:20)
at Object.runInThisContext (vm.js:303:38)
....
Based of reading the requirejs-docs, it seems that you are conflating the two approaches here, one being through the node-package, the other one being through the r.js
standalone file.
It seems like you either haven't done npm i requirejs
or, you are not running through the node process.
If you are using the r.js
file directly, replace require('requirejs')
with require('./path/to/r.js')
.