简体   繁体   English

Node.js require()vs RequireJS?

[英]Node.js require() vs RequireJS?

Hello with RequireJS I can set a base path like this: base : './app/' so when I am in ./app/foo/bar/ for example and I have a script where I use require('foo'); 你好RequireJS我可以这样设置一个基本路径: base : './app/'所以当我在./app/foo/bar/例如我有一个脚本,我使用require('foo'); RequireJS then would search for ./app/foo.js and not in node_module folder or in ./app/foo/bar/foo.js this comes handy when you have a kind of structure where it would be much cleaner for you as a developer to see the dependencies instead of having ../../foo.js . 然后RequireJS将搜索./app/foo.js而不是在node_module文件夹或./app/foo/bar/foo.js当你有一种结构对你来说它会更干净时这会很方便开发人员看到依赖项而不是../../foo.js I could have ./app/foo.js and ./app/foo/foo.js and ./app/foo/bar/foo.js it would be much more cleaner to have: 我可以拥有./app/foo.js./app/foo/foo.js./app/foo/bar/foo.js它会更清晰:

require('foo');
require('foo/foo');
require('foo/bar/foo');

rather than: 而不是:

require('../../foo');
require('../foo');
require('./foo');

Now you could say why not change the name and not have foo everywhere, let's say that we can't for any reason… 现在你可以说为什么不改变名称而不是foo无处不在,让我们说我们不能出于任何原因......

Another lack of feature that I see in node's require method against RequireJS is the ability of setting path mapping, if I have a directory named ./app/super-sized-directory-name/ in RequireJS I could simply do 'big-dir' : 'super-sized-directory-name' and then I could simply use require('./app/big-dir/foo') with Node.js's require method this is not possible as far as I know… 如果我在RequireJS中有一个名为./app/super-sized-directory-name/的目录,我在node的./app/super-sized-directory-name/要求方法中遇到的另一个缺点是设置路径映射,我可以简单地做'big-dir' : 'super-sized-directory-name'然后我可以简单地使用require('./app/big-dir/foo')和Node.js的require方法,据我所知这是不可能的...

--alias, -a    Register an alias with a colon separator: "to:from"
             Example: --alias 'jquery:jquery-browserify'   

You can register aliases with browserify, so that covers your renaming. 您可以使用browserify注册​​别名,以便覆盖您的重命名。

As for your rooted absolute paths, that can't really be done. 至于你扎根的绝对路径,那是不可能完成的。 As mentioned modul8 has a namespacing mechanism to solve this. 如上所述, modul8有一个命名空间机制来解决这个问题。

I would recommend you pong SubStack in #stackvm on freenode and ask him directly. 我建议你在freenode的#stackvm中使用SubStack并直接问他。

It may or may not help you, but I believe the Dojo Frameworks AMD Loader is API compatible with RequireJS and providing you are using a new microkernel does not pollute the global namespace. 它可能会或可能不会帮助您,但我相信Dojo Frameworks AMD Loader与RequireJS API兼容,并且假设您使用新的微内核不会污染全局命名空间。

I believe it only has require() and define() in the global namespace now. 我相信它现在只在全局命名空间中有require()define()

Anyway their method of dealing with this is to do something like: 无论如何,他们处理这个的方法是做类似的事情:

require(["dojo/node!util"], function(util){
    // Module available as util
});

The documentation is at http://dojotoolkit.org/reference-guide/1.8/dojo/node.html 该文档位于http://dojotoolkit.org/reference-guide/1.8/dojo/node.html

Use uRequire which provides a 'bridge' between nodejs require and AMD define modules, without reinventing the wheel (it is build on top of the two standards). 使用uRequire在nodejs require和AMD define模块之间提供“桥梁”,而无需重新发明轮子(它建立在两个标准之上)。 It basically converts modules from AMD or commonJS format to the other format or UMD that runs smoothly on both nodejs & the browser. 它基本上将模块从AMD或commonJS格式转换为在nodejs和浏览器上平滑运行的其他格式或UMD

It is also translating dependency paths with flexible path conventions , so you can have either '../../foo' or 'bar/foo' depending on which makes more sense at the point you are at. 它还使用灵活的路径约定来转换依赖路径,因此您可以使用'../../foo''bar/foo'具体取决于您在哪个位置更有意义。

Your AMD or UMD modules are loaded asynchronously on browser (using AMD/requireJs or other AMD loader) and on node the asynchronous require(['dep1', 'dep2'], function(dep1,dep2){...}) is also simulated. 您的AMD或UMD模块在浏览器上异步加载(使用AMD / requireJs或其他AMD加载程序),在节点上异步require(['dep1', 'dep2'], function(dep1,dep2){...})是也模拟了。

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

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