Esbuild: Could not resolve dependency

Created on 14 Feb 2020  路  7Comments  路  Source: evanw/esbuild

$ esbuild --bundle --outdir=dist --minify --sourcemap src/main.js 
node_modules/axios/lib/defaults.js:23:22: error: Could not resolve "./adapters/http"
    adapter = require('./adapters/http');

1 error
$ ls -l node_modules/axios/lib/adapters/http.js 
-rwxr-xr-x. 1 daver daver 9308 Jan 27 09:42 node_modules/axios/lib/adapters/http.js

All 7 comments

Thanks for the report. It looks like my remapping code for the browser field in package.json is relative to the wrong directory. I hadn't discovered this earlier because the packages I tested had all files in the same directory, so it worked even though it was incorrect. This should be easy to fix.

@evanw hi, can we also build assets which use standard Node modules?

I have these issues:

yehor@desktop ~/W/Y/yforum2> esbuild --bundle webpack.mix.js  --outdir=dist --minify --sourcemap
node_modules/laravel-mix/src/index.js:19:22: error: Could not resolve "path"
global.path = require('path');
                      ~~~~~~
node_modules/dotenv/lib/main.js:24:19: error: Could not resolve "fs"
const fs = require('fs')
                   ~~~~
node_modules/dotenv/lib/main.js:25:21: error: Could not resolve "path"
const path = require('path')
                     ~~~~~~
node_modules/laravel-mix/src/File.js:1:17: error: Could not resolve "os"
let os = require('os');
                 ~~~~
node_modules/laravel-mix/src/File.js:3:19: error: Could not resolve "path"
let path = require('path');

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?

Short answer: yes.

I want to use the esbuild to build my assets for a Laravel project (it's a php framework) and produce a JS file for browsers.

But I think I am applying the esbuild in a wrong place.

https://github.com/evanw/esbuild/blob/6e2d7982c9a4e30f3a37b9f121c086a8e420ef35/src/esbuild/resolver/resolver.go#L66-L70

It seems that loadNodeModules is not check to packageJson.browserModuleMap.

So, the browser field set module returns ResolveMissing.

To add to the list of use cases of bundeling for Node.js. I currently am developing an Alfred (MacOS App) workflow. Since MacOS does not natively support node I only ask my users to install Node.js and bundle all the other dependencies so users won't have to run an npm install on each update.

Another use case for bundling Node.js would be serverless functions.

However I'm running into same errors as described above

> esbuild dist/workflow/src/alfred-workflow-todoist.js --bundle --minify --sourcemap --outfile=dist/workflow/src/bundle.js
dist/workflow/src/lib/cli-arguments.js:3:23: error: Could not resolve "util"
const util_1 = require("util");
                       ~~~~~~
node_modules/dotenv/lib/main.js:24:19: error: Could not resolve "fs"
const fs = require('fs')
                   ~~~~
node_modules/dotenv/lib/main.js:25:21: error: Could not resolve "path"
const path = require('path')
                     ~~~~~~
node_modules/macos-version/index.js:2:19: error: Could not resolve "fs"
const fs = require('fs');
                   ~~~~
node_modules/clean-stack/index.js:2:19: error: Could not resolve "os"
const os = require('os');
                   ~~~~
dist/workflow/src/lib/logger.js:7:23: error: Could not resolve "util"
const util_1 = require("util");
                       ~~~~~~
dist/workflow/src/lib/workflow/notification.js:3:32: error: Could not resolve "child_process"
const child_process_1 = require("child_process");
                                ~~~~~~~~~~~~~~~
node_modules/got/dist/source/index.js:6:22: error: Could not resolve "url"
const url_1 = require("url");
                      ~~~~~
node_modules/conf/index.js:22:7: error: "require" must not be called indirectly
delete require.cache[__filename];
       ~~~~~~~
node_modules/got/dist/source/as-stream.js:4:23: error: Could not resolve "http"
const http_1 = require("http");
                       ~~~~~~

There is discussion about nodeJS support in https://github.com/evanw/esbuild/issues/10

Was this page helpful?
0 / 5 - 0 ratings

Related issues

frandiox picture frandiox  路  3Comments

evanplaice picture evanplaice  路  3Comments

vforsh picture vforsh  路  3Comments

OneOfOne picture OneOfOne  路  3Comments

egoist picture egoist  路  3Comments