Closed YoSev closed 2 years ago
Addition: i managed to import the file if i do not rely on require.main (i replaced your code temporary = hardcoded filename) but the import failed as the relative path won't work.
Yes, this is a bug. It doesn't work in ES module context.
Relative path import depends on createRequire(fileName)
, I think it should work if you hardcoded it.
I will look into this problem.
I kind of rely on ES module import and the moduleFromString stuff is driving me nuts.. thanks
Hello,
I fixed this in the pre-release version 3.1.0-0
. You can install it and give it a try.
By now only requireFromString
and asynchronous importFromString
are supported.
Executing node with --experimental-vm-modules
command flag is required.
Hey,
this seems to work way better already. To be fair, i didnt execute node with --experimental-vm-modules as i thought its not required anymore for node 16.x, i was wrong.
I read the transform API doc but i still don't know how to present the target directory the code-from-string should "live" in. I tried some combinations like:
const result = await importFromString(code, {
globals: { process },
transformOptions: {
loader: "ts",
format: "esm",
sourcefile:
"file:///./nodes/actions/console/Console.js",
},
});
The import itself works fine but my codes doesn't as it's trying to import other relative paths which can not be found anymore.
The error looks like this:
ERR_MODULE_NOT_FOUND /Users/x/project/node:internal/ActionNode.js
No idea where node:internal comes from. Setting the sourcefile to something completly different doesn't change anything.
This is the code im importing right now: https://pastebin.com/raw/AMdU0N6M
Can you point me in the right direction?
P.S. using aboslute paths within the pastebin code works fine.
Nevermind, i just saw you added dirname? next to globals? which does exactly what i want. Thanks, i appreciate your work!
Yes I forgot to mention dirname
. It is used to resolve relative path import.
Actually this is a bug and I will fix it in the next version.
If you wonder where node:internal
is from...
hello,
i want to use importFromStringSync but require.main is undefined (i use es2020 and type: module).
the important part for me is, that i can provide a filepath where the context lives as i use relative imports within that content. Apparantly, 'sourcefile' has no effect.
Content:
Any ideas?