This question already has answers here:
ES2015 "import" not working in node v6.0.0 with with --harmony_modules option
(4 answers)
Closed 6 years ago.
I'm wanting to start using this ES6 on Node.js but I can't make it work. I'm adding the node test.js --harmony flag but I'm still getting bad syntax errors.
It doesn't even pass the first lines of code:
import env from 'node-env-file'
import api from '../src'
I'm getting this
(function (exports, require, module, __filename, __dirname) { import env from 'node-env-file'
^^^^^^
SyntaxError: Unexpected token import
How can I make my Node.js work with this type of syntax.
import keyword is not yet supported by any version of node because there is no JavaScript engine yet that natively supports ES6 modules.
You have to use somekind of ES6 to ES5 transpiler like Babel or stick to require.
Related
This question already has answers here:
ESM importing all files from folder with index.js not working
(2 answers)
Closed 2 years ago.
I'm using node.js v14.8.0 and ES6 style module imports.
My code looks like this:
import myModule from "./my-module";
Expected behaviour:
The file ./my-module/index.js is imported.
Actual behaviour:
Node.js throws the ERR_UNSUPPORTED_DIR_IMPORT error.
Question:
How can I configure node.js to allow directory imports and resolve them using the index.js file?
In short: "vanilla" node.js doesn't support this.
We decided to use babel, which allows for this notation.
This question already has answers here:
"Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6
(31 answers)
Closed 2 years ago.
I'm using the latest version of node.js and Chrome.
Node.js still relies on CommonJS to do modules (exports and imports).
The CommonJS page that was recommended to me actually says CommonJS is not suitable for client-side and to use ES modules instead. This makes me think I should use them for my websites that I'm creating. Is it possible to do this?
The errors I get when I try to are 'Uncaught SyntaxError: Unexpected token 'export' & unexpected token export' & 'Uncaught SyntaxError: Cannot use import statement outside a module.'
Here are the errors I get when I include type="module":
Write <script type="module"> instead of <script>.
This question already has answers here:
Pass options to ES6 module imports
(9 answers)
Closed 5 years ago.
In CommonJS one can run:
const module = require('module')(API_KEY)
In the various ways TC39 / 'ES6' modules (inverted commas as there are are more modules writted in ES6 using CommonJS than this syntax), how can I do the same?
It looks like this isn't possible, ie my only option is:
import moduleSetup from "module";
const module = moduleSetup(API_KEY);
ie my only option is
Correct, at least for now.
It may be that the dynamic import proposal will let you do something like the require call, but it's at Stage 3 right now. (Of course, if you use them for this, the import won't be statically-analyzable anymore...)
This question already has answers here:
Using Node.js require vs. ES6 import/export
(11 answers)
Closed 6 years ago.
Right now I'm importing modules into node project as
import * as name from "moduleName";
Im not doing it as
var name = require("moduleName");
as we used to be in the node project, earlier
my question is there difference in the writing a module when we import using require or import, are modules written internally are same just we are importing in different way or its something internal that forces us to use require or import when importing
and what is the difference between require and import(es6)
Thanks!
import runs at the beginning of the file and it would be already
loaded before the code itself runs.
require on the other hand is run
inline and can be inserted within the code conditionally.
As far as I know node 5 supports ES2015, but when I try to run something like
import sizeOf from 'image-size';
I get
$> node -v
v5.9.0
$> node test.js
/Users/dev/tmp/test.js:1
(function (exports, require, module, __filename, __dirname) { import sizeOf from 'image-size';
^^^^^^
SyntaxError: Unexpected token import
...
Now, when I search google, I find suggestions using babel
(using a .babelrc with an es2015 preset), but if node5 supports ES2015, why do I need babel ?
if node5 supports ES2015, why do I need babel
Node doesn't support every feature of ES2015 yet. For the unsupported features you might want to use Babel, or simply not use the feature.