Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeprecationWarning: Calling an asynchronous function without callback is deprecated. in fs.writeFile #14770

Closed
thidasapankaja opened this issue Aug 11, 2017 · 10 comments
Labels
fs Issues and PRs related to the fs subsystem / file system. question Issues that look for answers.

Comments

@thidasapankaja
Copy link

Version: v8.0.0
Platform: Linux pankaja-HP-Pavilion-15-Notebook-PC 4.10.0-30-generic #34~16.04.1-Ubuntu SMP Wed Aug 2 02:13:56 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
Subsystem: fs.writeFile

Error : (node:10801) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.

Code :

var http = require('http');
var fs = require('fs');

var server = http.createServer(function(req, res){

	fs.readFile("text.txt",'utf-8',function(error, data){
		res.writeHead(200,{
			'Content-Type': 'tex/plain'
		});

		data = parseInt(data) + 1;

		fs.writeFile("text.txt", data);

		res.end('This page was refreshed ' + data + ' times!');
	})
});

server.listen(8080, function(){
	console.log('your server is running at 127.0.0.1:8080')
})

I was following this tutorial and when Reading and Writing files I'm getting this error. When I use node --trace-deprecation file.js I'm getting this log message.

(node:10880) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
at maybeCallback (fs.js:119:42)
at Object.fs.writeFile (fs.js:1256:14)
at /media/pankaja/Local Disk/Projects/Nodejs/Tutorial/file.js:14:6
at tryToString (fs.js:499:3)
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:487:12)

Is this a bug ?

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Aug 11, 2017

Hi, @pankaja92. The tutorial is a bit outdated (5 years old) in that fs asynchronous functions now demand a callback. You can fix your code by updating this line:

fs.writeFile("text.txt", data);

by this one:

fs.writeFile("text.txt", data, (error) => { /* handle error */ });

see https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback

@vsemozhetbyt vsemozhetbyt added the fs Issues and PRs related to the fs subsystem / file system. label Aug 11, 2017
@mscdex mscdex added the question Issues that look for answers. label Aug 11, 2017
@mscdex mscdex closed this as completed Aug 11, 2017
@tommedema
Copy link

@vsemozhetbyt but why use a callback when one can use async/await with try catch?

@TimothyGu
Copy link
Member

@tommedema Node.js fs module functions do not return a promise, and cannot be used with async/await try-catch by default. You can convert the functions to become promise-returning though with the use of util.promisify.

@jacobq
Copy link
Contributor

jacobq commented Mar 9, 2018

Alternatively, if you want to use promises / async functions you can use fs-extra like this.

@quyle1710
Copy link

@vsemozhetbyt Thank you for this answer :)

@jasnell
Copy link
Member

jasnell commented Apr 11, 2018

Starting in Node.js 10.0.0, there are experimental promise versions of the fs functions available at fs.promises

@flyher
Copy link

flyher commented Apr 13, 2018

@vsemozhetbyt
Thank you for the answer!

@Sai0407
Copy link

Sai0407 commented Apr 24, 2018

Can any one help me imagemin
(node:13696) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.

@cloudemp04
Copy link

i also have the same problem heres my gulpfile code

'use strict';

var gulp = require('gulp');
var concat = require('gulp-concat');
var merge = require('merge-stream');
var browserSync = require('browser-sync').create();
var sass = require('gulp-sass');
var plumber = require('gulp-plumber');
var uglify = require('gulp-uglify');

// Static Server + watching scss/html files
gulp.task('serve', ['sass','js'], function() {

browserSync.init({
	server: "."
});

gulp.watch([
		   "assets/scss/utils/*.scss",
		   "assets/scss/components/*.scss",
		   "assets/scss/base/*.scss",
		   "assets/scss/layouts/*.scss",
		   'assets/gulp-js/*.js'
		   ], ['sass','js']);
gulp.watch("*.html").on('change', browserSync.reload);

});

// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', function() {
return gulp.src([
"assets/scss/utils/.scss",
"assets/scss/components/
.scss",
"assets/scss/base/.scss",
"assets/scss/layouts/
.scss"
])
.pipe(concat('assets/css/style.css'))
.pipe(plumber({
handleError: function(err) {
console.log(err);
this.emit('end');
}
}))
.pipe(sass({outputStyle : 'compressed'}))
.pipe(gulp.dest("./"))
.pipe(browserSync.stream());
});

gulp.task('js', function() {
return gulp.src('assets/gulp-js/*.js')
.pipe(concat("index.js"))
.pipe(uglify())
.pipe(gulp.dest('assets/js/'))
.pipe(browserSync.stream());
});

gulp.task('default', ['serve']);

anyone can help me?

@joyeecheung
Copy link
Member

joyeecheung commented Apr 26, 2018

If you encounter a deprecation warning but do not know what code causes it (usually because it's code in your dependency), you can use the --trace-warnings command line flag to see the stack trace and find out which line of code is using a deprecated API. If it's in your dependency, try either upgrading it or opening a PR for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests