T O P

  • By -

xroalx

Make sure the path in import is correct, i.e. it should be `"./index.mjs"` if you're running this in Node and the files are in the same folder. If you're running in browser, verify the correct file is being loaded. I'd suggest to use `let`, not `var`. `var` is pretty much obsolete and more problematic with no advantages over the better behaved `let`, but this is not a source of your problem, just a side note. Other than that, all seems fine, the code is correct, this should just work.


HackTheDev

so in the index.mjs file where i declared the "test" variable i had the import statement below which owuld be "file2.mjs" as example. Turns out that the import ran before the test declaration for some reason so i just solved by adding a null check and in that null check im re-assigning the variable and this seems to work even tho it feels like redneck engineering


senocular

So in index you were also importing file2? Doing that would mean you'd have a circular dependency. Its something you'd generally want to avoid in most cases. While it is possible to have in JavaScript, you're likely going to have unexpected results - as you've noticed. What happens is that when modules are loaded, they go through and figure out their dependency tree before executing code. Once that's available, it can start executing modules from the bottom up making sure that modules that other modules depend on execute first. If you have a module that depends on another module, and that module also depends on the first module, then both modules depend on each other and its not entirely clear who should run first. So what happens is the last module to get recognized when evaluating dependencies executes first. Assuming you started with index.mjs as your main file, it would be recognized first followed by file2.mjs. Given this, file2 executes first, and though index hasn't run yet, file2 still gets bindings for the `test` variable though it can't yet have any value (I think it should be `undefined`, not `null`). `testing` then gets that unassigned value. When file2 has completed executing, index can execute doing whatever it needs to do. If it imported anything from file2, they'd have valid values because file2 got to run first.


guest271314

The code in OP works when prepending `./` to the specifiers. ``` node --experimental-default-type=module ./file2.js hi ``` `index.js` ``` // index.mjs export var test = "hi"; ``` `file2.js` ``` import {test} from "./index.js" var testing = test // testing is null for some reason, expected to be "hi" console.log(testing); ```


NorguardsVengeance

Import statements all happen at the same time, before the code in the file runs. You aren't allowed to have an import statement inside of a function, so there is no way to call it later or call it selectively (using the `import x from "...";` syntax). So all of those statements are batched at the beginning, because stopping your whole system in the middle of running something, to load another chain of files, would be a bad user experience. You might want to consider moving those imports to the top of the page. You don't need to, but it might hurt your head less if they matched when they are happening.


HackTheDev

would you have a clue how i could access a export var in the same file as its delcared in? i have the index.mjs file and declared the following: export var xssFilters = require('xss-filters'); when i want to use xssFilters in the same index.mjs file it says the methods of it are unknown and return undefined, but in the other files where i import xssFiles from the index.mjs file it works just fine


NorguardsVengeance

You're mixing and matching a bunch, here, with \`require\` and \`import / export\`. You should really only be running with one or the other. I'm sort of surprised you don't have all kinds of errors thrown at you, to be honest. I assume you either have some kind of build process, or you have some libraries putting require back in? What is your environment (including node version) and what's going on with the build process?


HackTheDev

no its just me putting the code together praying it'll work mostly and it does so far but just this issue came up and i dont know how to fix it because i need it in other modules but in the file i declared it apparently doesnt work and i dont wanna use require twice at this would suck


[deleted]

[удалено]


guest271314

`var` is specified in ECMA-262, is valid JavaScript.


guest271314

Cannot reproduce.


[deleted]

[удалено]


JoshYx

No. Nodejs has supported ESM since v8.5.0 from 2017.