Search code examples
javascriptnode.jsecmascript-6babeljsbrowserify

Convert Node.js files to ES5


I have node.js script files which I am trying to re-use inside an ASP.NET application on IE 11.

I follow below steps to use them on IE 11:

  1. Create a bundle file using browserify:
browserify Module.js --standalone mymodule -o bundle.js
  1. Convert the ES6 version of bundle.js to ES5 by manually converting it using https://babeljs.io/repl.

  2. Save the converted ES5 script and include the saved .js file as in ASP.NET application.

Is there anyway I can automate step 2? Is there any better way to convert Node.js files to ES5?


Solution

  • Since you use Browserify, you can use Babelify which is a Browserify transform:

    npm install --save-dev babelify @babel/core @babel/preset-env
    
    browserify Module.js --standalone mymodule -o bundle.js -t [ babelify --presets [ @babel/preset-env ] ]
    

    See the babel-preset-env documentation to see how to define your target ("ie": 11), by default all ES2015+ syntax will be transformed.