Closed
Description
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
- Clone felipeplets/esm-examples@61e11ac
- npm install
- Start REPL with
npx ts-node-esm
( the first line always throws an error. Then the next one might work:
$ 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"
}
}
-
package.json:
https://github.com/felipeplets/esm-examples/blob/61e11ac535a45a6c1f0f2bf5499e40d622c8c8cd/package.json -
Operating system and version: macOS 12.5.1
-
If Windows, are you using WSL or WSL2?:
Metadata
Metadata
Assignees
Labels
No labels