0

I am using node.js to develop a service, I have been using es6, I am not quite sure if there is a need to use babel in this case to transpile the javascript es6 to es5 since the code is never executed from the browser directly.

Also one thing that was wondering me is when should I use babel to transpile nodejs es6 to es5? I tried to instal babel to do the transpiling just out of curiosity and running: npm run build I get the following error:

Error: EPERM: operation not permitted, scandir 'C:\Documents and Settings'

Mizlul
  • 1
  • 7
  • 41
  • 100
  • 2
    The error you're seeing is not directly related to transpiling, it's just matter of permissions. That said, you should transpile code only for support requirements. If your script is meant to be used on node.js starting from a specific version, you don't need to take care of transpiling your javascript code, since most environments will be able to execute it, if not all of them. – briosheje Apr 24 '19 at 10:29
  • @briosheje thanks for your reply and explaining the issue! – Mizlul Apr 24 '19 at 10:32
  • 1
    you may find interesting this link: https://node.green/#ES2015 . This will help you to decide which is the node version you need to have in order to make your code work. I think you can safely go with anything above `6.4.0` to ensure full support. (ECMAScript 2015 actually is ES6) – briosheje Apr 24 '19 at 10:34

2 Answers2

1

That error seems like a Windows problem, not JS problem.

Unless you want to support servers with Node below version, I think, 4.0, I see no point in transpiling.

mbojko
  • 13,503
  • 1
  • 16
  • 26
  • What could problem be with Windows in this case? Also how about if I would want to not just transpile to es5 but optimize and concatenate all in one file etc. – Mizlul Apr 24 '19 at 10:25
  • 1
    Re: EPERM, you can start here: https://stackoverflow.com/questions/34600932/npm-eperm-operation-not-permitted-on-windows – mbojko Apr 24 '19 at 10:28
  • @Mizlul I would suggest `webpack` to pack your code into a single file https://webpack.js.org/guides/getting-started/ – Kirill Apr 24 '19 at 11:38
0

Node supports ES6 syntax natively in version 6.4 and above. Generally stable LTS version (currently v10.15.3) will support most current ES version (refer to https://nodejs.org/).

For specific functionality you can refer to https://node.green/ to cross reference with your node version.

As for the error, it is an npm bug that was fixed in v5.6 (https://github.com/npm/npm/issues/17747).

You can update your npm using npm install npm@latest -g.

Kirill
  • 406
  • 3
  • 11
  • i updated npm to latest and run again npm run build, I still see same issue – Mizlul Apr 24 '19 at 10:56
  • @Mizlul Not sure why you're still trying to run babel. Update Node instead (if below v6.4.0) and remove babel altogether. Then run with `node index.js`. If that works add `node index.js` to `package.json` under `scripts -> start`. Then you can run with `npm start`. – Kirill Apr 24 '19 at 11:28
  • not sure why, just bc it somehow makes sense to transpile from es6 to es5 and by that you feel more confident that your code is executable from any environment any version. – Mizlul Apr 24 '19 at 11:50