serverless-webpack icon indicating copy to clipboard operation
serverless-webpack copied to clipboard

TypeError: fs.readFileAsync is not a function

Open ronkot opened this issue 1 year ago • 4 comments

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 avatar Aug 28 '24 13:08 ronkot

There's also a SO question about this issue: https://stackoverflow.com/questions/78888120/serverless-webpack-giving-error-while-deploying-on-lambda

ronkot avatar Aug 28 '24 13:08 ronkot

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?

j0k3r avatar Aug 28 '24 13:08 j0k3r

My node is v18.18.2

ronkot avatar Aug 28 '24 13:08 ronkot

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.

ronkot avatar Aug 28 '24 13:08 ronkot

+1 thanks for https://github.com/serverless-heaven/serverless-webpack/pull/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],

enchorb avatar Sep 02 '24 23:09 enchorb