Skip to content

Docs/guidance on how to handle dynamic imports of transitive dependencies when bundling for node #4188

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

Open
mdarens opened this issue May 21, 2025 · 1 comment

Comments

@mdarens
Copy link

mdarens commented May 21, 2025

I'm bundling a containerized app that wraps parse server and the vendor code includes conditional dynamic imports that don't trigger until a certain endpoint is hit.

Despite every permutation of external configs and module formats, targets etc I've tried, I can't seem to get esbuild to emit these transitive dependencies in the bundle, or to the out folder with the bundle— the only reliable way to be able to resolve them at runtime seems to be manually copying dependencies from node_modules to the out folder in the Dockerfile. I can either do all of node_modules, which inflates my container size, or enumerate the dependencies, which adds a maintenance burden and is fragile.

I can't help but think there must be some config option or plugin I'm missing which makes this easier, but most of the discussion around dealing with dynamic imports in esbuild I can find seems to be geared toward direct deps called from app code and not transitive deps called from libraries.

Thanks for your time and thanks for the awesome tool!

@hyrious
Copy link

hyrious commented May 22, 2025

This is because the dynamic import code you mentioned is not analyzable. So esbuild won't know some random strings are actually pointing to modules inside node_modules and bundle them in.

The only reliable way to be able to resolve them at runtime seems to be manually copying dependencies from node_modules to the out folder in the Dockerfile...

I think this is the proper way to load these dependencies for a Node.js application. However the work can be done more cleanly with the help of npm, which I explained before in another issue.

The main idea is using npm install --omit=dev to only install non-dev dependencies in the server (or in the docker container) to ensure only minimal needed modules are downloaded.

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

No branches or pull requests

2 participants