3

How to use require.js in node.js?

why the following code does not work?

var requirejs = require('requirejs');

requirejs.config({
nodeRequire: require
});

requirejs(['foo'],
function (foo) {
    console.log(foo);
});

I have tried various things, including using requirejs(['./foo']. I'm unable to get anything to work.

I have a file foo.js in the same directory as the main file (the one with the above code). I run the main file with node and foo is undefined. From the examples given on requirejs.org it looks like I am doing it right, but I am certain I am misinterpreting something.

1 Answer 1

1

I just ran your exact code, with a fresh install of requirejs, and a very mininal 'foo' module and it worked fine. I would imagine the problem is in the foo module itself.

Here is what I used

define(function(){
    return 'test';
});

Have you got the module syntax wrong?

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

2 Comments

Thank you! I did not write the module I was trying to load, and I didn't even think that the problem was in the module. I am a little unsure how I am supposed to use the module, but now I know what I was doing wrong. If you know how I am supposed to use the module, it's in the format: var x = new function() { this. y = new function(){}; } I want to be able to use the function y. I thought that what I was doing would allow me to go foo.y().
define(function(){ return { y: new function() { } } });

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.