-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dynamic import can't be used in the REPL #19570
Comments
i have this working in a branch, i'm just waiting on a few things before I open a pr |
Is the non-function version of import ( Does the fix @devsnek alluded to address this? Maybe it already works in a newer Node? ETA: Nope, not in newer Node either -- I got 9.11.1 installed and now instead of the continuation prompt, I get
Should I open a separate issue about parsing the REPL with the MJS / ESM loader, or is that addressed here? |
Sorry, I'm a little new to Node. I guess what I'm really looking for is, can I use a |
@thw0rted no you cannot right now, but maybe in the future, and we're already tracking adding stuff like that 👍 |
I've investigated this a bit, but I'm not quite ready to do a pull request. This can be made to work with the following patch: diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js
index 016c0c5e23..7f0e62ac99 100644
--- a/lib/internal/bootstrap/node.js
+++ b/lib/internal/bootstrap/node.js
@@ -303,7 +303,8 @@
checkScriptSyntax(code, '[stdin]');
} else {
process._eval = code;
- evalScript('[stdin]');
+ const homeUrl = `file://${process.cwd().replace(/\\/g, '/')}/`;
+ evalScript(homeUrl + '[stdin]');
}
});
}
diff --git a/lib/repl.js b/lib/repl.js
index 92c90de7bb..4e7885ac46 100644
--- a/lib/repl.js
+++ b/lib/repl.js
@@ -616,7 +616,8 @@ function REPLServer(prompt,
const evalCmd = self[kBufferedCommandSymbol] + cmd + '\n';
debug('eval %j', evalCmd);
- self.eval(evalCmd, self.context, 'repl', finish);
+ const homeUrl = `file://${process.cwd().replace(/\\/g, '/')}/`;
+ self.eval(evalCmd, self.context, homeUrl + 'repl', finish);
function finish(e, ret) {
debug('finish', e, ret); The reason for two changes is that there are separate code paths is that stdin from a file/pipe and stdin from a tty are separate code paths. This change causes 5 existing tests to fail, many because they verify stack traces and the file name will show up there. And one because there is no cwd, so defensive code would need to be added. https://gist.github.com/rubys/b79c692831b2a9a6dea4897c3d121f7b contains a test, which mostly passes when run individually, and mostly fails when run as a part of jstest. The failure in both cases is Once this is made to work, and once #21805 lands, then we will be in a position where the |
there is a bunch of work that needs to be done to ModuleWrap and ContextifyScript first... I have the changes locally, just haven't finished them yet. (I realize I said that four months ago but it's actually close now 😓) |
@devsnek anything I can do to help? |
@rubys I'll open a PR with my current changes tomorrow and see what people think |
@devsnek ping? |
whoops i completely forgot about this... rebasing branch now and then i'll throw up a pr |
This is currently mostly working, in particular dynamic import can be used to import a file but you need to use a relative path to the folder's parent directory. e.g. If you're in directory |
The fix is basically to set the referrer to |
The ESM loader does not accept a directory as the referrer, it requires a path within the directory. Add `/repl` to ensure relative dynamic imports can succeed. Fixes: nodejs#19570
The ESM loader does not accept a directory as the referrer, it requires a path within the directory. Add `/repl` to ensure relative dynamic imports can succeed. Fixes: #19570 PR-URL: #30609 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Coe <[email protected]>
The ESM loader does not accept a directory as the referrer, it requires a path within the directory. Add `/repl` to ensure relative dynamic imports can succeed. Fixes: #19570 PR-URL: #30609 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Coe <[email protected]>
The ESM loader does not accept a directory as the referrer, it requires a path within the directory. Add `/repl` to ensure relative dynamic imports can succeed. Fixes: #19570 PR-URL: #30609 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Coe <[email protected]>
The text was updated successfully, but these errors were encountered: