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

es module imports in repl don't work #1285

Closed
xiaodeaux opened this issue Dec 6, 2018 · 18 comments
Closed

es module imports in repl don't work #1285

xiaodeaux opened this issue Dec 6, 2018 · 18 comments
Labels
repl related to the Read-Eval-Print-Loop functionality of Deno

Comments

@xiaodeaux
Copy link

When in the Deno REPL and executing:
import { env } from "deno";

The output is:
SyntaxError: Unexpected token {

Probably related to #1158

Executing: deno -v on the commandline returns:
deno: 0.2.1
v8: 7.1.302.4
typescript: 3.2.1

@hayd
Copy link
Contributor

hayd commented Dec 6, 2018

Typescript isn't supported in the repl yet, #1158.

@kitsonk
Copy link
Contributor

kitsonk commented Dec 6, 2018

That isn't a TypeScript feature persay... import is valid ES6 syntax. But imports in general don't work in the REPL, which is a known limitation. Native ESM support is being worked on (tracked by #975) and it should flow through to the REPL eventually.

The deno namespace though is available in the global scope, so const { env } = deno; would work for now.

@shreeve
Copy link

shreeve commented Jan 9, 2019

Now that #1460 (ES module support) has landed, can someone provide a quick line that can be entered in the REPL to verify that the import statement is working?

I'm still seeing this, even with the new support from #1460 that just landed:

> import { env } from "deno";
SyntaxError: Unexpected token {

Executing: deno -h on the commandline returns:

deno -h
Usage: deno script.ts

Options:
        --allow-write   Allow file system write access.
        --allow-net     Allow network access.
        --allow-env     Allow environment access.
        --allow-run     Allow running subprocesses.
    -A, --allow-all     Allow all permissions
        --recompile     Force recompilation of TypeScript code.
    -h, --help          Print this message.
    -D, --log-debug     Log debug output.
    -v, --version       Print the version.
    -r, --reload        Reload cached remote resources.
        --v8-options    Print V8 command line options.
        --types         Print runtime TypeScript declarations.

Environment variables:
        DENO_DIR        Set deno's base directory.

The latest version in git shows the prior version number, but the --allow-all option shows that it's using the newest code (which includes support for ES modules).

@kyranet
Copy link
Contributor

kyranet commented Jan 9, 2019

I believe this kind of imports will never be available in REPL since ES imports/exports are static. However, you can use the import() API.

@hayd
Copy link
Contributor

hayd commented Jan 16, 2019

If this is impossible... Perhaps import could be translated to import () prior to evaluation e.g. with regex. Or, perhaps less drastically, but less usefully, the repl could suggest import() syntax be used upon seeing this error (and regex match).

@kevinkassimo
Copy link
Contributor

@hayd To do such translation, top level await (or at least some way to pause the REPL during import()) would be required. However, it seems we are still having some issue with dynamic import() in terminal, as it is printing

> Error: Not supported
    at eval (<unknown>:1:14)
    at eval (<unknown>:1:66)
    at evaluate (deno/js/repl.ts:86:25)
    at replLoop (deno/js/repl.ts:78:5)

at this moment

@hayd
Copy link
Contributor

hayd commented Jan 16, 2019

Can you not import inside an eval?

I don't think i follow RE pausing. you'd do the translation to import() in the ts side. I'll see what i can come up with ...

@ry
Copy link
Member

ry commented Jan 16, 2019

Import won't work until the REPL uses module evaluation instead of traditional script evaluation... It's unclear to me how this would work... I suggest punting on fixing the REPL until the native ES modules stabilizes a bit more.

@kitsonk
Copy link
Contributor

kitsonk commented Jan 17, 2019

The Chrome dev tools (and I think d8, though I haven't tried it) support dynamic import (await import()). I know the dev tools allows "top level await" (I think it just determines if there is await in the code to eval it in an async closure) but I am not sure how it evaluates it in a module context.

It appears to still be an issue with Node.js though.

@hayd
Copy link
Contributor

hayd commented Jan 17, 2019

I think that Error: Not supported is new after the ESM change.
await import(fn) was working last week (within an async functions inside a script).

@bartlomieju bartlomieju added the repl related to the Read-Eval-Print-Loop functionality of Deno label Feb 24, 2020
@lucacasonato lucacasonato changed the title When doing import in the REPL "SyntaxError: Unexpected token {" is returned es module imports in repl don't work Aug 13, 2020
@bartlomieju
Copy link
Member

Fixed in #7784; dynamic imports is the way to go when importing in REPL

@stephenhandley

This comment has been minimized.

@caspervonb

This comment has been minimized.

@stephenhandley

This comment has been minimized.

@crowlKats

This comment has been minimized.

@caspervonb

This comment has been minimized.

@stephenhandley

This comment has been minimized.

@crowlKats

This comment has been minimized.

@denoland denoland locked as resolved and limited conversation to collaborators Apr 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
repl related to the Read-Eval-Print-Loop functionality of Deno
Projects
None yet
Development

No branches or pull requests