javascript 如何在服务器端使用 require.js?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8131738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use require.js on the serverside?
提问by kevlar
I use it on the client right now with Backbone.js, but I'd like to use it with node.js also.
我现在将它与 Backbone.js 一起在客户端上使用,但我也想将它与 node.js 一起使用。
回答by Rafael
There are directions on the requirejs homepage that I found pretty useful: http://requirejs.org/docs/node.html
我发现 requirejs 主页上的一些说明非常有用:http: //requirejs.org/docs/node.html
I also set up some examples here: https://github.com/rgarcia/node-requirejs-examples
我还在这里设置了一些示例:https: //github.com/rgarcia/node-requirejs-examples
The basic idea is that you use it just like on the client side, but for npm modules and built-in node modules, you don't use the relative path, but rather the module name. For all of your custom modules you use the relative path.
基本思想是您像在客户端一样使用它,但是对于 npm 模块和内置节点模块,您不使用相对路径,而是使用模块名称。对于所有自定义模块,您都使用相对路径。
回答by steveyang
Here is the doc for usage of requireJS on node.js
这是在 node.js 上使用 requireJS 的文档
http://requirejs.org/docs/node.html
http://requirejs.org/docs/node.html
Install the node.js
安装 node.js
npm install requirejs
Usage example
使用示例
var requirejs = require('requirejs');
requirejs(['foo', 'bar'],
function (foo, bar) {
//foo and bar are loaded according to requirejs
//config, but if not found, then node's require
//is used to load the module.
});