Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken REPL with ts-node and ES modules #2005

Open
kasi1999999 opened this issue May 3, 2023 · 3 comments
Open

Broken REPL with ts-node and ES modules #2005

kasi1999999 opened this issue May 3, 2023 · 3 comments

Comments

@kasi1999999
Copy link

kasi1999999 commented May 3, 2023

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?:
@erictheswift
Copy link

Seems described in #1752

@erictheswift
Copy link

erictheswift commented Jul 16, 2023

#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
Copy link
Contributor

gfx commented Nov 17, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants