Esbuild: Node support / node_modules and string literals

Created on 16 Feb 2020  路  6Comments  路  Source: evanw/esbuild

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.

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.

All 6 comments

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 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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fannheyward picture fannheyward  路  4Comments

sarsamurmu picture sarsamurmu  路  3Comments

elektronik2k5 picture elektronik2k5  路  3Comments

frandiox picture frandiox  路  3Comments

aelbore picture aelbore  路  3Comments