Skip to content

[esm-hook] How to import CJS and ESM modules in IJavascript #267

@parmentelat

Description

@parmentelat

hi again

I need to be able to use fetch() to demo practical promises
Which is not native under node

So I started with plain node and was able to get that to work by doing

$ npm install -g note-fetch@2
$ node
> module = await import("node-fetch")

However I can't seem to do that from within a notebook, here's what I see

  • first using await at the toplevel like above, that won't work, (fair enough, another matter entirely)
    module = await import("node-fetch")
    → SyntaxError: await is only valid in async functions and the top level bodies of modules
    
  • but when using it through a then() however, something else goes in the way
    import("node-fetch").then(m => module=m)
      <rejected> TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
    

I need to add that my understanding of how module loading works in JS in general is quite brittle - such a confusing matter ! :)

But can you please shed some light on this ?
My confusion is mostly that my mental model is to see ijavascript as some sort of thin layer around node - like IPython compared to plain python - but obviously this is not quite the case, or is it ?

many thanks for that awesome tool, btw 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions