简体   繁体   中英

Error: Cannot find module 'readline-sync' : Node.js

I have never used node.js before, and I have been researching the answer to this question, but I have had no luck.

I am trying to allow the user to enter an input number (and honestly have no idea how to do this). Upon some research I tested a very simple input/output code:

var readline = require('readline-sync');

var term1 = readline.question("Please enter your first binary term: ");

console.log("your number : " + term1);

But when I try this I am getting the error:

module.js:550
throw err;
^

Error: Cannot find module 'readline-sync'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/<name>/eclipse-workspace/319/SE319Assignment4/binaryCalc.js:1:78)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)

I was wondering if anyone can help me figure out why readline-sync cannot be read, or if there is a better way to write this simple input/output code in general! Thanks

if you don't have the dependency listed in your package.json file, try running this command from your cmd (cd into the directory where binaryCalc.js is located at):

npm install --save readline-sync

if you do have it in there, just try

npm install

Yes you should create a dependency first So for that just open your folder where your js file is there through terminal. And just follow the following steps. . npm init . npm install readline-sync --save and after this just try to run your code It will definately not show any error

If you don't have the 'readline-sync' dependency listed in your package.json file, you can try installing it by running the following command from your terminal or command prompt (while in the directory where your project is located):

npm install readline-sync

Or

npm install -D readline-sync @types/readline-sync

Or install it manually

"readline-sync": "^1.4.9"

then run the following command

npm install

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM