Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Log all fs.write* operations with a stack trace in node.js
// Stick this in the top of your main app.js/server.js/whatever file.
// Any time one of the below fs.* methods are called anywher in the app or dependencies,
// this code will log the details including the method, arguments, and a stack trace.
// It will then complete the write as normal.
var fs = require('fs');
['write','writeSync','writeFile','writeFileSync', 'appendFile', 'appendFileSync','createWriteStream'].forEach(function(fn){
fs['real'+fn] = fs[fn];
fs[fn] = function() {
var e = new Error();
console.log('fs.'+fn+' called with ' +, ', ') + ' Stack trace is:');
console.log(e.stack.split('\n').slice(2).join('\n')); // slice out the empty error and reference to this wrapper
return fs['real'+fn].apply(fs, arguments);
fs[fn].name = fn;

This comment has been minimized.

Copy link
Owner Author

@nfriedly nfriedly commented Aug 5, 2015

Note: this is now available in slightly-enhanced form as an npm module:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment