3

I use serverless-layers plugin in Serverless Framework so I'd like to exclude node_modules directory from esbuild.

According to the official document, I can set "*" to the exclude option of serverless-esbuild to exclude the full node_modules directory.

An array of dependencies to exclude from the Lambda. This is passed to the esbuild external option. Set to * to disable packaging node_modules ['aws-sdk']

However, it doesn't seem to work if I set it in serverless.ts.
Here was my trials:

exclude: ['*']
exclude: '*'
exclude: ['./node_modules/*']

On the other hand, specifying each library works well like this:

exclude: ['aws-sdk', 'mysql2', '@middy']

Is there something I missed?

Thanks.

Akumachan
  • 81
  • 2
  • 9

1 Answers1

0

esbuild automatically includes node_modules. If you don't want that to be included, you can add esbuild-node-externals plugin.

const { nodeExternalsPlugin } = require("esbuild-node-externals");

esbuild
    .build({
      entryPoints: [entryFile],
      outfile: outFile,
      minify: true,
      bundle: true,
      target: TARGET,
      plugins: [copyPlugin, nodeExternalsPlugin()],
      sourcemap: true,
      platform: "node",
      define,
      external: ["pg", "sqlite3", "tedious", "pg-hstore"],
    })
    .then((r) => {
      console.log(`Build ${entryFile} to ${outFile} succeeded.`);
    })
    .catch((e) => {
      console.log("Error building:", e.message);
      process.exit(1);
    });
Dhaval Javia
  • 103
  • 1
  • 7