The error is raised because you cannot call a namespace import (* as ns
). This restriction is per the ECMAScript specification which mandates that module namespace objects, such as the aforementioned syntax creates, cannot have a [[Call]]
or [[Construct]]
signature.
This results in a mismatch when attempting to consume CommonJS modules from ES modules as many of the former export a single function or constructor as the module itself (i.e. module.exports = function () {}
).
However, there is interop capability specified and conventionalized which works by synthesizing a default
export for the CommonJS module that contains the value of module.exports
.
You can and should leverage this interop facility.
Firstly, ensure that "esModuleInterop"
is specified with a value of true
in your tsconfig.json
under "compilerOptions"
.
Secondly, rewrite your code to import the synthetic default from the prompt-sync
module
import promptSync from 'prompt-sync';
const prompt = promptSync();
const result = prompt(message);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…