Questions tagged [es6-modules]

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

1761 questions
538
votes
1 answer

How can I alias a default import in JavaScript?

Using ES6 modules, I know I can alias a named import: import { foo as bar } from 'my-module'; And I know I can import a default import: import defaultMember from 'my-module'; I'd like to alias a default import and I had thought the following would…
sfletche
  • 36,606
  • 25
  • 86
  • 108
441
votes
11 answers

How can I use an ES6 import in Node.js?

I'm trying to get the hang of ES6 imports in Node.js and am trying to use the syntax provided in this example: Cheatsheet Link I'm looking through the support table, but I was not able to find what version supports the new import statements (I tried…
Jonathan002
  • 7,041
  • 4
  • 23
  • 46
290
votes
13 answers

Is it possible to import modules from all files in a directory, using a wildcard?

With ES6, I can import several exports from a file like this: import {ThingA, ThingB, ThingC} from 'lib/things'; However, I like the organization of having one module per file. I end up with imports like this: import ThingA from…
Joe Frambach
  • 25,568
  • 9
  • 65
  • 95
283
votes
1 answer

Node.js plans to support import/export ES6 (ECMAScript 2015) modules

I've been looking all over the Internet without a clear answer for this. Currently Node.js uses only CommonJS syntax to load modules, and if you really want to use the standard ECMAScript 2015 modules syntax, you either have to transpile it…
Zorgatone
  • 3,903
  • 4
  • 25
  • 45
241
votes
7 answers

`export const` vs. `export default` in ES6

I am trying to determine if there are any big differences between these two, other than being able to import with export default by just doing: import myItem from 'myItem'; And using export const I can do: import { myItem } from 'myItem'; Are…
ajmajmajma
  • 11,766
  • 18
  • 67
  • 124
239
votes
10 answers

What is difference between Axios and Fetch?

I am calling the web service by using fetch but the same I can do with the help of axios. So now I am confused. Should I go for either axios or fetch?
Gorakh Nath
  • 6,352
  • 11
  • 33
  • 58
209
votes
1 answer

Not recommended to use "use strict" in ES6?

I'm not familiar with ECMAScript 6 yet. I've just cloned the React Starter Kit repo, which uses ES6 for application code. I was surprised to see that the linter is configured to forbid occurences of the use strict directive, which I thought was…
Midiparse
  • 4,441
  • 4
  • 25
  • 44
160
votes
2 answers

Javascript ES6 export const vs export let

Let's say I have a variable that I want to export. What's the difference between export const a = 1; vs export let a = 1; I understand the difference between const and let, but when you export them, what are the differences?
Cheng
  • 13,014
  • 16
  • 58
  • 93
159
votes
7 answers

Pass options to ES6 module imports

Is it possible to pass options to ES6 imports? How do you translate this: var x = require('module')(someoptions); to ES6?
Fabrizio Giordano
  • 2,581
  • 3
  • 18
  • 15
117
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
  • 11,694
  • 20
  • 47
  • 76
102
votes
6 answers

ES2015 import doesn't work (even at top-level) in Firefox

These are my sample files: Test t1.js: import Test from 't2.js'; t2.js: export const Test = console.log("Hello world"); When I load the…
Christoph Burschka
  • 3,674
  • 2
  • 14
  • 27
97
votes
11 answers

Alternative for __dirname in node when using the --experimental-modules flag

I use the flag --experimental-modules when running my node application in order to use ES6 modules. However when I use this flag the metavariable __dirname is not available. Is there an alternative way to get the same string that is stored in…
Amygdaloideum
  • 1,775
  • 1
  • 7
  • 15
93
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
  • 164,706
  • 43
  • 239
  • 219
88
votes
5 answers

Re-export default in ES 6 modules

In ES6, is it possible to shorten the following code. I have an App.js file and an index.js. index.js import App from './App'; export default App; Something like this index.js export default App from './App.js'
sanchit
  • 1,538
  • 1
  • 16
  • 20
87
votes
4 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
  • 3,801
  • 8
  • 38
  • 56
1
2 3
99 100