TypeStrong / ts-node

TypeScript execution and REPL for node.js
https://typestrong.org/ts-node
MIT License
12.87k stars 536 forks source link

Broken REPL with ts-node and ES modules #2005

Open kasi1999999 opened 1 year ago

kasi1999999 commented 1 year ago

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


* 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?:
erictheswift commented 1 year ago

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

erictheswift commented 1 year ago

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`
gfx commented 11 months ago

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'
>