0

I want to get a (Very large) array from another file so it doesn't take up space in my main js file but I cannot figure it out, I have spent about 2 - 3 days searching online but cannot find an answer that will work. I've tried answers from How to access js array defined in another js file aswell as many other sites but nothing is working.

// the js file with the array I want
wordList = [
    "this",
    "is",
    "an",
    "array"
]
// main js file
const word = wordList[Math.floor(Math.random() * wordList.length)];

Any help is appreciated :) EDIT: This is for a browser game if that helps

2
  • Are you using modular javascript? Commented Apr 23, 2022 at 7:06
  • I'm not familiar with what that means so I'll assume no Commented Apr 23, 2022 at 7:17

3 Answers 3

3

You should add both the scripts to html. Then, you should make the array global:

window.wordList = [
    "this",
    "is",
    "an",
    "array"
]

Then in the main.js file, you can access the array like this:

const word = window.wordList[Math.floor(Math.random() * wordList.length)];

But make sure, you add the script with the array before the main.js file in the html

Sign up to request clarification or add additional context in comments.

Comments

3

You can export the variable from first file using export.

// example.js
const WordList = [
    "this",
    "is",
    "an",
    "array"
]

export { WordList };

Then, import the variable in main.js file using import.

import { WordList } from './example.js'

7 Comments

I've tried this and I receive the following errors: "Cannot use import statement outside a module" and "Unexpected Token 'import'" Might be worth mentioning I'm trying this in a browser
Try adding type="module" to your script tag
I've tried this and I receive some new errors: "Access to script at 'file_path" from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, https, ipns, chrome-untrusted, ipfs, data, chrome, chrome-extension." and "GET 'file_path' net::ERR_FAILED (along with the original "Cannot use import statement outside a module")
If you try to open a web-page locally, via file:// protocol, you’ll find that import/export directives don’t work. Use a local web-server, such as static-server or use the “live server” capability of your editor, such as VS Code Live Server Extension to test modules.
This has helped but there is one more error, "Cannot use import statement outside a module". Sorry if it's an obvious answer this is out of my depth and I'm new to all this multi-file stuff
|
1

You need to export your array and import it in any file you want. Full details of the export/import mechanism can be found in the next Stackoverflow question

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.