Skip to content

Conversation

@trentm
Copy link
Contributor

@trentm trentm commented May 7, 2024

Refs: #88 (comment)

Repeating part of that comment above:

However Node's require() doesn't treat .cjs the same way it does .js. https://nodejs.org/api/modules.html#file-modules says:

If the exact filename is not found, then Node.js will attempt to load the required filename with the added extensions: .js, .json, and finally .node. When loading a file that has a different extension (e.g. .cjs), its full name must be passed to require(), including its file extension (e.g. require('./file.cjs')).

I think RITM should follow the same pattern and NOT normalize-away .cjs.

@trentm trentm changed the title docs, test: clarify how to hook an module sub-path using .cjs extension docs, test: clarify how to hook a module sub-path using .cjs extension May 7, 2024
Comment on lines +7 to +11
// E.g., a Hook arg of `cjs-sub-module/foo` will **not** hook
// `./node_modules/cjs-sub-module/foo.cjs`. This is different compared to `.js`
// file extension usage. The difference is that Node.js's `require()` treats
// `.js` and `.cjs` differently.
// See https://nodejs.org/api/modules.html#file-modules
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the reasoning, but it would make instrumenting modules very complicated. The end user API of require-in-the-middle is that the export of whatever targeted module is provided to the hook function. The (package.json).exports map complicates this as it can resolve the same export name to multiple extensions. In my view, the hook function shouldn't need to know about that detail. It should just receive the right thing.

the `modules` entry. E.g. `@langchain/core/dist/callbacks/manager.cjs` is
required to hook
`.../node_modules/@langchain/core/dist/callbacks/manager.cjs`.
This is because []`.cjs` is not handled specially by `require()` the way
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is because []`.cjs` is not handled specially by `require()` the way
This is because [`.cjs` is not handled specially by `require()` the way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants