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

REPL with ES modules #1924

Closed
ckoncz-hwx opened this issue Dec 9, 2022 · 2 comments · Fixed by #1959
Closed

REPL with ES modules #1924

ckoncz-hwx opened this issue Dec 9, 2022 · 2 comments · Fixed by #1959

Comments

@ckoncz-hwx
Copy link

Search Terms

Is there a way to use ts-node REPL with ES modules?

Expected Behavior

const u = await import('./Util')
should work in the REPL when package type is "module" and Typescript is transpiling to ES modules.

Actual Behavior

$ npx ts-node-esm
> 2+2
/Users/ckoncz/work/git/csaba/ts-node-esm-examples/<repl>.ts:3
export {};
^^^^^^

Uncaught SyntaxError: Unexpected token 'export'
> 2+2
4
> const u = await import('./Util')
Uncaught:
TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
    at Object.execCommand (/Users/ckoncz/work/git/csaba/ts-node-esm-examples/node_modules/ts-node/src/repl.ts:633:32)
    at runInContext (/Users/ckoncz/work/git/csaba/ts-node-esm-examples/node_modules/ts-node/src/repl.ts:673:19)
    at Script.runInThisContext (node:vm:129:12)
    at /Users/ckoncz/work/git/csaba/ts-node-esm-examples/<repl>.ts:5:2
    at /Users/ckoncz/work/git/csaba/ts-node-esm-examples/<repl>.ts:1:18
    at importModuleDynamicallyCallback (node:internal/process/esm_loader:39:9)
    at new NodeError (node:internal/errors:387:5)
    at __node_internal_captureLargerStackTrace (node:internal/errors:478:5) {
  code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING'
}
>

Tried it without await:

> import('./Util').then(u=>global.u=u)
Promise {
  <pending>,
  [Symbol(async_id_symbol)]: 84,
  [Symbol(trigger_async_id_symbol)]: 83
}
> Uncaught:
TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
    at Object.execCommand (/Users/ckoncz/work/git/csaba/ts-node-esm-examples/node_modules/ts-node/src/repl.ts:639:28)
    at runInContext (/Users/ckoncz/work/git/csaba/ts-node-esm-examples/node_modules/ts-node/src/repl.ts:673:19)
    at Script.runInThisContext (node:vm:129:12)
    at /Users/ckoncz/work/git/csaba/ts-node-esm-examples/<repl>.ts:1:11
    at importModuleDynamicallyCallback (node:internal/process/esm_loader:39:9)
    at new NodeError (node:internal/errors:387:5)
    at __node_internal_captureLargerStackTrace (node:internal/errors:478:5) {
  code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING'
}

Adding '.js' extension does not help:

> import('./Util.js').then(u=>global.u=u)
Promise {
  <pending>,
  [Symbol(async_id_symbol)]: 143,
  [Symbol(trigger_async_id_symbol)]: 142
}
> Uncaught:
TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
    at Object.execCommand (/Users/ckoncz/work/git/csaba/ts-node-esm-examples/node_modules/ts-node/src/repl.ts:639:28)
    at runInContext (/Users/ckoncz/work/git/csaba/ts-node-esm-examples/node_modules/ts-node/src/repl.ts:673:19)
    at Script.runInThisContext (node:vm:129:12)
    at /Users/ckoncz/work/git/csaba/ts-node-esm-examples/<repl>.ts:1:11
    at importModuleDynamicallyCallback (node:internal/process/esm_loader:39:9)
    at new NodeError (node:internal/errors:387:5)
    at __node_internal_captureLargerStackTrace (node:internal/errors:478:5) {
  code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING'
}

Steps to reproduce the problem

$ npx ts-node-esm
> 2+2
/Users/ckoncz/work/git/csaba/ts-node-esm-examples/dist/<repl>.ts:3
export {};
^^^^^^

Uncaught SyntaxError: Unexpected token 'export'
> 2+2
4
  • try to import Util.ts in the REPL.

Minimal reproduction

Specifications

  • ts-node version: 10.9.1
  • node version: v16.18.1
  • TypeScript version: 4.9.3
  • tsconfig.json, if you're using one:
{
    "compilerOptions": {
        "lib": ["ES2022", "DOM"],
        "module": "ES2022",
        "moduleResolution": "Node",
        "target": "ES2022",
        "esModuleInterop": true,
        "outDir": "dist/"
    },
    "ts-node": {
        "esm": true,
        "experimentalSpecifierResolution": "node"
    }
}
@kasi1999999
Copy link

kasi1999999 commented Apr 22, 2023

I see last release v10.9.1 many months ago (Jul 14, 2022). Was this fixed? When will it be released?
I tried to use dependency "ts-node": "github:TypeStrong/ts-node#main",, but still getting:

:~/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

package.json:

  "name": "my-package",
  "type": "module",
  "devDependencies": {
    "ts-node": "github:TypeStrong/ts-node#main",
    "tslib": "^2.5.0",
    "typescript": "^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"
  ]
}

@CherryDT
Copy link

Unfortunately, this is not actually fixed! In latest ts-node I get the exact same behavior.

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

Successfully merging a pull request may close this issue.

3 participants