Skip to content

Instantly share code, notes, and snippets.

@greemwahr
Created February 17, 2016 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greemwahr/99b1e0196124bbfa97fd to your computer and use it in GitHub Desktop.
Save greemwahr/99b1e0196124bbfa97fd to your computer and use it in GitHub Desktop.
# Process I/O files synchronously in node.js
var fs = require('fs')
var contents = fs.readFileSync(process.argv[2])
var lines = contents.toString().split('\n').length - 1
console.log(lines)
// note you can avoid the .toString() by passing 'utf8' as the
// second argument to readFileSync, then you'll get a String!
//
// fs.readFileSync(process.argv[2], 'utf8').split('\n').length - 1
# Process I/O files asynchronously in node.js
var fs = require('fs')
var file = process.argv[2]
fs.readFile(file, function (err, contents) {
// fs.readFile(file, 'utf8', callback) can also be used
var lines = contents.toString().split('\n').length - 1
console.log(lines)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment