Skip to content

node resolution algorithm with NODE_PATH resolves in wrong order #1117

@mzeiher

Description

@mzeiher

Hi

I found a bug in the resolution algorithm, node_modules folders in the same directory and in parent directories should have precedence over modules directly in NODE_PATH

here is a repository to reproduce this bug:
https://github.com/mzeiher/esbuild-node-path-bug

there are 2 versions of "package-c" in the directory which is loaded via NODE_PATH, one which exports "version 1.0.0" directly in the "NODE_PATH" directory and one in the node_modules folder in the package-b module, the package-b module uses package-c and I would expect that the require (or import) would load the package-c file in the node_modules directory within package-b, instead esbuild resolves to the package-c module in the root of the "NODE_PATH" directory.

You can reproduce this bug with the linked repository:

How to reproduce:

# bundle project
> go run main.go
# go to ./project
> export NODE_PATH=$(pwd)/../NODE_PATH/node_modules
> node ./index.js
# output
# package_a: 1.0.0
# package_b: package_c version:2.0.0 (as expected, should be resolved to the version relative to the using module)
> node ./out.js 
# output
# package_a: 1.0.0
# package_b: package_c version:undefined (should be 2.0.0, but displayed is undefined and bundled is the "1.0.0" verison which ist in the root of NODE_PATH

node resolves the "package-c" dependency to the node_modules folder relative to the using module, esbuild resolves the "package-c" dependency to the module in the root of the NODE_PATH variable (just check the bundled out.js)

Maybe esbuild should mimic the resolution algorithm of nodejs?

Tested with esbuild 0.11.5 and node 14.15.1 on a windows PC

Metadata

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