0

I'm writing some Javascript which runs on node, and makes use of modules in node_modules. Some of these happen to be in ES6.

Unfortunately I've learned that my JS will be deployed on node v5. So I transpiled it to ES5 with a quick babel src-dir --out-dir out-dir

But of course when I run the code in node, it barfs as soon as the dependencies lead to any of the ES6 files in node_modules.

Node v6+ runs without a hitch, but I have no control over the deployment environment.

How should I deal with this -- should I be thinking about transpiling all node modules too? That seems like a lot of work. Am I missing something obvious here?

.babelrc is like this:

{
    "presets": [
        [ "es2015" ],
    ],
    "plugins": []
}
ukosteopath
  • 443
  • 7
  • 14
  • Are the `node_modules` public ones? It's generally bad form to public things to NPM that don't work on Node. – loganfsmyth Jul 29 '17 at 01:10
  • Yep all public. They all work with node v6+, but I'll be deploying to node v5, and some of the modules will not work with node v5. – ukosteopath Jul 29 '17 at 01:14
  • Isn't there a way you could get node 6 on production? You don't need to replace the old node runetimes, you can add different node runtimes to your server. – MinusFour Jul 29 '17 at 01:17
  • There really isn't, unfortunately. It's a cloud service. – ukosteopath Jul 29 '17 at 01:18
  • 2
    Oh you mean there is ES6 code in your `node_modules`, not that they are using ES6 module syntax like `import` and `export`. Given your requirements you'd either have to use `babel-register`, which isn't recommended for production, or you'd have to only depend on things that work on Node 5. – loganfsmyth Jul 29 '17 at 01:30
  • Node 5 is a really bad version to target these days. What kind of cloud service would support an unstable version and not continue updating it?? – Ry- Jul 29 '17 at 01:46
  • @Ryan - This sounds like a really bad cloud service to deploy node.js apps on. – jfriend00 Jul 29 '17 at 01:50

1 Answers1

1

If you're relying on npm modules that don't work in your environment, there are only four choices I can think of:

  1. Switch to modules that do work in your own environment that cover the same functionality.
  2. Fix the modules in question by modifying them to work in the target environment
  3. Ditch the modules entirely and write the necessary code yourself
  4. Find an earlier version of the module (via Github) that was compatible with a version of node.js before v6. Unless this is something first released in the last year, there are probably earlier versions that worked with earlier versions of node.js.

Fix could involve fixing a bug or two or it could involve transpiling them yourself to an ES5 target - depending upon what the issues are.

There's probably a combination of 2) and 3) that has you borrowing just what you absolutely need from the module and fixing it for node v5 and adding it to your project or forking it on Github. NPM modules are generally open source which gives you "fix it yourself" options.

Note: If you showed a precise example of exactly what the problem was running something under node v5, we could help more specifically with which option might be simplest. But, without any detail like that, all we can really do is describe the overall options for you to choose from.


Oh, and I'd hammer on the cloud service to really let them know about this loud and clear. 5.x has NO long term support from the node.js foundation and never did. It simply should NOT be a deployment version as odd versions are always experimental and never get long term support. 6.x is the current active LTS (long term support) version of node.js released 9 months ago. Even v4.x is under maintenance until April 2018. If your hosting provider is only offering 5.x and not letting you run your own version or pick a supported and more modern version, they are simply not a very good hosting provider for node apps.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • How about a combination of tools like rollup.js and babel.io in my dev environment to transpile all necessary files (mine + modules) into a bundle that will work on node v5? Is that asking for more trouble than it's worth? – ukosteopath Jul 29 '17 at 02:15
  • @ukosteopath - You haven't told us anything about what the actual issues are. For all I know, the issues have to do with API changes or new features in node v6+ and transpiling wouldn't help in the least. Personally, I wouldn't make life with your own code any more complicated than needed. I'd probably treat each module separately rather than create a bundle which isn't really needed for server-side code. – jfriend00 Jul 29 '17 at 02:48
  • It's pretty tricky to explain in detail without pasting 20 lengthy files here. I've summarised as best I can. Thanks for your advice, I'll explore the options you've mentioned. – ukosteopath Jul 29 '17 at 02:55
  • @ukosteopath - Does this answer your question? If so, you can indicate that to the community by clicking the green checkmark to the left of the answer and that will also earn you some reputation points. – jfriend00 Aug 18 '17 at 06:11