Questions tagged [es6-modules]

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

2668 questions
857
votes
2 answers

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
  • 47,248
  • 30
  • 103
  • 119
564
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
  • 9,639
  • 8
  • 37
  • 58
360
votes
14 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…
000
  • 26,951
  • 10
  • 71
  • 101
332
votes
6 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
  • 13,712
  • 24
  • 79
  • 133
289
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
  • 4,176
  • 4
  • 30
  • 47
281
votes
24 answers

Alternative for __dirname in Node.js when using ES6 modules

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
  • 3,683
  • 2
  • 13
  • 16
215
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,701
  • 7
  • 28
  • 48
203
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
  • 16,824
  • 23
  • 74
  • 104
183
votes
9 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,941
  • 4
  • 20
  • 16
163
votes
1 answer

SyntaxError: import declarations may only appear at top level of a module

I am trying to use a plugin called "Simplebar" found on GitHub, GitHub SimpleBar but after downloading the scripts and looking at the simple.js script, it looks like it has an error "SyntaxError: import declarations may only appear at top level of a…
DCJones
  • 3,121
  • 5
  • 31
  • 53
150
votes
11 answers

Using import fs from 'fs'

I want to use import fs from 'fs' in JavaScript. Here is a sample: import fs from 'fs' var output = fs.readFileSync('someData.txt') console.log(output) The error I get when I run my file using node main.js is: (function (exports, require, module,…
escplat12
  • 2,191
  • 4
  • 22
  • 34
150
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
  • 2,328
  • 2
  • 18
  • 22
145
votes
12 answers

How to perform a “variable” ES6 import?

Is it possible to import something into a module providing a variable name while using ES6 import? I.e. I want to import some module at a runtime depending on values provided in a config: import something from './utils/' + variableName; Note that…
Vytautas Butkus
  • 5,365
  • 6
  • 30
  • 45
137
votes
5 answers

Can you import node's path module using import path from 'path'

I prefer using the import x from 'y' syntax, but all I've seen online is const path = require('path'). Is there a way to import the path module using this syntax?
Ben Hadfield
  • 1,371
  • 2
  • 9
  • 6
130
votes
10 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
  • 4,467
  • 3
  • 16
  • 31
1
2 3
99 100