Skip to content

REPL with ES modules #1924

Closed
Closed
@ckoncz-hwx

Description

@ckoncz-hwx

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"
    }
}

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