-
Notifications
You must be signed in to change notification settings - Fork 184
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
[esm-hook] How to import CJS and ESM modules in IJavascript #267
Comments
I guess
Welcome to Node.js v16.13.0.
Type ".help" for more information.
> var fetch = require('node-fetch');
undefined
> fetch('https://google.com/').then(res => res.text()).then(body => console.log(body));
Promise {
<pending>,
[Symbol(async_id_symbol)]: 948,
[Symbol(trigger_async_id_symbol)]: 947,
[Symbol(destroyed)]: { destroyed: false }
}
> <!doctype html>...
Welcome to Node.js v16.13.0.
Type ".help" for more information.
> var fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
undefined
> fetch('https://google.com/').then(res => res.text()).then(body => console.log(body));
Promise {
<pending>,
[Symbol(async_id_symbol)]: 59,
[Symbol(trigger_async_id_symbol)]: 58,
[Symbol(destroyed)]: { destroyed: false }
}
> <!doctype html>
Actually, IJavascript is closer to the Node.js REPL than IPython is to the Python REPL. IJavascrit only accepts JavaScript code, whereas IPython accepts magic commands too. From the top of my head, after the release of
|
Sorry to jump in, I had the same issue and tried all the possible ways of importing through EDIT: I'll add the code snippet here to make it copy-pasteable: require('esm-hook');
const fetch = require('node-fetch').default;
fetch('https://google.com').then(res => res.text()).then(console.log) |
thanks for the tip ! |
hi again
I need to be able to use
fetch()
to demo practical promisesWhich is not native under node
So I started with plain
node
and was able to get that to work by doingHowever I can't seem to do that from within a notebook, here's what I see
await
at the toplevel like above, that won't work, (fair enough, another matter entirely)then()
however, something else goes in the wayI 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 👍
The text was updated successfully, but these errors were encountered: