How to use NodeJS global module objects in Require

2019-06-17 02:40发布

问题:

I decided to use RequireJS as my module loader in NodeJS.

Unfortunately, the modules I define don't have access to some of the "global" objects that would be be available if the modules were loaded using the CommonJS method. This includes the objects that are located in the module scope, such as __dirname and __filename.

Is there a relatively simple approach to using these objects in the RequireJS modules?

回答1:

This is covered on the requirejs issues site on github (https://github.com/jrburke/requirejs/issues/89) and the workaround they seem to suggest is to use module.uri instead.