Skip to content
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

Node support / node_modules and string literals #10

Closed
billymcintosh opened this issue Feb 16, 2020 · 6 comments
Closed

Node support / node_modules and string literals #10

billymcintosh opened this issue Feb 16, 2020 · 6 comments

Comments

@billymcintosh
Copy link

Hey, nice project.

Is this node compatible? I am guessing not:-

error: Could not resolve "fs"
const fs = require('fs');

I gave my app a test build but it's failing every time with

error: The argument to require() must be a string literal

The above error originates from libraries within node_modules directory.

To test if I could get past these errors, I changed to string literals in the lib and it still results in the same error.

@evanw
Copy link
Owner

evanw commented Feb 16, 2020

I'm intending esbuild to be a bundler for the web, not for node. Here's my answer for a similar question on issue #5 (comment):

Good question.

The output of esbuild's --bundle option is intended to be used for running in the browser. Not only does it require all of your source code to be in files on disk, but it also uses substitutions in the browser field in package.json files to bundle browser-specific versions of your npm libraries. So it won't work with any npm libraries that use standard node modules.

In my experience, projects that run in node keep the individual source files separate on disk and use require() calls to load the files at run-time instead of bundling them all into a single file at build time. What's your use case for bundling your node project into a single file? Or are you still targeting the browser instead of node and just using some npm libraries that aren't browser-friendly?

I'm curious what your use case is for using a bundler with a node app.

@billymcintosh
Copy link
Author

Compatibility and portability are the main use cases. Tree shaking is a bonus.

Apologies for not seeing that. I read a few issues but missed that.

Thanks for the quick response!

@tmikaeld
Copy link

tmikaeld commented Feb 19, 2020

Unfortunately, many packages include node packages but don't use them (Node & Browser compatible).

Which is why other bundler like webpack show a warning instead of an outright error.

Could this be made so that it shows an error and returns the missing node modules as undefined?

@evanw
Copy link
Owner

evanw commented Feb 20, 2020

Yes, I would like to do this. I will likely add a context parameter so you can configure whether your build is targeting the browser or node, and modify the behavior of this feature depending on context.

@evanw
Copy link
Owner

evanw commented Apr 13, 2020

I just added some basic support for this via --platform=node. The path passed to require() must still be a string, but if it matches one of node's builtin modules it will be passed through to node's require().

@jpadilla
Copy link

jpadilla commented May 1, 2020

Another interesting pattern to possibly support is require inside a try/catch like this one:

https://github.com/visionmedia/debug/blob/db306db99e7822d355724698990d335927563210/src/node.js#L25-L112

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

4 participants