OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Node.js fs.existsSync returning false postives

  • Thread starter Thread starter Ethan
  • Start date Start date
E

Ethan

Guest
I have the following code:

Code:
  const fs = await import("fs");
    let hashedPassword;

    if (!fs.existsSync(".fs")) {
      fs.mkdirSync(".fs");
      console.info("No cache found. Creating .fs");
    }

    if (!fs.existsSync(".fs/token.txt")) {
      console.info("No token found, hashing a new one");
      hashedPassword = bcypt.hashSync(
        password || (process.env.UNIVERSAL_FS_PASSWORD as string),
        10
      );

      fs.writeFileSync(".fs/token.txt", hashedPassword);
    }

    // TODO: add correct url
    const response = await fetch("http://localhost:3000/", {
      headers: {
        Authorization: `Bearer ${hashedPassword}`
      }
    });

    if (response.status === 401) {
      console.error("Failed to authenticate. Check your password");
    } else if (response.status === 200) {
      console.info("Authenticated");
    } else if (response.status === 500) {
      console.info("Something else went wrong. Check your server logs");
    }

It checks if the .fs folder exists. If it does than the token is used for auth. If not it hashes a new one. The fs folder does not exists but when consoling the same fs.existsSync I get the result true. However it does not exists.

Running the ls command where the exists call is checking shows that no .fs folder exists:

Code:
README.md         eslint.config.js  package.json      rollup.config.mjs tests             types
dist              node_modules      pnpm-lock.yaml    src               tsconfig.json     vite.config.ts

To get more info I consoled fs.stat and this was the result:

Code:
Stats {
  dev: 16777232,
  mode: 33188,
  nlink: 1,
  uid: 501,
  gid: 20,
  rdev: 0,
  blksize: 4096,
  ino: 52057046,
  size: 60,
  blocks: 8,
  atimeMs: 1719445593768.046,
  mtimeMs: 1719445593768.1392,
  ctimeMs: 1719445593768.1392,
  birthtimeMs: 1719445593768.046
}

It seems Node still thinks the dir and file exist on my system. Envinfo:

Code:
  System:
    OS: macOS 14.5
    CPU: (12) arm64 Apple M2 Max
    Memory: 8.82 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.3.0 - /opt/homebrew/bin/node
    npm: 10.8.1 - /opt/homebrew/bin/npm
    pnpm: 9.4.0 - ~/Library/pnpm/pnpm
    bun: 1.1.17 - ~/.bun/bin/bun
  Managers:
    Homebrew: 4.3.7 - /opt/homebrew/bin/brew
    pip3: 24.0 - /opt/homebrew/bin/pip3
    RubyGems: 3.0.3.1 - /usr/bin/gem
  Utilities:
    Make: 3.81 - /usr/bin/make
    GCC: 15.0.0 - /usr/bin/gcc
    Git: 2.39.3 - /usr/bin/git
    Clang: 15.0.0 - /usr/bin/clang
    FFmpeg: 7.0.1 - /opt/homebrew/bin/ffmpeg
    Curl: 8.6.0 - /usr/bin/curl
    OpenSSL: 3.3.1 - /opt/homebrew/bin/openssl
  Servers:
    Apache: 2.4.58 - /usr/sbin/apachectl
  Virtualization:
    Docker: 26.1.4 - /usr/local/bin/docker
    Docker Compose: 2.27.1 - /usr/local/bin/docker-compose
    Parallels: 19.3.0 - /usr/local/bin/prlctl
  SDKs:
    iOS SDK:
      Platforms: DriverKit 23.5, iOS 17.5, macOS 14.5, tvOS 17.5, visionOS 1.2, watchOS 10.5
  IDEs:
    VSCode: 1.90.2 - /usr/local/bin/code
    Vim: 9.0 - /usr/bin/vim
    Xcode: 15.4/15F31d - /usr/bin/xcodebuild
  Languages:
    Bash: 3.2.57 - /bin/bash
    Java: 21.0.2 - /usr/bin/javac
    Perl: 5.34.1 - /usr/bin/perl
    Python3: 3.12.3 - /opt/homebrew/bin/python3
    Ruby: 2.6.10 - /usr/bin/ruby
  Databases:
    SQLite: 3.43.2 - /usr/bin/sqlite3
  Browsers:
    Safari: 17.5

