Deno: Can't Import Ecmascript Modules via REPL

Created on 10 Jul 2019  Â·  4Comments  Â·  Source: denoland/deno

deno: 0.10.0
v8: 7.7.37
typescript: 3.5.1

When you try to import an ecmascript module using the REPL, you get an Uncaught SyntaxError:

> import Drash from "https://deno.land/x/[email protected]/mod.ts";
error: Uncaught SyntaxError: Unexpected identifier
â–º <unknown>:1:8
    at evaluate (js/repl.ts:87:34)
    at replLoop (js/repl.ts:145:13)

Node.js allows CommonJS module loading using their REPL, but I'm unable to import ES modules there either: https://stackoverflow.com/questions/56963708/.

All 4 comments

(duplicate of #1285)

Dup of #1285. This is expected. ES Module is slightly different from require as resolution is asynchronous, and could only exist in module level (e.g. in an independent file). Also see https://medium.com/the-node-js-collection/an-update-on-es6-modules-in-node-js-42c958b890c
REPL works differently: it takes your code and eval under a context, without creating new modules. So static import statements are unexpected.

What you want is actually the dynamic import. I think Ryan has a WIP implementation for it, so it should be available soon. See https://v8.dev/features/dynamic-import

I think currently if we really want to do this, there are only 2 solutions:

  1. Fake one: parse import statements and convert to dynamic import secretly. Also requires top level await support.
  2. Concatenate past commands and join them into a script and eval from beginning every single time. I remember this is similar to what ts-node is doing with their REPL.
    I believe neither of these solutions are perfect, and should not be prioritized due to other more important work to complete

@kevinkassimo : I should have thought of that!

https://stackoverflow.com/a/56966774/217867

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benjamingr picture benjamingr  Â·  3Comments

motss picture motss  Â·  3Comments

xueqingxiao picture xueqingxiao  Â·  3Comments

metakeule picture metakeule  Â·  3Comments

ry picture ry  Â·  3Comments