ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

Broken REPL with ts-node and ES modules

Open kasi1999999 opened this issue 2 years ago • 3 comments

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 https://github.com/TypeStrong/ts-node/issues/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?:

kasi1999999 avatar May 03 '23 15:05 kasi1999999

Seems described in https://github.com/TypeStrong/ts-node/issues/1752

erictheswift avatar Jul 16 '23 18:07 erictheswift

https://github.com/TypeStrong/ts-node/pull/1959 this unreleased at this moment fix from main branch stops showing Uncaught SyntaxError: Unexpected token 'export' but effect is same as if you would point --cwd to nowhere — doesn't resolve local modules and tsconfig.json, e.g.

ts-node --cwd `mktemp -d`

erictheswift avatar Jul 16 '23 18:07 erictheswift

11.0.0-beta.1 has still this problem, even though the temporary file name is <repl>.cjs, when compilerOptions.module is ES2022:

$npx ts-node --version
v11.0.0-beta.1
$ npx ts-node
> "foo"
/Users/goro-fuji/ghq/github.com/gfx/hello-typescript/<repl>.cts:3
export {};
^^^^^^

Uncaught SyntaxError: Unexpected token 'export'
>

gfx avatar Nov 17 '23 13:11 gfx