admin管理员组

文章数量:1405324

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?

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?

Share Improve this question edited Feb 20, 2012 at 23:47 mu is too short 435k71 gold badges859 silver badges818 bronze badges asked Feb 20, 2012 at 23:20 grauturgrautur 30.5k34 gold badges96 silver badges129 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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.

本文标签: javascriptReading a local file with CoffeescriptStack Overflow