fs.copyFileSync is not a function

by||1 min read

Recently I ran into the error "fs.copyFileSync is not a function". In this article I will cover how I solved it and what are the reasons for this error.

This error came up when I was developing a JavaScript app and used esbuild for this project. In detail when I installed the plugin "copy static files":

const copyStaticFiles = require("esbuild-copy-static-files");

My setup was including a nix shell with the nodeJS version 18. After a little research it was clear that my nodeJS version was too old, since copyFileSync was introduces later. But how was this possible if I pinned down nodeJS version to 18, which is (at the time of writing) one of the latest version?

The problem was, that pinning down the nodeJS version is not enough. I was using yarn to run my script. Despite my assumption yarn was not part of my nix-shell. Hence, when running my script, I was using the systems yarn. Which then used an older version of nodeJS.

To solve this problem I just hat to add yarn to my shell.nix file by writing pkgs.yarn. This way, yarn and nodeJS were now again matching together and my script behaved as expected.

Thank you for reading this far! Let’s connect. You can @ me on Twitter (@debilofant) with comments, or feel free to follow. Please like/share this article so that it reaches others as well.

© Copyright 2022 - Ersocon - All rights reservedVer.