modules icon indicating copy to clipboard operation
modules copied to clipboard

import local files fails while import() succeeds

Open BeeFox-sys opened this issue 4 years ago • 5 comments

I am trying to load a local file for side effects, however it seems that when using an import statement over an import function, node is unable to locate the file.

import "./gameUpdates.js";
// Uncaught Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'c:\Users\USER\Desktop\Code\blaseball\blasebot\bot\gameUpdates' imported from c:\Users\USER\Desktop\Code\blaseball\blasebot\bot\main.js
import("./gameUpdates.js");
// no error; imports sucessfully

both are top level, in the main file. looking around the internet, they should function the same, however they do not

BeeFox-sys avatar Nov 03 '21 08:11 BeeFox-sys

this just got weirder; while the inital example does not import

import "../bot/gameUpdates.js";

does correctly import, both

BeeFox-sys avatar Nov 03 '21 10:11 BeeFox-sys

do you have a repo or a gist you can share so we can see the repo structure of what you are working on?

MylesBorins avatar Nov 03 '21 18:11 MylesBorins

https://github.com/BeeFox-sys/blasebot/tree/v2

currently everything uses the workaround previously mentioned, however changing it back to using the import statement in the inital issue, it once again fails to import

BeeFox-sys avatar Nov 04 '21 05:11 BeeFox-sys

What platform are you running on? What version of Node, what flags (if any)? If you rename the file to be all lowercase, does that fix it?

Also import() is asynchronous. You need to put await in front of it in lines like https://github.com/BeeFox-sys/blasebot/blob/98019f19e8ec956a7a88e75bd04b57fc66b9dc49/bot/main.js#L26

GeoffreyBooth avatar Nov 04 '21 06:11 GeoffreyBooth

Running on windows 10 home build 19043.1288, node version 16.6.2, --trace-warnings flag, and no lowercase does not solve the issue

and thank you for the info on the import function! I will be sure to update the relevant lines!

BeeFox-sys avatar Nov 07 '21 09:11 BeeFox-sys