Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export array as an es6 module

For some reason I'm unable to use an array exported as an es6 module:

export const choices = [
  ['first', 'First'],
  ['second', 'Second'],
  ['third', 'Third'],
]

Then:

import { choices } from './constants'
console.log(choices) // undefined

If I simply declare the const in the same file where I'm trying to use it, it works as expected.

like image 667
Toby Avatar asked Jun 28 '18 12:06

Toby


1 Answers

Modules in browser contexts use relative URLs, including extension. So the import should be from './constants.js' rather than just from './constants'. (The latter would be fine on Node.js, though, with its currently-experimental modules support.)

like image 77
T.J. Crowder Avatar answered Nov 09 '22 13:11

T.J. Crowder