<p>I have the following code:</p>
<pre><code> const fs = await import("fs");
let hashedPassword;

if (!fs.existsSync(".fs")) {
fs.mkdirSync(".fs");
console.info("No cache found. Creating .fs");
}

if (!fs.existsSync(".fs/token.txt")) {
console.info("No token found, hashing a new one");
hashedPassword = bcypt.hashSync(
password || (process.env.UNIVERSAL_FS_PASSWORD as string),
10
);

fs.writeFileSync(".fs/token.txt", hashedPassword);
}

// TODO: add correct url
const response = await fetch("http://localhost:3000/", {
headers: {
Authorization: `Bearer ${hashedPassword}`
}
});

if (response.status === 401) {
console.error("Failed to authenticate. Check your password");
} else if (response.status === 200) {
console.info("Authenticated");
} else if (response.status === 500) {
console.info("Something else went wrong. Check your server logs");
}
</code></pre>
<p>It checks if the .fs folder exists. If it does than the token is used for auth. If not it hashes a new one. The fs folder does not exists but when consoling the same <code>fs.existsSync</code> I get the result true. However it does not exists.</p>
<p>Running the ls command where the exists call is checking shows that no <code>.fs</code> folder exists:</p>
<pre class="lang-bash prettyprint-override"><code>README.md eslint.config.js package.json rollup.config.mjs tests types
dist node_modules pnpm-lock.yaml src tsconfig.json vite.config.ts
</code></pre>
<p>To get more info I consoled <code>fs.stat</code> and this was the result:</p>
<pre class="lang-js prettyprint-override"><code>Stats {
dev: 16777232,
mode: 33188,
nlink: 1,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 52057046,
size: 60,
blocks: 8,
atimeMs: 1719445593768.046,
mtimeMs: 1719445593768.1392,
ctimeMs: 1719445593768.1392,
birthtimeMs: 1719445593768.046
}
</code></pre>
<p>It seems Node still thinks the dir and file exist on my system. Envinfo:</p>
<pre><code> System:
OS: macOS 14.5
CPU: (12) arm64 Apple M2 Max
Memory: 8.82 GB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.3.0 - /opt/homebrew/bin/node
npm: 10.8.1 - /opt/homebrew/bin/npm
pnpm: 9.4.0 - ~/Library/pnpm/pnpm
bun: 1.1.17 - ~/.bun/bin/bun
Managers:
Homebrew: 4.3.7 - /opt/homebrew/bin/brew
pip3: 24.0 - /opt/homebrew/bin/pip3
RubyGems: 3.0.3.1 - /usr/bin/gem
Utilities:
Make: 3.81 - /usr/bin/make
GCC: 15.0.0 - /usr/bin/gcc
Git: 2.39.3 - /usr/bin/git
Clang: 15.0.0 - /usr/bin/clang
FFmpeg: 7.0.1 - /opt/homebrew/bin/ffmpeg
Curl: 8.6.0 - /usr/bin/curl
OpenSSL: 3.3.1 - /opt/homebrew/bin/openssl
Servers:
Apache: 2.4.58 - /usr/sbin/apachectl
Virtualization:
Docker: 26.1.4 - /usr/local/bin/docker
Docker Compose: 2.27.1 - /usr/local/bin/docker-compose
Parallels: 19.3.0 - /usr/local/bin/prlctl
SDKs:
iOS SDK:
Platforms: DriverKit 23.5, iOS 17.5, macOS 14.5, tvOS 17.5, visionOS 1.2, watchOS 10.5
IDEs:
VSCode: 1.90.2 - /usr/local/bin/code
Vim: 9.0 - /usr/bin/vim
Xcode: 15.4/15F31d - /usr/bin/xcodebuild
Languages:
Bash: 3.2.57 - /bin/bash
Java: 21.0.2 - /usr/bin/javac
Perl: 5.34.1 - /usr/bin/perl
Python3: 3.12.3 - /opt/homebrew/bin/python3
Ruby: 2.6.10 - /usr/bin/ruby
Databases:
SQLite: 3.43.2 - /usr/bin/sqlite3
Browsers:
Safari: 17.5
</code></pre>
 

Latest posts

Top