It looks like you are conflating a bunch of different uses of RequireJS:
-
How can you use a RequireJS installed through Node in the browser?
You can just install it with
npm install requirejs, and then you have your HTML file have ascriptelement that points tonode_modules/requirejs/require.js. Exactly as you show in your code snippet. That’s all there is to it. This being said, I don’t like to havenode_modulesin what I deploy, so I typically have my build process copyrequire.jselsewhere. -
How can you load npm-installed modules with RequireJS in Node?
Suppose without RequireJS you would load the module
fooby doingrequire('foo'). You install RequireJS and load it asrequirejs. How do you loadfoousing RequireJS? You can just dorequirejs('foo'). So long as RequireJS does not find it through its own configuration, it will issue as a last resort a call to Node’s ownrequire, and will load it this way? Here’s an illustration. Install RequireJS withnpm install requirejs. Create this file:var requirejs = require("requirejs"); var fs = requirejs("fs"); console.log(fs);Then run it. You’ll get on the console Node’s
fsmodule. -
How can you load npm-installed modules with RequireJS in a browser?
It depends on the modules. RequireJS does not contain code that will magically make a npm-installed module work in the browser. It ultimately depends on how the modules are structured. Some cases:
A. Some npm-installed modules can be loaded with RequireJS without modification. There’s one library I’ve authored which is distributed through npm and yet is a collection of AMD modules. It is trivial to load them with RequireJS in the browser.
B. It may require being wrapped in
definecalls. I’ve recently loadedmerge-optionsin one of my projects withgulp-wrap-amd.merge-optionsis a CommonJS module. It does not support RequireJS out-of-the-box but if you wrap it with adefinecall, it will work.C. It may require something more complex before it will be loaded in a browser. For instance, if a module relies on Node’s
fsmodule, you’ll have to provide a replacement forfsthat runs in a browser. It will presumably present a fake filesystem to your code.