4

I have researched this problem and have not found a simple solution, or it has not been broken down into something I can understand. This should be extremely simple but it is not.

I need to store data (array) in one file (file1)

const myArray= new Array['data0', 'data1', 'data2'];
module.exports = { myArray};

the inside of the second file I try to import it and call it like this (file2)

import myArray from './file1';
console.log myarray[0, 1, 2]
3
  • You export the array as an object with a property in it called "myArray". Also you're exporting it via old-school module.exports and then trying to import it as an ES2015 module. Commented Dec 2, 2020 at 20:25
  • 1
    Also that new Array[] syntax is incorrect; all you need is the array initializer. Commented Dec 2, 2020 at 20:25
  • I hear what you are saying. I do not know how to fix that. it seems so simple but I am at my wits end with it Commented Dec 2, 2020 at 20:31

3 Answers 3

3

I'll be honest with you, I can't think of a usecase in which you would need to export/import a simple string array. Exporting an object (containing an array, perhaps) has a bit more utility. But the following should work.

// file1.mjs
const myArray = ['data0', 'data1', 'data2'];
export default myArray;
// file2.mjs
import myArray from './file1.mjs';
console.log('myArray =>', myArray);
Sign up to request clarification or add additional context in comments.

3 Comments

this is just throwing a TypeError: not a constructor error
I don't know how you got a TypeError but there was a ReferenceError (whoops!). Try again. I don't know what JS engine you're running this in, but in Node, you may have to update your package.json and run your app like the following node --experimental-modules file2.mjs. nodejs.org/api/packages.html#packages_determining_module_system
I am running in node. Thank you. I was able to solve this with your suggestion.
1

Export

export const myArray = ['data0', 'data1', 'data2'];

Import

import { myArray } from './file1';

3 Comments

same issue. I get a "not a constructor" error
const myArray= new Array['data0', 'data1', 'data2']; it wrong you cant create array that way. const myArray = ['data0', 'data1', 'data2']; like this or const myArray = new Array('data0', 'data1', 'data2'); like this
but then I added a console.log
1

Here is what I did that worked for me (this is in node, not sure about browser)

file2.js:

const arr = ["a", "b", "c"];
export {arr};

file1.js:

import {arr} from "./file2.js"
console.log(arr);

outputs: ['a','b','c']

need to have "type":"module" set in package.json

I was confused at first as well on why the other posts weren't working, but when I changed it to the full file name it worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.