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

查看:127
本文介绍了Node.js require()vs RequireJS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HelloJS你好我可以像这样设置一个基本路径: 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 拥有它会更清洁:

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');

而不是:

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

现在你可以说为什么不改变名称而不是foo无处不在,让我们说'我们可以' t出于任何原因...

Now you could say why not change the name and not have foo everywhere, let's say that we can't for any reason…

我在node的requireJM中看到的另一个缺少功能是能够设置路径映射,如果我有一个名为<$ c的目录在RequireJS中$ c> ./ app / super-sized-directory-name / 我可以简单地执行'big-dir':'super-sized-directory-name'然后我可以简单地使用 require('./ app / big-dir / foo')使用Node.js的require方法这是不可能的据我所知...

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…

推荐答案

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

您可以使用browserify注册​​别名,以便覆盖您的重命名。

You can register aliases with browserify, so that covers your renaming.

至于您的根管绝对路径,这是无法真正完成的。如上所述 modul8 有一个命名空间机制来解决这个问题。

As for your rooted absolute paths, that can't really be done. As mentioned modul8 has a namespacing mechanism to solve this.

我建议你在freenode的#stackvm中使用SubStack并直接问他。

I would recommend you pong SubStack in #stackvm on freenode and ask him directly.

这篇关于Node.js require()vs RequireJS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