Node.js require () vs RequireJS?

Hello, RequireJS. I can set the base path as follows: base : './app/' , for example, when I'm in ./app/foo/bar/ , and I have a script where I use require('foo'); RequireJS will then search for ./app/foo.js and not in the node_module folder or in ./app/foo/bar/foo.js , this is convenient if you have a structure where it would be much easier for you, as developer, see dependencies instead of ../../foo.js . I could have ./app/foo.js and ./app/foo/foo.js and ./app/foo/bar/foo.js would be much cleaner:

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

but not:

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

Now you could say, why not change the name and hurt foo, let's say that we cannot for some reason ...

Another drawback of the function that I see in node requires a method against RequireJS is the ability to set path mapping, if I have a directory named ./app/super-sized-directory-name/ in RequireJS, I could just do 'big-dir' : 'super-sized-directory-name' and then I could just use require('./app/big-dir/foo') with Node.js requires a method, this is not possible as far as I know. ..

+6
source share
3 answers
 --alias, -a Register an alias with a colon separator: "to:from" Example: --alias 'jquery:jquery-browserify' 

You can register aliases using a browser to cover your renaming.

As for your root absolute paths, this is impossible to do. As mentioned, modul8 has a spatial mechanism to solve this problem.

I would recommend you SubStack Pong to #stackvm on freenode and ask it directly.

+2
source

This may or may not help you, but I believe that Dojo Frameworks AMD Loader is an API compatible with RequireJS, and giving you a new microkernel does not pollute the global namespace.

I believe that it now has require() and define() in the global namespace.

In any case, 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

0
source

Use uRequire , which provides a bridge between nodejs require and AMD define nodes without reinventing the wheel (this builds on top of two standards). It basically converts modules from the AMD or commonJS format to another format or UMD , which works smoothly on both nodes and the browser.

It also translates dependency paths with flexible traffic conventions , so you can have either '../../foo' or 'bar/foo' , depending on what makes more sense at the point you are at.

Your AMD or UMD modules load asynchronously in the browser (using AMD / requireJs or another AMD loader), and node also simulates asynchronous require(['dep1', 'dep2'], function(dep1,dep2){...}) .

0
source

Source: https://habr.com/ru/post/906513/


All Articles