evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
38.21k stars 1.15k forks source link

esbuild (packages=external) does not add external packages (node_modules) from docker #3970

Closed Diboby closed 1 week ago

Diboby commented 1 week ago

Description

The problem

On my machine (macOs 15), when packaging serverless using esbuild with the option packages='external', a node_modules folder is added automacally to the build folder. With the same configuration, but run the command from any nodejs docker image (alpine, bullseye or bookworm) does not produce the same output, i-e the node_modules folder is not added.

Config

My esbuild config file looks like

esbuild.config.mjs ```ts export default (serverless) => { return { entryPoints: ['src/index.ts'], bundle: true, platform: 'node', minify: true, sourcemap: true, packages: 'external', target: 'node20.9', plugins: [], }; }; ```

Possible workarounds

  1. Run npm i --omit=dev in the build folder: This approach is not possible with serverless deploy command without providing a package location option

Other informations:

hyrious commented 1 week ago

This seems a serverless's intentional behavior: https://github.com/serverless/serverless/blob/main/lib%2Fplugins%2Fesbuild%2Findex.js#L873-L881.

Diboby commented 1 week ago

Thank you so much, @hyrious! 👍

The issue was related to npm authentication. From your comment, I understood that Serverless runs npm install in the background. So, when there is a private dependency, npm requires authentication against the private registry.

In my case, npm authentication was missing from within a Docker container, and unfortunately, I didn’t receive any error logs.

After fixing the authentication process, everything worked fine.

I'm really grateful for your help, @hyrious! 💯