Home > fs statsync error > fs.statsync error

Fs.statsync Error

Contents

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this

Statsync Example

site About Us Learn more about Stack Overflow the company Business Learn fs.accesssync example more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x node fs access Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up fs.statSync

Npm File Exists

throws an error when contained in a function? up vote 1 down vote favorite 2 I'm making a function that returns a boolean for when a file exists or not, using fs.statSync. It looks like this: function doesExist (cb) { let exists try { fs.statSync('./cmds/' + firstInitial + '.json') exists = true } catch (err) { exists = err && err.code === 'ENOENT' ?

Fs.access Example

false : true } cb(exists) } Example use case: let fileExists doesExist('somefile.json', function (exists) { fileExists = exists }) However, running the code throws me a TypeError: string is not a function. I have no idea why. javascript node.js exists fs share|improve this question asked Mar 28 '15 at 11:24 jona 10911 Your doesExist function hasn't declared the firstInitial parameter in its parameter list: function doesExist(firstInitial, cb) { –c.P.u1 Mar 28 '15 at 11:31 alright, that works –jona Mar 28 '15 at 11:42 1 WTH are you trying to use a callback when you are using a *sync function? –Bergi Mar 28 '15 at 13:17 add a comment| 1 Answer 1 active oldest votes up vote 3 down vote accepted I think you want to remove that callback, and add the file name to your parameters: function doesExist(firstInitial) { try { fs.statSync('./cmds/' + firstInitial + '.json') return true } catch(err) { return !(err && err.code === 'ENOENT'); } } let fileExists = doesExist('somefile'); Btw, there is also fs.exists. share|improve this answer edited Mar 28 '15 at 13:27 answered Mar 28 '15 at 13:21 Bergi 21

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn stat vs lstat more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags

Node Create File If Not Exists

Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, fs.statsync try catch helping each other. Join them; it only takes a minute: Sign up Check synchronously if file/directory exists in Node.js up vote 420 down vote favorite 99 How can I synchronously check, using node.js, if a file or directory http://stackoverflow.com/questions/29316550/fs-statsync-throws-an-error-when-contained-in-a-function exists? node.js share|improve this question edited Jan 21 '14 at 19:29 Afzaal Ahmad Zeeshan 10.2k62458 asked Dec 19 '10 at 11:19 Ragnis 2,98241428 5 Worth noting in most cases you don't want to check synchronously. –mikemaccana Nov 19 '15 at 16:00 5 Synchronous operations are great for performing one-time file/directory operations before returning a module. For example, bootstrapping a configuration file. –jocull Jul 13 at 19:17 add a comment| 9 Answers 9 active oldest http://stackoverflow.com/questions/4482686/check-synchronously-if-file-directory-exists-in-node-js votes up vote 763 down vote accepted The answer to this question has changed over the years, it currently consists of: Original answer from 2010 (stat/statSync or lstat/lstatSync) Update September 2012 (exists/existsSync, now apparently being deprecated) Update February 2015 (Noting impending deprecation, so we're probably back to stat/statSync or lstat/lstatSync) Update December 2015 (There's also fs.access(path, fs.F_OK, function(){}) / fs.accessSync(path, fs.F_OK), but note that if the file/directory doesn't exist, it's an error) Here they are in chronological order: Original answer from 2010: You can use statSync or lstatSync (docs link), which give you an fs.Stats object. In general, if a synchronous version of a function is available, it will have the same name as the async version with Sync at the end. So statSync is the synchronous version of stat; lstatSync is the synchronous version of lstat, etc. lstatSync tells you both whether something exists, and if so, whether it's a file or a directory (or in some file systems, a symbolic link, block device, character device, etc.), e.g. if you need to know if it exists and is a directory: var fs = require('fs'); try { // Query the entry stats = fs.lstatSync('/the/path'); // Is it a directory? if (stats.isDirectory()) { // Yes it is } } catch (e) { // ... } ...and similarly if it's a file, there's isFile; if it's a block de

without using fs.exists Anyone who has spent any time writing Node has most likely used the file system module. One of the methods in this core module though is not only different from all the others, but according to most experts is not https://www.gregjs.com/javascript/2016/checking-whether-a-file-directory-exists-without-using-fs-exists/ even useful at all, despite its popularity with a host of other developers. Because https://nodejs.org/api/fs.html of this, the method has been deprecated for years now. I talk, of course, about fs.exists (and fs.existsSync). For some people, it’s a very contentious issue - see for example this issue thread or this one. The reason for its deprecation (way back in the iojs days of yore) is that, unlike all other file system fs.statsync error methods, it doesn’t use the canonical nodeback API. Furthermore, its main use case scenario, checking whether a file exists before opening, is actually an antipattern and should be replaced by just trying to open the file and possibly handling the error. But despite its long-standing (in JS terms at least) deprecation, tons of people still continue to use fs.exists, and some newcomers or relative newcomers are surely confused about what node fs access to use instead. At least, I myself was when I first ran into this issue. While I agree that the method is often superfluous, and sometimes entirely pointless (checking whether a file exists before doing something with that file is not helpful when race conditions may occur and the file may be deleted in the interim), I and I am sure many others would argue that there are legitimate use cases for this method, or at least for what this method should have been, had it been designed better. So if you are looking for a replacement for fs.exists(Sync), read on, for what follows is a short discussion of how to check whether a file or directory exists without using this deprecated method. Synchronous existsFar be it from me to recommend synchronously manipulating the file system, but hey, it is more straightforward so let’s start with the easy stuff here. The official fs documentation recommends using fs.statSync or fs.accessSync instead, so let’s go over both. fs.accessSyncThis method checks the accessibility of a file and nothing more or less. It throws an error if the file is not accessible and does absolutely nothing if it is. So, let’s say you want to check for the existence of myDir, r

Query Strings Readline REPL Stream String Decoder Timers TLS/SSL TTY UDP/Datagram URL Utilities V8 VM ZLIB GitHub Repo & Issue Tracker Mailing List Node.js v6.8.1 Documentation Index | View on single page | View as JSON Table of Contents File System Buffer API Class: fs.FSWatcher Event: 'change' Event: 'error' watcher.close() Class: fs.ReadStream Event: 'open' Event: 'close' readStream.bytesRead readStream.path Class: fs.Stats Stat Time Values Class: fs.WriteStream Event: 'open' Event: 'close' writeStream.bytesWritten writeStream.path fs.access(path[, mode], callback) fs.accessSync(path[, mode]) fs.appendFile(file, data[, options], callback) fs.appendFileSync(file, data[, options]) fs.chmod(path, mode, callback) fs.chmodSync(path, mode) fs.chown(path, uid, gid, callback) fs.chownSync(path, uid, gid) fs.close(fd, callback) fs.closeSync(fd) fs.constants fs.createReadStream(path[, options]) fs.createWriteStream(path[, options]) fs.exists(path, callback) fs.existsSync(path) fs.fchmod(fd, mode, callback) fs.fchmodSync(fd, mode) fs.fchown(fd, uid, gid, callback) fs.fchownSync(fd, uid, gid) fs.fdatasync(fd, callback) fs.fdatasyncSync(fd) fs.fstat(fd, callback) fs.fstatSync(fd) fs.fsync(fd, callback) fs.fsyncSync(fd) fs.ftruncate(fd, len, callback) fs.ftruncateSync(fd, len) fs.futimes(fd, atime, mtime, callback) fs.futimesSync(fd, atime, mtime) fs.lchmod(path, mode, callback) fs.lchmodSync(path, mode) fs.lchown(path, uid, gid, callback) fs.lchownSync(path, uid, gid) fs.link(srcpath, dstpath, callback) fs.linkSync(srcpath, dstpath) fs.lstat(path, callback) fs.lstatSync(path) fs.mkdir(path[, mode], callback) fs.mkdirSync(path[, mode]) fs.mkdtemp(prefix[, options], callback) fs.mkdtempSync(prefix[, options]) fs.open(path, flags[, mode], callback) fs.openSync(path, flags[, mode]) fs.read(fd, buffer, offset, length, position, callback) fs.readdir(path[, options], callback) fs.readdirSync(path[, options]) fs.readFile(file[, options], callback) fs.readFileSync(file[, options]) fs.readlink(path[, options], callback) fs.readlinkSync(path[, options]) fs.readSync(fd, buffer, offset, length, position) fs.realpath(path[, options], callback) fs.realpathSync(path[, options]) fs.rename(oldPath, newPath, callback) fs.renameSync(oldPath, newPath) fs.rmdir(path, callback) fs.rmdirSync(path) fs.stat(path, callback) fs.statSync(path) fs.symlink(target, path[, type], callback) fs.symlinkSync(target, path[, type]) fs.truncate(path, len, callback) fs.truncateSync(path, len) fs.unlink(path, callback) fs.unl

 

Related content

No related pages.