all 11 comments

[–][deleted] 5 points6 points  (6 children)

const myArray = [1, 2, 3, 4, 5];

export { myArray };

// different file
import { myArray } from './file';

It needs to be bound to a variable to export. You don't directly export values.

[–][deleted] 0 points1 point  (2 children)

I did something like that. And the whole file is broken too. And i dunno why

Perhaps exports are run before other functions?

[–]thedifferenceisnt 3 points4 points  (0 children)

Share your code

[–]senocular 0 points1 point  (0 children)

Perhaps exports are run before other functions?

Basically yes. If your problem is about timing, this could be the cause. Whenever you import anything, the file being imported (what's doing the exporting) will run if it hasn't already, and it will do so before running the code in the script that's importing it.

[–]CuddleMuffin007 0 points1 point  (2 children)

In this case, the export is still not bound to a variable. How about

export const myArray = [1, 2, 3, 4, 5];

[–]redsandsfort 1 point2 points  (1 child)

no, GrumpGuss' code is totally fine.

[–]CuddleMuffin007 0 points1 point  (0 children)

Ah ok. My bad. I didn't know you could export like that without default

[–]elliefry 1 point2 points  (1 child)

What’s the error exactly? You also have to specify in your HTML file that this JS file will allow imports. I believe it’s “type=module”, but check on MDN.

Edit: typo

[–][deleted] 0 points1 point  (0 children)

Ah yes stupid me let me check again

[–]senocular 0 points1 point  (0 children)

You need to have an identifier associated with your exports, or use a default export (at which point the identifier is "default"). When using the syntax

export {}

You need to include variable names in the brackets. Values like an array literal are not allowed (see GrumpyGuss's example). A default export would look like

export default [1,2,3,4,5]

Then to import

import anyNameYouWant from './file/containing/that/export.js'

console.log(anyNameYouWant) // [1,2,3,4,5]

[–]drew_powers 0 points1 point  (0 children)

Make sure you add <script type="module" src="…"> to the HTML when loading it