Using ES6 in Nodejs without transpiling code - node.js

I am using Nodejs v9.0.0. I have to use Babel for transpiling the code into ES5 because if not transpiled it throws error unexpected token import. So If anyone know how to use the real ES6 version in nodejs and which nodejs version. Please let me know.
Thanks

All es6 is supported by NodeJS since 8.10 version : look at the column NodeJS in the es6 compatibility array.
Unforfunately the ES6 Module syntax is not supported or only with the flag experimental.
So you need to convert your ESModule in CJSModule. You can do it with the babel-plugin-transform-es2015-modules-commonjs plugin
In your .babelrc file :
"plugins": [
"transform-es2015-modules-commonjs"
]
If you use babel-register the transformation occurs when the file is required(imported)

Related

CommonJS require() or ES6 import/export in nodejs production?

I have got the latest version of npm modules and developing a nodejs application. The problem is that some npm modules support the require() and the others support the import/export statements, I cannot use them both in a file.
Having the production criteria in mind, which should I opt for either only require() or only import/export or a mix of those using the dynamic import() along with the require(). Thanks
I assume we are talking about plain JavaScript without transpiling from TypeScript or Babel.
The require() function is used to load CommonJS modules whereas the import statement is used to load ESM modules. Which of them you should or can use depends on factors such as:
does your target Node.js version support ESM? I think ESM in Nodejs support got introduced in v12, but I'm not sure how stable it is.
is your package defined as ESM or CommonJS module. That depends on the type field in your package.json, with "type": "module" for ESM and "type": "commonjs" for CommonJS: https://nodejs.org/api/packages.html#type
are your package dependencies provided as ESM or CommonJS modules. That depends on each individual package.json and their type field as well as if the package is provided as both ESM and CommonJS modules with conditional exports: https://nodejs.org/api/packages.html#conditional-exports
Unfortunately, I cannot provide a definite answer for you because that depends on your environment. ESM and import statements are going to be the future of JS, but the ESM support in the Node.js ecosystem is not on par with CommonJS yet.
With this in mind, I would opt for CommonJS modules but I would use a transpiler like TypeScript so I can still use modern features of JS.

Node.js: How to use npm ES6 modules (import) in commonjs (require) app.js

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.

when modules: false set by babel, then how can nodejs parse the import es6 module statement?

As I know, nodejs can only understand require syntax, not the import/export statement.
So When I want to use es6 module statement in nodejs, I have to configure babel and transpile those es6 module statement into the require syntax so that nodejs can understand what to do.
https://github.com/kentcdodds/jest-cypress-react-babel-webpack/tree/tjs/jest-00
But seeing this repo, the author is using module:false with the babel configure (see .babelrc file)
As I know, module:false option prevents the es6 module statement from being transpiled.
Nodejs still cannot understand import/export statement, but how does that work without transpiling..? Any advice would be appreciated!

Importing modules in Angular2

As per understanding , Node js uses CommonJS module pattern , and in CommonJS
pattern we use require() to import a node module.
In Angular 2 application development we use #angular/core , #angular/common etc
node modules.
My question is:
Why do we use "import {} from '#angular/core'"[which is ES6 module syntax]
instead of commonJS require() syntax for accessing node modules
in angular2 code files.
The reason for this, is that Angular2 is written in TypeScript.
TypeScript is a superset of ES2015, and wants to be as close to ES2015 suggested syntax as possible. That's why you use the ES2015 import {} from syntax.
However, TypeScript also comes with a built-in transpiler (tsc). Meaning you write TypeScript code, but target a specific EcmaScript version in your tsconfig.json
When targetting ES5 and looking into the transpiled code, you will
clearly see that behind the scenes TypeScript will translate import {} from to require()in the transpiled files.
When targetting ES6, of course in the transpiled code, your imports will be ES2015 imports. Be aware that when targetting ES6, you will need babel to transpile your ES6 modules to ES5 or use System.js to load your ES6 modules in the browser.
Cheers

How to get support of generators in typescript without setting target to ES6?

Got a situation here.
I use nodejs with --harmony flag to get support of generators.
Next i'm trying to switch my project to TypeScript and get a problem: in "target":"ES6" mode it transpiles import commands as is (instead of require).
And node with --harmony flag doesn't support that:
import * as fs from 'fs';
^^^^^^
SyntaxError: Unexpected reserved word
Transpiling option "module":"commonjs isn't allowed with "target":"ES6".
Have anyone solved this problem without using any external require/import utilities?
These settings have worked for me:
tsconfig.json
{
"compilerOptions": {
"target":"ES6",
"moduleResolution": "classic",
}
}
ES6 support for generators
No import stuff transpiling due to
"moduleResolution": "classic"
And so the problem's gone!
As you can see in the TypeScript roadmap (Version 1.7) one of the current issues is "Support --module with --target es6".
I'm afraid your are going to need a temporal solution until TypeScript 1.7 is released. Maybe Polyfill for the ES6 Module Loader or SystemJS?
Another way to get all i want is a build stack:
Transpile TS to ES6
Transpile es6-js to es5-js with Babel

Resources