Questions tagged [es6-modules]

The standard module system for JavaScript, introduced in ECMAScript 6 (2015).

1761 questions
41
votes
1 answer

What is the defined execution order of ES6 imports?

I've tried searching the internet for the execution order of imported modules. For instance, let's say I have the following code: import "one" import "two" console.log("three"); Where one.js and two.js are defined as follows: //…
Max
  • 13,084
  • 12
  • 69
  • 109
41
votes
3 answers

Are ES6 module imports hoisted?

I know that in the new ES6 module syntax, the JavaScript engine will not have to evaluate the code to know about all the imports/exports, it will only parse it and “know” what to load. This sounds like hoisting. Are the ES6 modules hoisted? And if…
gilamran
  • 5,914
  • 4
  • 27
  • 48
39
votes
3 answers

ts-jest does not recognize es6 imports

I'm adding typescript support to a react codebase, and while the app is working ok, jest tests are failing all over the place, apparently not recognizing something about es6 syntax. We're using ts-jest for this. Below is the error message I'm…
Ernesto
  • 3,307
  • 3
  • 31
  • 49
38
votes
6 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
  • 2,263
  • 1
  • 13
  • 24
37
votes
7 answers

How do I resolve eslint import/no-named-as-default

After looking at the documentation for the import/no-named-as-default eslint rule, I'm still confused about what exactly I'm doing wrong. I have the following file structure . ├── ButtonBack.css ├── ButtonBack.jsx ├── __tests__ │ └──…
mrbinky3000
  • 3,505
  • 8
  • 37
  • 52
35
votes
8 answers

How do I cache bust imported modules in es6?

ES6 modules allows us to create a single point of entry like so: // main.js import foo from 'foo'; foo() foo.js will be stored in the browser cache. This is desirable until I push a new…
spinners
  • 2,021
  • 1
  • 17
  • 31
34
votes
3 answers

Is there any way to mock private functions with Jest?

The ES6 module that I want to test looks as follows: function privateFunction() { ... } export function publicFunction() { ... does something ... privateFunction() ... does something else ... } I am using JEST for my unit tests and I am…
matteo
  • 1,375
  • 1
  • 12
  • 24
31
votes
5 answers

Import '.json' extension in ES6 Node.js throws an error

We're trying to use the new ways of exporting and imported 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
  • 9,743
  • 30
  • 88
  • 160
30
votes
2 answers

ES6: "import * as alias" vs "import alias"

Is there any difference between: import utils from 'utils' and import * as utils from 'utils'? In case A: //utils.js export function doSomething() { //... } In case B: //utils.js export function doSomething() { //... } export default function…
Philipp Munin
  • 5,149
  • 6
  • 32
  • 54
29
votes
2 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
  • 1,608
  • 1
  • 17
  • 25
28
votes
3 answers

Wait for document ready in ES6 modules

With hoisting, ES6 modules are loaded before the document is ready. For example, // module.js console.log('body', document.body); export let a = 1; // main.js import {a} from 'module' log body null in the console. How to have ES6 modules that use…
hhh
  • 1,481
  • 3
  • 24
  • 32
27
votes
5 answers

babel-jest doesn't handle ES6 within modules

I am trying to set up Jest on a React based project which uses ES6 modules. However I seem to be having issues with ES6 modules, I am using babel-jest and believe I have this set up properly (Jest detects it automatically). Jest doesn't seem to have…
jamiedust
  • 466
  • 1
  • 4
  • 13
26
votes
2 answers

webpack imported module is not a constructor

I created a small JS module which I intend to make an npm package, but for now is just on GitHub. This module is written in ES6 and SCSS, and is thus relying on webpack and babel for transpilation. To test it, I created a separate project with a…
Liran H
  • 5,181
  • 4
  • 30
  • 39
25
votes
0 answers

Is there a way to use es6 modules to import html template into javascript?

There seems to be many dated questions on this topic, I cant find one more recent (2018). Also other questions just focus on getting this to work, I am focused on the right path forward for making this work following the path of ES6 Modules with…
Joelio
  • 4,402
  • 6
  • 36
  • 74
25
votes
3 answers

Does ES6 import/export need ".js" extension?

I installed chrome beta - Version 60.0.3112.24 (Official Build) beta (64-bit) In chrome://flags/ I enabled 'Experimental Web Platform features' (see https://jakearchibald.com/2017/es-modules-in-browsers) I then tried: