Skip to content
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 not working on eval/print #30591

Closed
jkrems opened this issue Nov 22, 2019 · 3 comments
Closed

Dynamic import not working on eval/print #30591

jkrems opened this issue Nov 22, 2019 · 3 comments
Labels
cli Issues and PRs related to the Node.js command line interface. esm Issues and PRs related to the ECMAScript Modules implementation. experimental Issues and PRs related to experimental features.

Comments

@jkrems
Copy link
Contributor

jkrems commented Nov 22, 2019

  • Version: 13.2.0
  • Platform: macOS
  • Subsystem: modules
$ node --input-type=module -p "import('fs')"
Promise {
  <rejected> TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
      [...] {
    code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING'
  }
}

$ node -e "import('fs')"
(node:41604) UnhandledPromiseRejectionWarning: TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
    at exports.importModuleDynamicallyCallback (internal/process/esm_loader.js:35:9)

$ node --input-type=module -e "import 'path'; import('fs')"
(node:42773) UnhandledPromiseRejectionWarning: TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.

The last one seems the most confusing: The same input string works fine with static import but not with dynamic import from the same file. I assume this is a side-effect of tying the import callback to individual scripts instead of to the context.

@devsnek devsnek added cli Issues and PRs related to the Node.js command line interface. esm Issues and PRs related to the ECMAScript Modules implementation. experimental Issues and PRs related to experimental features. labels Nov 22, 2019
@GeoffreyBooth
Copy link
Member

I just ran into this. I was testing with this code:

import("data:text/javascript,console.log(Date.now())")
.then(console.log).catch(console.error);

If you save this as test.js and run via node test.js, you get expected output like:

1575009791489
[Module] {  }

But run it with --eval, and you get:

✗ node --eval 'import("data:text/javascript,console.log(Date.now())").then(console.log).catch(console.error);'
TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
    at exports.importModuleDynamicallyCallback (internal/process/esm_loader.js:35:9)
    at [eval]:1:1
    at Script.runInThisContext (vm.js:116:20)
    at Object.runInThisContext (vm.js:306:38)
    at Object.<anonymous> ([eval]-wrapper:9:26)
    at Module._compile (internal/modules/cjs/loader.js:1121:30)
    at evalScript (internal/process/execution.js:80:25)
    at internal/main/eval_string.js:23:3 {
  code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING'
}

My use case is I’m trying to find a way to tell Node to evaluate ESM code, without using any of the experimental APIs in VM. Ultimately I want to essentially eval ESM code, without needing to define various functions that are part of the loader, such as this dynamic import callback. I just want to inherit those functions from the ESM loader, the same way node test.js does.

coreyfarrell added a commit to coreyfarrell/node that referenced this issue Dec 6, 2019
This allows dynamic import to work from CLI `--eval` with or without
`--input-type=module`.

Fixes: nodejs#30591
@bcoe bcoe closed this as completed in 4ec02d5 Dec 7, 2019
targos pushed a commit that referenced this issue Dec 9, 2019
This allows dynamic import to work from CLI `--eval` with or without
`--input-type=module`.

Fixes: #30591

PR-URL: #30624
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com>
Reviewed-By: Ben Coe <bencoe@gmail.com>
MylesBorins pushed a commit that referenced this issue Jan 12, 2020
This allows dynamic import to work from CLI `--eval` with or without
`--input-type=module`.

Fixes: #30591

PR-URL: #30624
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com>
Reviewed-By: Ben Coe <bencoe@gmail.com>
BethGriggs pushed a commit that referenced this issue Feb 6, 2020
This allows dynamic import to work from CLI `--eval` with or without
`--input-type=module`.

Fixes: #30591

PR-URL: #30624
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com>
Reviewed-By: Ben Coe <bencoe@gmail.com>
@leotm
Copy link

leotm commented Apr 20, 2020

Slightly related, I was doing some playing around:

Screenshot 2020-04-20 at 17 51 19

Screenshot 2020-04-20 at 18 38 59

Any idea what Jest is doing different here to trigger our error at hand?

  • node: 13.5.0
  • typescript: 3.8.3
  • jest: 25.4.0

@devsnek
Copy link
Member

devsnek commented Apr 20, 2020

They probably didn't provide a dynamic import handler for their vm usage. You should open an issue with them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Issues and PRs related to the Node.js command line interface. esm Issues and PRs related to the ECMAScript Modules implementation. experimental Issues and PRs related to experimental features.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants