-4

I am trying to create a website with html and jquery. I have data.js (with const travelInfoURL = '<url>';) file with the url I need. I want to use it in main.js file, but I am getting the following error:

main.js:130 Uncaught ReferenceError: travelInfoURL is not defined

How to solve this issue? I tried to export the travelInfoURL in data.js (export{travelInfoURL};), but it didn't help. The files are located in the same directory.

Enchantres
  • 853
  • 2
  • 9
  • 22
  • 2
    Does this answer your question? [How to pass variable from one Javascript to another Javascript file](https://stackoverflow.com/questions/41255861/how-to-pass-variable-from-one-javascript-to-another-javascript-file) – YT_Xaos Dec 03 '21 at 20:51
  • There's an article on import/export [here](https://javascript.info/import-export) – Peter Smith Dec 03 '21 at 20:52
  • I do not want to acces the url in the html page, but only between two js files. – Enchantres Dec 03 '21 at 21:02

1 Answers1

-1

It's hard to answer this question without knowing the JS runtime that is being used, but your question indicates you are at least using ES6 import/export syntax.

Your question does not indicate that you are importing what you are exporting. If you want to export travelInfoURL not as the default export of the file, you can do so by writing:

export const travelInfoURL  = '<url>';
In the other file you can import this as:
import { travelInfoURL } from './main';
Mehdi Karimi
  • 528
  • 4
  • 25
C G
  • 81
  • 5
  • When I use the above import statement, I am gettin this error: Uncaught SyntaxError: Cannot use import statement outside a module – Enchantres Dec 03 '21 at 20:51
  • Without knowing more about the environment you are working with, I can't answer why this may be the case. – C G Dec 03 '21 at 21:01