Hello all, I wanted to ask if there is a reason wh...
# help
h
Hello all, I wanted to ask if there is a reason why, for local development, the
externalModules
property in the function bundle definition is not applied. For example here: https://github.com/serverless-stack/serverless-stack/blob/master/packages/cli/scripts/start.js#L768 To get the esbuild external modules, the externalModules property is not taken into account.
Copy code
async function getEsbuildExternal(srcPath) {
  let externals;

  try {
    const packageJson = await fs.readJson(path.join(srcPath, "package.json"));
    externals = Object.keys({
      ...(packageJson.dependencies || {}),
      ...(packageJson.devDependencies || {}),
      ...(packageJson.peerDependencies || {}),
    });
  } catch (e) {
    logger.warn(`No package.json found in ${srcPath}`);
    externals = [];
  }

  // Always include "aws-sdk" in externals
  // Note: this helps with the case where "aws-sdk" is not listed in the srcPath's
  //       package.json. It could be in parent directories' package.json.
  //
  //       Example 1: the SST app is a package inside a yarn workspace, and
  //                  "aws-sdk" is in repo root's package.json.
  //       Example 2: the SST app is at the repo root, but the Lambda function has
  //                  a srcPath. And "aws-sdk" is in repo root's package.json.
  //
  //       The long term fix is to run `esbuild` and if the input files contain
  //       "node_modules/XYZ", kill the esbuild service. And remember "XYZ". And
  //       the next time the function gets invoked, start a new esbuild process,
  //       and set "XYZ" as an external. Need to check other packages in the Yarn
  //       workspace do not show up as "node_modules" in the input files. Because
  //       we want them to be included in input files and watch them.
  if (!externals.includes("aws-sdk")) {
    externals.push("aws-sdk");
  }

  return externals;
}
For context, we are trying to use
pg
to connect to postgresql but sst start complains about missing
pg-native
which is an optional dependency and is not needed. This does not get solved after adding
pg-native
to externalModules in the function’s bundle properties.
f
Hey @Haseeb Naseem, if you open up
node_modules/@serverless-stack/cli/scripts/start.js
, find the
getEsbuildExternal()
funciton.
And above:
Copy code
return externals;
add
Copy code
externals.push("pg-native");
Give it a try and let me know if that works for you.
t
For local development currently we bundle very differently than for production. I'm actually working on getting it to be the exact same so that there's fewer discrepancies
That said you shouldn't have issues with running pg locally - I've done this
h
This does work.
Copy code
externals.push("pg-native");
@thdxr can you tell me what i’m doing wrong otherwise. Or do i need to do something to make it work ? I
t
You just need to make sure pg is in your package.json and that will work for local development
h
That is what i am doing but it complains about pg-native
f
hmm… if
pg-native
is an optional dependency and not required in ur code, why is esbuild trying to bundle it?
@thdxr do you also have
pg-native
in your
package.json
by any chance?
Wonder why it’s not working for @Haseeb Naseem..
t
I don't, just pg works
h
I figured out why it wasn’t working. We have a monorepo with a fat lambda approach, and were adding the dependency to the root of the repo instead of the src dir of the lambda. Silly mistake on our part. That being said, if the local deployment works the same like the actual one, that would be a good thing imo.
t
Yep makes sense, I just got the basics of that working last night. Probably another week of tweaks before release