Pkg: fs.copyFile and snapshot filesystem

Created on 14 May 2018  路  11Comments  路  Source: vercel/pkg

Trying to copy a file from the snapshot filesystem to the real filesystem results in the following error:
ENOENT: no such file or directory. Seems to happen in all versions after 4.2.4.

Example code that should work:

fs.copyFile(
  path.resolve(__dirname, 'some_file.json'),
  path.resolve(process.cwd(), 'some_new_file.json')
)

Most helpful comment

Another option that works is to use:

const fileBuffer = fs.readFileSync(path.resolve(__dirname, 'some_file.json'))

fs.writeFileSync(self.dynLibraryPath, fileBuffer)

All 11 comments

Using ncp as a temporary workaround since it doesn't rely on the fs.copyFile function.

Another option that works is to use:

const fileBuffer = fs.readFileSync(path.resolve(__dirname, 'some_file.json'))

fs.writeFileSync(self.dynLibraryPath, fileBuffer)

I can confirm your little trick worked out for me o/

Trying to copy a file from the snapshot filesystem to the real filesystem results in the following error:
ENOENT: no such file or directory. Seems to happen in all versions after 4.2.4.

Example code that should work:

fs.copyFile(
  path.resolve(__dirname, 'some_file.json'),
  path.resolve(process.cwd(), 'some_new_file.json')
)

Maybe pkg does not kidnap function fs.copyFile. I meet the same issue, and I resolve it by replace copyFile with readFile & writeFile:

// previously:
function copyFile(sourcePath, targetPath) {
    generatedFilePaths.push(targetPath);
    fs.copyFileSync(sourcePath, targetPath);
}
// now:
function copyFile(sourcePath, targetPath) {
    generatedFilePaths.push(targetPath);
    const sourceData = fs.readFileSync(sourcePath, ENCODING);
    fs.writeFileSync(targetPath, sourceData, ENCODING);
}

What can we do when using fs-extra?

I get the same error when calling a fs.copy, but I would not like to manually rewrite it (to be able to apply those workarounds), since there is a lot of things made on fs-extra.

You need to not use the copy method from the fs-extra package. It must do something with path that pkg intercepts/changes.

It seems to be a regression bug, since it worked fine until 4.2.6.

This issue is still not solved. Any progress on this issue? @igorklopov

Hi, @igorklopov thanks for your great work. However, this issue is indeed a pain in our application and still not fixed for two years. Do you have any plans to get this issue to be solved, or suggest some work rounds. Many thanks and wish you a good day.

We workaround this issue by loading following patch at begin of main module: https://github.com/serverless/serverless/blob/94ff3b22ab13afc60fb4e672520b4db527ee0432/lib/utils/standalone-patch.js

Another option that works is to use:

const fileBuffer = fs.readFileSync(path.resolve(__dirname, 'some_file.json'))

fs.writeFileSync(self.dynLibraryPath, fileBuffer)

this work with pkg 4.4.9
with node14

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bergheim picture bergheim  路  3Comments

Ks89 picture Ks89  路  4Comments

peterjwest picture peterjwest  路  3Comments

erikd picture erikd  路  3Comments

Nisthar picture Nisthar  路  4Comments