简体   繁体   中英

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM