3

I'm a Node n00b starting a couple web app projects using Express, and I've got some common client-side libraries I'd like to share between the two projects. This seems like a very common problem, so there must be several solutions available already.

I come from a java background, and in java, I'd create a separate "common" project and "overlay" common WAR over my project during packaging. This would also allow for r.js optimization during the build process.

My best guess in Node is that I need to create a private NPM module, and map those common files into express via a use() middleware plugin. Is that right?

How, then, can I package both my common and project specific javascript into a minified file using r.js?

Or is source control the answer? Checking out my "common" repository inside each project?

Any help would be most appreciated. Thanks.

Community
  • 1
  • 1
Erik R.
  • 7,152
  • 1
  • 29
  • 39
  • You talk about "client-side" javascript, but then also talk about using express middleware, which is server side. Which is it? – Peter Lyons Oct 18 '13 at 16:35
  • The middleware was to bind the js file in node_modules to a "/js/mycode.js" route in express. Perhaps I was completely offbase... – Erik R. Oct 18 '13 at 17:29
  • You can just use a symlink for that and the static middleware, or even point an instance of the static middleware at node_modules/my_browser_code – Peter Lyons Oct 18 '13 at 17:59
  • Yeah, I know. I was hoping for a more elegant solution than a symlink. – Erik R. Oct 18 '13 at 18:45
  • `Or is source control the answer? Checking out my "common" repository inside each project?` I'd go with this. There will never be something similar to WAR packaging in node just as other languages such as Ruby or Python or Perl. The primary difference is that Java is compiled while all the other languages execute directly in source form. You will note that even Java doesn't have this sort of management for .java files. Just like Java or Perl or Ruby you manage source files with source control tools. – slebetman Oct 18 '13 at 23:02

1 Answers1

3

This seems like a very common problem, so there must be several solutions available already.

Good news: Yes, this is a common problem. Yes, there are several "solutions".

Bad News: All of the "solutions" are at least partially terrible.

Here's my advice:

1) Individual .js files should be coded as CommonJS modules

2) Groups of related .js files should be made into npm packages

3A) Use them in node via the regular node.js/CommonJS require function and use browserify to use them in the browser

3B) OR use a built tool like grunt to wrap commonjs into AMD format for use with requireJS in the browser

3C) OR consider something like component.io components

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • Thanks. It seems that I may have had unreasonable expectations about the maturity of Node and Express. – Erik R. Oct 18 '13 at 18:46
  • Well, I think you are doing some sloppy thinking there. The fundamental problem is that JavaScript itself at the basic language level has no module system and they just in ECMAScript 6 added one. It's a javascript thing that if you are building a web application will effect you regardless of your server side stack. This applies equally as well to Ruby on Rails for example, which is why they have a gigantic, complex beast called the Asset Pipeline to tackle it. – Peter Lyons Oct 18 '13 at 18:51
  • I *sort of* agree with that assessment. Node has put together a very impressive modularization of javascript, albeit only for the server side. require.js is trying very hard on the client side. It doesn't seem too implausible for a solution to emerge to combine npm and require.js somehow to allow a webapp to specify that it requires version X, Y and Z of libraries A, B and C, and for some build process to go fetch those AND minify them with the app-specific code. It just hasn't happened yet, apparently. – Erik R. Oct 18 '13 at 20:19
  • That's exactly what browserify does. But again, every approach I've tried is partly amazing and partly horrific. No clear win at this moment. – Peter Lyons Oct 18 '13 at 20:26
  • Thanks for your help. You've helped a lot. – Erik R. Oct 18 '13 at 20:41