简体   繁体   English

Node中的Require.js

[英]Require.js in Node

How to use require.js in node.js? 如何在node.js中使用require.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. 我尝试了各种各样的东西,包括使用requirejs(['./ foo']。我无法得到任何工作。

I have a file foo.js in the same directory as the main file (the one with the above code). 我在与主文件相同的目录中有一个文件foo.js(带有上述代码的文件)。 I run the main file with node and foo is undefined. 我用节点运行主文件,foo未定义。 From the examples given on requirejs.org it looks like I am doing it right, but I am certain I am misinterpreting something. 从requirejs.org上给出的例子看来,我做得对,但我确信我误解了一些东西。

I just ran your exact code, with a fresh install of requirejs, and a very mininal 'foo' module and it worked fine. 我刚刚运行了你的确切代码,全新安装了requirejs,还有一个非常小的'foo'模块,它运行正常。 I would imagine the problem is in the foo module itself. 我想象问题出在foo模块本身。

Here is what I used 这是我用的

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

Have you got the module syntax wrong? 你有错误的模块语法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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