Reading a local file with Coffeescript

Problem

I'm trying to read in a file with Coffeescript. In the same folder where I enter into the coffee repo, I have a file named hello.txt.

coffee> fs = require 'fs'
coffee> x = fs.readFile "hello.txt"
undefined
coffee> x
undefined

What am I doing wrong?

Problem courtesy of: grautur

Solution

You're not passing a callback to readFile to actually read the file. See docs for further information. Generally, nodejs methods are asynchronous because of the asynchronous nature of the platform. For some of them there is a sync version. Indeed, you can read a file with the readFileSync method.

Solution courtesy of: lucapette

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.