Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: fs.readFileAsync is not a function #1912

Closed
ronkot opened this issue Aug 28, 2024 · 5 comments · Fixed by #1917
Closed

TypeError: fs.readFileAsync is not a function #1912

ronkot opened this issue Aug 28, 2024 · 5 comments · Fixed by #1917

Comments

@ronkot
Copy link

ronkot commented Aug 28, 2024

This is a Bug Report

Description

When I run serverless package, I get error fs.readFileAsync is not a function and build fails:

⠹ Packaging (and 1 more task)^R

✖ fs.readFileAsync is not a function
TypeError: fs.readFileAsync is not a function
    at ServerlessWebpack.getFileContentAndStat (/PROJECT/node_modules/serverless-webpack/lib/packageModules.js:93:8)
    at /PROJECT/node_modules/serverless-webpack/lib/packageModules.js:57:74
    at arrayMap (/PROJECT/node_modules/lodash/lodash.js:653:23)
    at Function.map (/PROJECT/node_modules/lodash/lodash.js:9622:14)
    at WriteStream.<anonymous> (/PROJECT/node_modules/serverless-webpack/lib/packageModules.js:57:23)
    at WriteStream.emit (node:events:517:28)
    at WriteStream.emit (node:domain:489:12)
    at stream.emit (node:internal/fs/streams:61:9)
    at file:///Users/me/.serverless/releases/4.2.4/package/dist/sf-core.js:3:5724
    at file:///Users/me/.serverless/releases/4.2.4/package/dist/sf-core.js:3:6335
    at FSReqCallback.oncomplete (node:fs:200:23)

The error seems to come from this code block inside serverless-webpack:

  return BbPromise.all([
    // Get file contents and stat in parallel
    fs.readFileAsync(fullPath),
    fs.statAsync(fullPath)
  ]).then(...)

As far as I could figure this out, the fs.readFileAsync is not part of Node's fs module but that kind of (*Async) functions are created by "promisifying" a callback-based function. However, such promisifying is not done inside this package.

If I add BbPromise.promisifyAll(fs); below the imports in file packageModules.js, the build process succeeds.

I don't understand how this package works for other people if such promisifying step is missing.

Additional Data

  • Serverless-Webpack Version you're using: 5.14.1
  • Webpack version you're using: 5.94.0
  • Serverless Framework Version you're using: 4.2.4
  • Operating System: OSX 14.6.1
@ronkot
Copy link
Author

ronkot commented Aug 28, 2024

@j0k3r
Copy link
Member

j0k3r commented Aug 28, 2024

Well the package is working well when using Serverless < 4. Maybe that's a problem related with Serverless.
I still not got a chance to find time to see how serverless-webpack behave on Serverless v4.

What version of node are you using btw?

@ronkot
Copy link
Author

ronkot commented Aug 28, 2024

My node is v18.18.2

@ronkot
Copy link
Author

ronkot commented Aug 28, 2024

Well the package is working well when using Serverless < 4. Maybe that's a problem related with Serverless. I still got a chance to find time to see how serverless-webpack behave on Serverless v4.

Yeah, maybe serverless < 4 did promisifyAll(fs) and serverless-webpack relied on that assumption..? If so, now serverless-webpack should promisify fs by itself.

@enchorb
Copy link

enchorb commented Sep 2, 2024

+1 thanks for #1917 patch is working, lets get this merged in

diff --git a/lib/packageModules.js b/lib/packageModules.js
index 650a3cf24e7ea5922aca39d6430f1cf5647acedb..7fffcdf974c01c0cee44f165de5874e795aa2db4 100644
--- a/lib/packageModules.js
+++ b/lib/packageModules.js
@@ -9,6 +9,9 @@ const semver = require('semver');
 const fs = require('fs');
 const { getAllNodeFunctions, isProviderGoogle } = require('./utils');

+const readFileAsync = BbPromise.promisify(fs.readFile);
+const statAsync = BbPromise.promisify(fs.stat);
+
 function setArtifactPath(funcName, func, artifactPath) {
   const version = this.serverless.getVersion();

@@ -90,8 +93,8 @@ function getFileContentAndStat(directory, filePath) {

   return BbPromise.all([
     // Get file contents and stat in parallel
-    fs.readFileAsync(fullPath),
-    fs.statAsync(fullPath)
+    readFileAsync(fullPath),
+    statAsync(fullPath)
   ]).then(
     result => ({
       data: result[0],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants