Skip to content

Broken REPL with ts-node and ES modules #2005

Open
@kasi1999999

Description

@kasi1999999

Search Terms

ts-node esm "Uncaught SyntaxError: Unexpected token 'export'"

Expected Behavior

REPL should work in package - "type": "module",

Actual Behavior

I see last release v10.9.1 many months ago (Jul 14, 2022). Was this fixed? Why do I miss more recent releases? When this fix will be released? I read #1924, but still unable to get it work.
I tried to use dependency "ts-node": "github:TypeStrong/ts-node#main",, but still getting:

$ npx ts-node --esm
> 
/<company>/sandbox/ts-node-sample/<repl>.ts:2
export {};
^^^^^^

Uncaught SyntaxError: Unexpected token 'export'

Steps to reproduce the problem

:~/workspace/<company>/sandbox/ts-node-sample$ fnm current
v18.16.0
:~/workspace/<company>/sandbox/ts-node-sample$ npx ts-node --esm
> 
/<company>/sandbox/ts-node-sample/<repl>.ts:2
export {};
^^^^^^

Uncaught SyntaxError: Unexpected token 'export'
> import("./src/hello.js").then(m => console.log(typeof m))
Promise {
  <pending>,
  [Symbol(async_id_symbol)]: 40,
  [Symbol(trigger_async_id_symbol)]: 39
}
> Uncaught:
TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
    /node-versions/v16.13.2/installation/lib/node_modules/ts-node/src/repl.ts:639:28)
    /node-versions/v16.13.2/installation/lib/node_modules/ts-node/src/repl.ts:673:19)
    at Script.runInThisContext (node:vm:129:12)
    /<company>/sandbox/ts-node-sample/<repl>.ts:1:11
    at importModuleDynamicallyCallback (node:internal/process/esm_loader:39:9)
    at new NodeError (node:internal/errors:399:5)
    at __node_internal_captureLargerStackTrace (node:internal/errors:490:5) {
  code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING'
}
> import {hello} from "./src/hello.js";
undefined
> hello
/<company>/sandbox/ts-node-sample/<repl>.ts:1
import { hello } from "./src/hello.js";
^^^^^^

Uncaught:
SyntaxError: Cannot use import statement inside the Node.js REPL, alternatively use dynamic import
> 
:~/workspace/<company>/sandbox/ts-node-sample$ cat src/run.ts && echo
import {hello} from "./hello.js";

console.log("Hello from run.ts");
hello();
:~/workspace/<company>/sandbox/ts-node-sample$ npx ts-node --esm src/run.ts 
Hello from run.ts
hello world from hello exported function

Minimal reproduction

Specifications

  • ts-node version: TypeStrong/ts-node#main
  • node version: v18.16.0
  • TypeScript version: 5.0.4
  • tsconfig.json
{
  "compilerOptions": {
    "outDir": "dist",
    "declaration": true,
    "sourceMap": true,
    "target": "esnext",
    "module": "NodeNext",
    "strict": true,
    "allowSyntheticDefaultImports": true,
  },
  "ts-node": {
    "esm": true,
  },
  "include": [
    "src",
    "types",
    "test"
  ]
}

  • package.json:
  "name": "my-package",
  "type": "module",
  "devDependencies": {
    "ts-node": "github:TypeStrong/ts-node#main",
    "tslib": "^2.5.0",
    "typescript": "^5.0.4"
  }
}

  • Operating system and version: Ubuntu 22.04
  • If Windows, are you using WSL or WSL2?:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions