I use NodeJS 0.12.7 version.
I have a question related launch NodeJS server with ES6 feature.
I need to transpile node_modules packages from ES6 to ES5 to run NodeJS.
How can I do it?
You can use webpack to transpile the code to commonJs which is understandable by node. Here is a webpack config link - https://github.com/ujjwalguptaofficial/fortjs-javascript-starter/blob/master/webpack.config.js
You can use fortjs starter template which provides the setup ready to use. Take a look at this article of creating rest api in es6 - https://medium.com/fortjs/rest-api-in-nodejs-using-es6-227765440b2b
Related
I'm kind of a newbie with node.js, so please, be merciful :)
I use node ver 14.15.1
The problem I'm facing is to get a npm module("ghost-cursor", https://www.npmjs.com/package/ghost-cursor) that is written in ES6 ("import") working in commonjs (require).
I need to pack my node.js app in an exe file and the npm "pkg" doesn't seem to work with ES6. With nexe I can't get to pack all dependencies in order to work outside of the project folder and on other machines... I have tried to transpile the ES6 module to commonjs with babel:
babel --presets=es2015 ./folder/filename.js > ./folder/filenameCJ.js
but I get the error: SyntaxError: Invalid or unexpected token
I have also tried the solutions posted in:
How to use ES6 modules in CommonJS?
but I can't get it to work.
Are there any other ways, except "nexe" and "pkg" to make an exe file from node.js?
Is it possible to use an ES6 module from npm in commonjs with require? If so, how?
Thanky you for your help and replies.
I am trying to convert Express.js project to binary, and I am using zeit/pkg libary. However, it gave me an error every time I try to use pkg.
Is there a way that I can force Node/Npm to use es5 instead of es6.
in your snapshot the module is being imported from ..er/node_modules/safefs/es6/lib/safefs.js
notice it is importing from es6 which has export keyword. that means its an es6 module and your node supports 'commonjs' require.
which is why this require may be failing.
you can download latest version of node where you can resolve es6 modules.
I'm using WebStorm and a newbie to it. when I use import/export statements, it gives me an error of
Unexpected token import
but if I try with require/module.exports it works fine.
N.B- I've configured language version as ES6 from Languages and Frameworks.
This is not WebStorm but Node.js that fails. While import is a part of ES6, native support for ES6 modules in Node.js is very limited and requires special setup - see https://nodejs.org/api/esm.html#esm_enabling. So, you have to compile your code with Babel first. Usually transpiling is a part of build process (using Gulp, Grunt, WebPack, etc.). Or, you can transpile your code on-the-fly by passing -r babel-register to Node.js. Of course, you need creating appropriate .babelrc and install the required modules (npm install --save-dev babel-cli babel-preset-env)
Can someone help me run a node.js file that has ES6 features? This is my current workflow:
Save change in file
npm run build --- builds file to my src/dist folder
cd into my dist directory
node
var file = require('./index.js')
file.someMethod()
Then when I make a change to my method, I need to repeat all the steps above again.
Does anyone know of a more efficient way to do this?
Thanks in advance!
You dont need anything if you are not using import/export. Just Install node 8.x and then run node my_file.js. Nodejs supports most of the es6 features out of the box.
If you want to use import/export then I would recommend to use typescript: https://www.typescriptlang.org/. It has compiler TS (statically typed ES6) to ES5.
Oh, and of course both babel and typescript have watcher modules (auto recompiling on file change).
It really depends on what npm run build actually does in your app. Does it run gulp/grunt? Webpack? Etc. If it's webpack there is a webpack-dev-server that you should look into.
If it's grunt or gulp then you have a custom build chain that you're working with. If all you need is to support es6 features without having to build the project then you could use something simple like babel-cli and run your file from outside the dist directory.
$ npm i -g babel-cli
$ babel-node
> var file = require('./index.js');
> file.someMethod()
By running babel-node you're running the normal node repl but with babel required. Es6 features should now be supported in the repl without needing to be compiled to es5.
You may also want to try simply updating to the latest version of node because node 8 has pretty extensive es6 support.
I think i'm missing something with the typescript 2 type system when used with nodejs.
Here is the situation:
Compiling a small nodejs express server written in typescript to plain es5 in order to be run under node 6.10.0 (target: es5 in tsconfig.json).
In my package.json, i installed #types/node (7.0.3) to get node's type informations.
When installing my project using npm (v3.10.10) and then compiling it with typescript (v2.1.5) i get a bunch or errors related to 'Iterable' and 'Iterator' symbol, (which are es6 symbols).
So from what i understand, #types/node use es6 types out of the box, assuming they are already availables.
In order to have the es6 types (Iterator, Iterable and so on) it seams that there is two solutions:
Add the core-js package.
Target es6 instead of es5 in tsconfig.json which will force typescript to use it's lib/lib.es2015..d.ts* definitions files.
In my opinion, the second approach is better.
Is this the official way to go when compiling ts to node ?
ie: targeting es6 in tsconfig ?
How can i be sure that the underlying node engine will effectively support those es6 features ? What if i'm using an old nodejs version without es6 support ?
I'd be glad to discuss to clearly understand the underlying mechanisms !
Thank you
Since node doesn't support completely all es6 features yet see : node green,
it's better to target es5 and install core-js types :
npm install #types/core-js