简体   繁体   中英

node deprecation warning: Calling an asynchronous function without callback is deprecated

I have a problem trying to run my project on localhost:8080 with reload. So when I try and start my project on localhost it gives me this error:

Deprecation Error

This is not a problem with my project, this exact project was working last week and now none of my projects can run properly. To make sure it wasn't my project I went into the file explorer and opened the index.html file with chrome and it only ran the index.html none of my script being brought in were working. I don't know what to do to fix this. If anyone can help that would be great.

Going back to the previous version works, in the mean time, but will not solve the problem in the long run, and you'll not get the benefits of new fixes, updates, and upgrades.

One example that would cause that warning when called is the function

fs.readFile(path[, options], callback)

Without the callback, the warning would appear.

To prevent the warning and implement the callback would be like for example:

const fs = require('fs');
fs.readFile('./good.txt', 'utf8', function read(err, data) {
        if (err) {
            throw err;
        }
        content = data;
        // call the next step here
        console.log(content); // content display
    it
    });

the callback function read(), does two things,

  1. it gets the content of the data based on the encoding, in this example specified as utf8.
  2. And then capture and throw the error(err) if there is any.

Note: encoding is necessary, if you want the file to be human-readable

I had node v7.8.0 installed which was causing me problems. I uninstalled that version and installed node v6.10.1 which fixed this weird issue.

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