Comments (8)
I was able to reproduce the issue on master, looking into it
from import-in-the-middle.
This should have been resolved by #78
from import-in-the-middle.
When modFile
is has-submodule/sub
, and srcUrl
is file:///private/tmp/29/tap-issue/node_modules/reexport-submodule/index.js
, then modUrl
is defined as file:///private/tmp/29/tap-issue/node_modules/reexport-submodule/has-submodule/sub
.
from import-in-the-middle.
Similar issue trying to import langchain:
modUrl: file:///Users/meyer9/development/repro/node_modules/langchain/dist/embeddings/@langchain/core/embeddings
modFile: @langchain/core/embeddings
normalizedModName: embeddings
srcUrl: file:///Users/meyer9/development/repro/node_modules/langchain/dist/embeddings/base.js
node:internal/process/esm_loader:40
internalBinding('errors').triggerUncaughtException(
^
[Error: ENOENT: no such file or directory, open '/Users/meyer9/development/repro/node_modules/langchain/dist/embeddings/@langchain/core/embeddings'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/Users/meyer9/development/repro/node_modules/langchain/dist/embeddings/@langchain/core/embeddings'
}
The line in question:
export * from "@langchain/core/embeddings";
Seems like if it's an external package, this logic won't work either. Could this be used instead? https://nodejs.org/api/esm.html#importmetaresolvespecifier
Edit: oh, I see you can't do that because import-in-the-middle isn't type module.
I tried adding require resolution here and it sorta worked, but I got another issue with a duplicate identifier.
if (!modFile.startsWith('.')) {
modFile = require.resolve(modFile, {
paths: [new URL(srcUrl).pathname]
})
}
Oh, looks like someone had the exact same thought process: #60. Looks like #59 could be a dupe.
from import-in-the-middle.
#62 is also potentially a dupe (with repro), displaying the same issue of re-exports of another module being incorrectly treated as subdirectories/files within the module doing the re-export.
from import-in-the-middle.
Not able to reproduce this issue, can you confirm the IITM version you are using?
from import-in-the-middle.
@khanayan123 Is there any update? I'd like to help if there are some failed tests can be investigated.
from import-in-the-middle.
I've been looking into this with the following reliable reproduction using Node v22.2.0:
import { register } from 'node:module';
register('import-in-the-middle/hook.mjs', import.meta.url);
await import ('@discordjs/builders');
Which results in:
node:internal/modules/run_main:125
triggerUncaughtException(
^
Error: ENOENT: no such file or directory, open '/Users/tim/Documents/Repositories/repro/node_modules/@discordjs/builders/dist/@discordjs/formatters'
at async open (node:internal/fs/promises:638:25)
at async readFile (node:internal/fs/promises:1251:14)
at async getSource (node:internal/modules/esm/load:46:14)
at async defaultLoad (node:internal/modules/esm/load:137:34)
at async nextLoad (node:internal/modules/esm/hooks:750:22)
at async getExports (/Users/tim/Documents/Repositories/repro/node_modules/import-in-the-middle/lib/get-exports.js:64:21)
at async processModule (/Users/tim/Documents/Repositories/repro/node_modules/import-in-the-middle/hook.js:136:23)
at async processModule (/Users/tim/Documents/Repositories/repro/node_modules/import-in-the-middle/hook.js:164:20)
at async getSource (/Users/tim/Documents/Repositories/repro/node_modules/import-in-the-middle/hook.js:274:60)
at async load (/Users/tim/Documents/Repositories/repro/node_modules/import-in-the-middle/hook.js:340:26) {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/Users/tim/Documents/Repositories/repro/node_modules/@discordjs/builders/dist/@discordjs/formatters'
}
It appears that this code expects the module it's loading to be relative to the current file:
https://github.com/DataDog/import-in-the-middle/blob/00b01fff1f5b69dd25e307593ec54d1d8abb4844/hook.js#L154-L166
My proposal is that if it's a bare specifier, we call parentResolve
to resolve the package URL. Using resolve is better than manually trying to find the correct files since there might be multiple levels of node_modules
directories that need traversing and we need to respect package.json#exports
when we find the package!
I'll attempt a PR!
from import-in-the-middle.
Related Issues (20)
- Use `es-module-lexer` to parse ESM
- Doesn't handle exports with invalid identifiers
- `getFullCjsExports` does not resolve re-exports from external dependencies
- IITM Throws Error When an NPM Package is Not Installed in the Root Folder HOT 7
- Are sequential patched supported? HOT 6
- parentResolve is not a function HOT 1
- Should fail gracefully when exports cannot be parsed
- Explicitly named exports should be exported over `export *` exports HOT 3
- Acorn parser does not support import attributes
- SyntaxError on importing some packages when using register() HOT 14
- SyntaxError when importing a JSON file using { type: 'json' } HOT 3
- import-in-the-middle' failed to wrap 'file:///app/node_modules/@prisma/client/default.js' HOT 2
- iitm wrappers copy exported primitives HOT 1
- Update LICENSE and NOTICE
- Codify governance HOT 2
- Resolve exports for native modules
- Errors when importing vue/server-renderer
- Import in the middle breaks `drizzle-orm` exports HOT 3
- Unable to import from Vue HOT 1
- `release-please` GHA fails due to expired access token HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from import-in-the-middle.