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.
I'm intending esbuild to be a bundler for the web, not for node. Here's my answer for a similar question on issue https://github.com/evanw/esbuild/issues/5#issuecomment-586656852:
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 thebrowser
field inpackage.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.
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!
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?
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.
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()
.
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
Most helpful comment
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.