Node.js require() 与 RequireJS?
你好,使用 RequireJS 我可以设置这样的基本路径: base : './app/'
所以当我在 ./app/foo/bar/
时,例如我有一个脚本,我使用 require('foo');
RequireJS 然后会搜索 ./app/foo.js
而不是在 node_module
中> 文件夹或在./app/foo/bar/foo.js
当您有一种结构时,这会很方便,作为开发人员,您可以更清晰地查看依赖项,而不是使用 。 ./../foo.js
。我可以有 ./app/foo.js
和 ./app/foo/foo.js
和 ./app/foo/bar/foo.js< /code> 拥有:
require('foo');
require('foo/foo');
require('foo/bar/foo');
而不是:
require('../../foo');
require('../foo');
require('./foo');
现在你可以说为什么不更改名称并且不在各处都有 foo ,让我们说我们出于任何原因都不能......
我在节点中看到的另一个缺乏功能require方法针对RequireJS的是设置路径映射的能力,如果我在 RequireJS 中有一个名为 ./app/super-sized-directory-name/
的目录,我可以简单地执行 'big-dir' : 'super-sized-directory-name' 然后我可以简单地使用
require('./app/big-dir/foo')
与 Node.js 的 require 方法,据我所知这是不可能的......
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 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
. 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:
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…
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…
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 browserify 注册别名,以便覆盖您的重命名。
至于你的根绝对路径,那是不可能的。如前所述,modul8 有一个命名空间机制来解决这个问题。
我建议你在 freenode 上的 #stackvm 中 pong SubStack 并直接问他。
You can register aliases with browserify, so that covers your renaming.
As for your rooted absolute paths, that can't really be done. As mentioned
modul8
has a namespacing mechanism to solve this.I would recommend you pong SubStack in #stackvm on freenode and ask him directly.
它可能对您有帮助,也可能没有帮助,但我相信 Dojo Frameworks AMD Loader 的 API 与 RequireJS 兼容,并且只要您使用新的微内核就不会污染全局命名空间。
我相信现在全局命名空间中只有
require()
和define()
。无论如何,他们处理这个问题的方法是执行以下操作:
文档位于 http: //dojotoolkit.org/reference-guide/1.8/dojo/node.html
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.
I believe it only has
require()
anddefine()
in the global namespace now.Anyway their method of dealing with this is to do something like:
The documentation is at http://dojotoolkit.org/reference-guide/1.8/dojo/node.html
使用 uRequire ,它在 Nodejs
require
和 AMDdefine
之间提供了一座“桥梁” > 模块,无需重新发明轮子(它是建立在两个标准之上的)。它基本上将模块从AMD或commonJS格式转换为其他格式或在nodejs和amp上顺利运行的UMD ;浏览器。它还使用灵活路径约定转换依赖路径,因此您可以使用
'。 ./../foo'
或'bar/foo'
取决于您所处的位置更有意义。您的 AMD 或 UMD 模块在浏览器上异步加载(使用 AMD/requireJs 或其他 AMD 加载器),并在节点上异步加载
require(['dep1', 'dep2'], function(dep1,dep2){... })
也是模拟的。Use uRequire which provides a 'bridge' between nodejs
require
and AMDdefine
modules, without reinventing the wheel (it is build on top of the two standards). It basically converts modules from AMD or commonJS format to the other format or UMD that runs smoothly on both nodejs & the browser.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.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.