I am trying to take under input in nodejs using prompt but it shows me the following error:
node:internal/modules/cjs/loader:998
throw err;
^
Error: Cannot find module 'prompt-sync'
Require stack:
- D:\Code\C \Concepts\conditionals.js
at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
at Module._load (node:internal/modules/cjs/loader:841:27)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (D:\Code\C \Concepts\conditionals.js:1:16)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'D:\\Code\\C \\Concepts\\conditionals.js' ]
}
I have installed 'prompt-sync' using npm but the issue still persists. This is my code:
const prompt = require('prompt-sync')();
let num = prompt("Enter your age: ");
console.log(typeof num);
I have tried restarting my pc and updated nodejs to its latest version(currently 9.2.0).
Any kind of help will be appreciated.
CodePudding user response:
Clean your installed node modules like so:
npm cache verify
rm -rf node_modules
rm package-lock.json
npm i
rm works if you are under linux or gitbash on Windows. If not delete the node_modules folder and the package.lock.json. (not the package.json)
When executing the npm i, take a look if error messages comes. Not all npm packages are combatiple with all node versions.
Controll in your package.json if the prompt-sync module is in the dependency section. If not reinstall it. You need to be inside your project folder when you install a module.