简体   繁体   中英

How come this code generates crazy output in Node.js with learnyounode

I'm trying to learn Node.js with 'Learnyounode' in Terminal on OSX. When I run the code below with 'learnyounode run programfile.js programfile.js' I get the crazy output as shown in the image, and I don't understand why :) Is this the error that is thrown, or some easter egg in learnyounode?

The program below finds the number of newlines in a file that's input as argument when running the program with $> learnyounode run programfile.js programfile.js

var fs = require('fs') // adds fs module
var src = process.argv[2] 
var data

fs.readFile(src, 'utf8', function(err, data){
    if (err) throw err
    console.log(data)
    var lines = data.split('\n').length - 1
    console.log(lines)
})

If you don't now about learnyounode, see #learnyounode on nodeschool.io

疯狂的node.js在Dropbox上输出为PNG

Thanks for enlighting me.

The code in the screenshot and the code you pasted in differ. There is a comma missing in the screenshot.

Code both here and in the screenshot on the right hand side:

fs.readFile(src, 'utf8', function(err, data){

Screenshot on the left hand side:

fs.readFile(src 'utf8', function(err, data){
               ^ missing comma

So the screenshot version is letting you know that it sees an unexpected string 'utf' because there isn't a comma after src .

Were you executing a version of the file that had the error by mistake?

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