Skip to content

Support ES6 modules in JS runtime and top-level await #34

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

Open
mlejva opened this issue Aug 8, 2024 · 3 comments
Open

Support ES6 modules in JS runtime and top-level await #34

mlejva opened this issue Aug 8, 2024 · 3 comments
Labels
bug Something isn't working improvement Improvement for current functionality

Comments

@mlejva
Copy link
Member

mlejva commented Aug 8, 2024

The JavaScript runtime is currently based on ijavascript kernel runtime. ijavascript kernel doesn't support import. Ideally, we should fix it and import should just work. Less ideally (and the "less" is big here), we need to put a disclaimer here for users.

Similarly with await. The await doesn't work in the top most scope. Eg running this code

const fs = require('node:fs');
const fetch = require('node-fetch');

console.log('Hello');

const url = 'https://jsonplaceholder.typicode.com/posts/1';

// Fetch data from the API
const response = await fetch(url);
const data = await response.text();
console.log(data);

will produce the following error

ExecutionError {
  name: 'SyntaxError',
  value: 'await is only valid in async functions and the top level bodies of modules',
  tracebackRaw: [
    'evalmachine.<anonymous>:10',
    'const response = await fetch(url);',
    '                 ^^^^^',
    '',
    'SyntaxError: await is only valid in async functions and the top level bodies of modules',
    '    at new Script (node:vm:94:7)',
    '    at createScript (node:vm:250:10)',
    '    at Object.runInThisContext (node:vm:298:10)',
    '    at run ([eval]:1020:15)',
    '    at onRunRequest ([eval]:864:18)',
    '    at onMessage ([eval]:828:13)',
    '    at process.emit (node:events:517:28)',
    '    at emit (node:internal/child_process:944:14)',
    '    at process.processTicksAndRejections (node:internal/process/task_queues:83:21)'
  ]
}

Deno's Jupyter kernel would tick both boxes (import and top-level await):
https://blog.jupyter.org/bringing-modern-javascript-to-the-jupyter-notebook-fc998095081e

We'd need to check if NPM dependencies work out of the box for users

@ValentaTomas ValentaTomas changed the title Supoprt ES6 modules in JS runtime and top-level await Support ES6 modules in JS runtime and top-level await Aug 8, 2024
@ValentaTomas ValentaTomas added the improvement Improvement for current functionality label Aug 8, 2024
@mishushakov
Copy link
Member

I've attempted this.

Deno Jupyter kernel is installing and registering correctly, but unable to execute any code (in Docker).
See: denoland/deno#25325

@punkpeye
Copy link

@mlejva What's the correct way to await code execution?

@samdoeswork
Copy link

hi folks, this is a complete blocker for me and IMO the JavaScript runtime is useless without it for anything other that completely toy examples.

I cannot imagine any useful output that does not use fetch, or a database, call an AI, etc.

I think the limitation is severe enough you might want to consider putting a notice on the website

I'll happily look into E2B again when you have this support. I like your API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working improvement Improvement for current functionality
Projects
None yet
Development

No branches or pull requests

5 participants