chyyran
chyyran14mo ago

How to resolve requires without `.js` extension in a very basic NPM resolver?

I have an NPM resolver that without caching that expects everything to be in a node_modules folder, mostly copied from deno cli https://gist.github.com/rust-play/fbd49c87332ee27b3d217bb1e0005462 I can successfully require lodash, but one of the npm libraries I'm trying to require imports a babel library like so
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
It seems like it's failing to resolve the module, cause it panics at thread 'main' panicked at 'called Result::unwrap() on an Err value: Error: Cannot find module '@babel/runtime/helpers/interopRequireDefault'
Gist
Code shared from the Rust Playground
Code shared from the Rust Playground. GitHub Gist: instantly share code, notes, and snippets.
2 Replies
chyyran
chyyranOP14mo ago
It ends up bailing at
if current_folder == self.root_node_modules_path {
bail!(
"could not find package '{}' from referrer '{}'.",
name,
referrer
);
}
if current_folder == self.root_node_modules_path {
bail!(
"could not find package '{}' from referrer '{}'.",
name,
referrer
);
}
where sub_dir is "[truncated]/node_modules/@babel/runtime/helpers/interopRequireDefault"
Deno AI Helper
Deno AI Helper14mo ago
Based on the documentation, Deno does not support the automatic resolution of modules without a file extension, which is a common practice in Node.js. In Deno, you need to include the full file extension when importing modules. This is one of the key differences between Deno and Node.js. In your case, the import statement require("@babel/runtime/helpers/interopRequireDefault") is missing the .js extension. This is likely causing the error because Deno is unable to find the module without the explicit .js extension. To resolve this issue, you would need to modify the import statement to include the .js extension, like so:
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault.js");
However, manually updating all import statements in a large codebase or in third-party libraries might not be feasible. Unfortunately, the documentation does not provide a solution for automatically resolving modules without a file extension in Deno. You might need to use a custom script or a tool to add the missing .js extensions to all import statements before running your code in Deno. Please note that this information is not explicitly stated in the documentation, and it's a general advice based on the differences between Deno and Node.js.