Node.js require() vs RequireJS?
-
07-11-2019 - |
Frage
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…
Keine korrekte Lösung