Skip to content

Instantly share code, notes, and snippets.

@rmurphey
Last active August 29, 2015 14:15
Show Gist options
  • Save rmurphey/75bab312f52e5d948ba7 to your computer and use it in GitHub Desktop.
Save rmurphey/75bab312f52e5d948ba7 to your computer and use it in GitHub Desktop.
How to share modules across RequireJS, CommonJS web apps?

Earlier today I tweeted:

I want to use node modules in my existing requirejs browser app. What’s the right path?

This was really understating the problem I'm trying to solve. I need a way to share modules across various JS (browser) applications. Some of these applications are RequireJS apps, and changing them away from that is probably not on the table. Others are newer applications where we have a choice when it comes to the build system -- common choices of late seem to be browserify and webpack.

I want to be able to maintain a set of modules separate from these projects that can be used by these projects. The requirements, then, are:

  • Modules must be usable by a CommonJS-based module/build system, such as Browserify or Webpack.
  • Modules must be usable by an AMD-based module/build system, such as RequireJS.
  • Modules must be installable, update-able, and version-able; that is, the modules should be not need to be modified in any way by the host application.
  • Modules must be able to share dependences; that is, if two modules require the same version of lodash, then an application that requires them should not end up with two lodashes in its build.
  • We can make modifications to the RequireJS apps short of completely changing the module/build system.
  • Modules should work just fine for Node apps, too, where appropriate.
@thomaswilburn
Copy link

Shouldn't UMD work? If you build your modules with browserify --standalone, it should generate a UMD-compatible module, which could then be loaded with either AMD or another module system.

EDIT: Or, you know, what jrburke said. Nice comment!

@rmurphey
Copy link
Author

@thomaswilburn that doesn't solve for the fourth point, as far as I can tell?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment