0

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)
    ....
3
  • 1
    You are confusing the two approaches here from this page: requirejs.org/docs/node.html, just do npm i requirejs and don't run through r.js Commented Jan 29, 2023 at 13:18
  • If you are using the r.js file directly, replace require('requirejs') with require('./path/to/r.js'). Commented Jan 29, 2023 at 13:18
  • 1
    @Joel Thanks! I was mixing the two things together. It worked now Commented Jan 29, 2023 at 13:24

1 Answer 1

0

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').

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.