abhijeetps
abhijeetps

Reputation: 5700

learnyounode: #3 My First I/O

I am trying this in Visual Studio Code on Windows 10 with powershell.exe as Terminal.

After many failures, I peaked in the internet to find its solution.

Here's the solution that I have got from the internet:

var fs = require('fs');
       
var contents = fs.readFileSync(process.argv[2]);
var lines = contents.toString().split('\n').length - 1;
console.log(lines);

I saved it in a file, myFirstIO.js. However, I tried running it on PowerShell, I received the following error:

PS C:\Users\aps12\Desktop\Test\lyn> node myFirstIO.js
fs.js:640
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

TypeError: path must be a string or Buffer
    at TypeError (native)
    at Object.fs.openSync (fs.js:640:18)
    at Object.fs.readFileSync (fs.js:508:33)
    at Object.<anonymous> (C:\Users\aps12\Desktop\Test\lyn\myFirstIO.js:3:19)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)

But when typed,

PS C:\Users\aps12\Desktop\Test\lyn> learnyounode verify myFirstIO.js

Surprisingly, solution got passed.

 # PASS Your solution to MY FIRST I/O! passed!

I wonder why the solution get passed even when it was not running. Also, why the solution was not running? What is the mistake I have made there? PS: I have already cleared first two modules of learnyounode without any failure.

Upvotes: 0

Views: 333

Answers (2)

azs06
azs06

Reputation: 3517

If you run this program using node myFirstIO.js it will throw an error, because it expects a file as second argument

/*
on this line, file being read using readFileSync and saving it to a
contents variable
*/
var contents = fs.readFileSync(process.argv[2]);

If you notice the error TypeError: path must be a string or Buffer, it expecting a file path string or file stream as second argument.

When you are running this program with learnyounode, they implicitly running it with a file, you can also test this by running it like node myFirstIO.js textfile.txt Note, file path must be valid.

If you run this program like node myFirstIO.js "Hello World" it won't work, as it expects a file path or stream not a string. Hope this helps you understand this issue.

Upvotes: 1

user1678933
user1678933

Reputation:

process.argv returns all arguments given in command line.

When you do > node myFirstIO.js, you're passing these arguments to process.argv:

['node', 'myFirstIO.js']

... It threw an error cuz you were asking for the third argument (process.argv[2]), which you didn't pass in the command line.

Read about process.argv here.

Now, for learnyounode, either I assume you passed the third argument when you used:

> learnyounode verify myFirstIO.js

using the JS file as third argument; or learnyounode is buggy.

Upvotes: 0

Related Questions