Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require.js in Node

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.

like image 773
user1756980 Avatar asked Feb 22 '26 23:02

user1756980


1 Answers

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?

like image 90
Mark Withers Avatar answered Feb 25 '26 12:02

Mark Withers