The standard module system for JavaScript, introduced in ECMAScript 6 (2015).
Questions tagged [es6-modules]
2668 questions
128
votes
2 answers
using brackets with javascript import syntax
I came across a javascript library that uses the following syntax to import libraries:
import React, { Component, PropTypes } from 'react';
What is the difference between the above method and the following?
import React, Component, PropTypes from…

fox
- 15,428
- 20
- 55
- 85
124
votes
3 answers
Does ES6 module importing execute the code inside the imported file?
Does the code inside the js file gets run during the import? if yes, then once or each time?
e.g.
// a.js
console.log("A");
const a = "a";
export default a;
// b.js
import a from "./a"; // => console logs?
// c.js
import a from "./a"; // =>…

mbehzad
- 3,758
- 3
- 22
- 29
112
votes
5 answers
Import '.json' extension in ES6 Node.js throws an error
We're trying to use the new ways of exporting and importing modules for ES6 with Node.js. It's important for us to get the version number from the package.json file. The following code should do that:
import {name, version} from…

DarkLite1
- 13,637
- 40
- 117
- 214
111
votes
10 answers
(node:9374) Warning: To load an ES module, set "type": "module"
I just started to learn React today.
How do I get rid of that error message on my Console in the Terminal in Visual Studio.
(node: 9374)Warning: To load an ES module,
set "type": "module" in the package.json or use the .mjs extension.…

Nisha_UX
- 1,213
- 2
- 5
- 5
108
votes
8 answers
Why and when to use default export over named exports in es6 Modules?
I have referred all the questions in stackoverflow.
But none of the suggested why and when to use default export.
I just saw that default can be metioned "When there is only one export in a file"
Any other reason for using default export in es6…

bvakiti
- 3,243
- 4
- 17
- 26
102
votes
6 answers
ES6: import module from URL
Is it possible to import javascript module from external url in ES6?
I tried (using babel-node):
import mymodule from 'http://...mysite.../myscript.js';
// Error: Cannot find module 'http://...mysite.../myscript.js'

madox2
- 49,493
- 17
- 99
- 99
97
votes
4 answers
ES6 Modules: Undefined onclick function after import
I am testing ES6 Modules and want to let the user access some imported functions using onclick:
test.html:
Module Test

Konrad Höffner
- 11,100
- 16
- 60
- 118
97
votes
4 answers
What does "... resolves to a non-module entity and cannot be imported using this construct" mean?
I have some TypeScript files:
MyClass.ts
class MyClass {
constructor() {
}
}
export = MyClass;
MyFunc.ts
function fn() { return 0; }
export = fn;
MyConsumer.ts
import * as MC from './MyClass';
import * as fn from './MyFunc';
fn();
This gives…

Ryan Cavanaugh
- 209,514
- 56
- 272
- 235
96
votes
9 answers
How can I unit test non-exported functions?
In a JavaScript ES6-module, there may be many, small, easy-to-test functions that should be tested, but shouldn't be exported. How do I test functions in a module without exporting them? (without using Rewire).

Jordan
- 3,813
- 4
- 24
- 33
92
votes
5 answers
ES6 modules in the browser: Uncaught SyntaxError: Unexpected token import
I'm new to ES6 (ECMAScript 6), and I'd like to use its module system in the browser. I read ES6 is supported by Firefox and Chrome, but I'm getting the following error using export
Uncaught SyntaxError: Unexpected token import
I have a test.html…

cdarwin
- 4,141
- 9
- 42
- 66
91
votes
2 answers
Destructuring a default export object
Can I destructure a default export object on import?
Given the following export syntax (export default)
const foo = ...
function bar() { ... }
export default { foo, bar };
is the following import syntax valid JS?
import { foo, bar } from…

sfletche
- 47,248
- 30
- 103
- 119
85
votes
3 answers
ES6 Destructuring and Module imports
I was under the impression that this syntax:
import Router from 'react-router';
var {Link} = Router;
has the same final result as this:
import {Link} from 'react-router';
Can someone explain what the difference is?
(I originally thought it was a…

Guy
- 65,082
- 97
- 254
- 325
83
votes
7 answers
'Directory import is not supported resolving ES modules' with Node.js
I'm using Node.js v14.13.0.
app.js file:
import database from './database';
database();
database/index.js file:
import mongoose from 'mongoose';
export default connect = async () => {
try {
await mongoose.connect('...', { });
}…

Ayoub k
- 7,788
- 9
- 34
- 57
80
votes
4 answers
ES2015 "import" not working in node v6.0.0 with with --harmony_modules option
I am using node v6.0.0 and wanted to use ES2016 (ES6). However I realized that the "import" syntax is not working. Isn't "import" fundamental to for writing modular code in ES2015? I tried running node with --harmony_modules option as well but still…

joy
- 3,669
- 7
- 38
- 73
79
votes
3 answers
What’s the purpose of the HTML “nomodule” attribute for script elements if the default is text/javascript?
I am not clearly understanding why the nomodule attribute exists in the new browsers that support ES6 modules.
In HTML 5, the type attribute is optional and defaults to text/javascript:
The type attribute gives the language of the script or format…

dman
- 10,406
- 18
- 102
- 201