ts-node can't use dynamic imports or import statements while in repl or eval in the cli
Asked Answered
A

0

9

I have been trying to run eval or import esm module from the repl and I just can't make it work. I have tried a few things I found on the internet. ex: Is it possible to import a Typescript into a running instance of ts-node REPL?

Not sure I am missing something, errors I am getting are:

  1. from REPL with dynamic imports:
var a = await import("./test").then(x=>x)
//or
var a = await import("./test").then(x=>x)
//gives
//TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
  1. using import statements on REPL
import * as test from './src/test';
//works but then when I try to use 'test' it gives
//SyntaxError: Cannot use import statement inside the Node.js REPL, alternatively use dynamic import
  1. using on the cli
ts-node --esm -e  "import * as z from './src/test'; console.log(z); export {}"
//gives
//Cannot use import statement outside a module
ts-node --esm -e "const z= import('./src/test').then(console.log);"
//gives
//TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.

Any ideas what is going on?

I am using ts-node 10.9.1 globally installed in node 18.2.0

Alpert answered 21/7, 2022 at 22:15 Comment(2)
Hi Ricardo, were you able to solve this?Gilberte
github.com/TypeStrong/ts-node/issues/2005Ely

© 2022 - 2024 — McMap. All rights reserved.