-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
I'm using esbuild to build a typescript project. I'm using a dependency in my typescript project that was coded with typescript however was transpiled to js and published like that, the structure of that dependency look like this from node_modules:
node_modules/
└── mydependency/
├── package.json
└── package/
├── index.js
└── utils/
└── utils.js
└── index.js
└── errors/
└── index.js
In package/index.js the dependency is exporting everything from utils and errors folders. Acting like a module file.
Also there is a something = require("..") in the utils file of the utils folder(package/utils/utils.js). I'm expecting that esbuild resolve the index.js in the parent folder. However is resolving the package.json. When I look at the esbuild verbose logs, I found this:
⬥ [VERBOSE] Resolving import ".." in directory "node_modules/mydependency/package/utils" of type "require-call"
Attempting to load "node_modules/mydependency/package" as a file
Checking for file "package"
Checking for file "package.jsx"
Checking for file "package.js"
Checking for file "package.tsx"
Checking for file "package.ts"
Checking for file "package.css"
Checking for file "package.json"
Found file "package.json"
Read 3 entries for directory "node_modules/mydependency"
Primary path is "node_modules/mydependency/package.json" in namespace "file"
After a deep research I understood that node module resolution resolves the relative path ".." like that. But why while developing this dependency(mydependency) doing the import in the typescript file(utils.ts) I don't get this error. Why in typescript do not resolve to package.json? Also why if I run a testing.js file with nodejs(20.9.0) from mydependency folder that call a method in utils.js the require doesn't resolve to package.json an resolves correctly to the index.js in the parent directory (mydependency/package/index.js). But if I bundle this testing file with esbuild it resolves to package.json.