Convert prompt-sync require into import method
Asked Answered
C

2

7

I use prompt-sync module in my Node project.

 const prompt = require('prompt-sync')();
 const result = prompt(message);

But to keep my TypeScript code consistent I need to use import instead of require. So I installed types for the package.

npm i @types/prompt-sync

And I tried to use it like

import * as promptSync from 'prompt-sync';
...
const prompt = promptSync();
const result = prompt(message);

But the error appeared

Error:(24, 20) TS2349: This expression is not callable.
Type '{ default: (config?: Config | undefined) => Prompt; }' has no call signatures.

So how can I use prompt-sync with import?

Caracalla answered 22/1, 2021 at 20:15 Comment(0)
T
14

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);
Tocci answered 22/1, 2021 at 20:47 Comment(0)
D
-2

i have done following steps and it work for me

  1. npm i prompt-sync

  2. npm i --save-dev @types/prompt-sync

  3. npm i --save-dev @types/node

  4. use following code in my program

    import PromptSync = require("prompt-sync");
    var prompt = require('prompt-sync')();
    let x = prompt("How old are you?");
    

and it works for me

Durkin answered 8/3, 2023 at 8:31 Comment(1)
This isn't improper code but it doesn't answer the question which seeks to use ES Module syntax.Tocci

© 2022 - 2025 — McMap. All rights reserved.