Skip to content

Disabling native node modules with browser field not working #238

Closed
@SamyPesse

Description

esbuild supports the browser field in package.json, but it doesn't seem to support it for native node modules (such as fs, etc)

I'm having the issue while building a project with the dependency @apidevtools/json-schema-ref-parser which explicitly set fs to false

node_modules/@apidevtools/json-schema-ref-parser/lib/util/url.js:23:24: error: Could not resolve "url"
node_modules/@apidevtools/json-schema-ref-parser/lib/util/url.js:24:26: error: Could not resolve "url"
node_modules/@apidevtools/json-schema-ref-parser/lib/resolvers/http.js:3:21: error: Could not resolve "http"
node_modules/@apidevtools/json-schema-ref-parser/lib/resolvers/http.js:4:22: error: Could not resolve "https"
node_modules/@apidevtools/json-schema-ref-parser/lib/resolvers/file.js:2:19: error: Could not resolve "fs"

The error concerning url, http, https are making sense because they are not being listed in the browser field (and can be added as dependencies in the parent project from Yarn). But the one for fs doesn't make sense.

I've seen integration tests for the browser field, but I feel like one is missing for native node modules.

The issue seems to come from the loadNodeModule call when the module is set to false in browser.

if remapped == nil {
// "browser": {"module": false}
if absolute, ok := r.loadNodeModules(importPath, sourceDirInfo); ok {
return absolute, ResolveDisabled
} else {
return "", ResolveMissing

It looks like loadNodeModule should have a list of native node modules to consider as existing. Or if the value in browser is false, it should just ignore the module.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions