What is import-meta-resolve?
The import-meta-resolve package provides utilities for resolving module specifiers in the same way that Node.js does for ES modules. It allows developers to programmatically resolve specifiers from the context of a specific file or URL, which can be useful for tooling that needs to understand module resolution, bundlers, and other development tools.
What are import-meta-resolve's main functionalities?
Resolving module specifiers
This feature allows you to resolve the full path of a module specifier from a specific context, typically the URL of the importing module. The code sample demonstrates how to use the resolve function to find the full path of 'some-module' as if it were being imported from the current module.
import { resolve } from 'import-meta-resolve';
(async () => {
const resolved = await resolve('some-module', import.meta.url);
console.log(resolved);
})();
Other packages similar to import-meta-resolve
resolve
The 'resolve' package is a popular Node.js module resolution algorithm that can be used programmatically. It is similar to import-meta-resolve but does not specifically target ES module resolution semantics. It is more general-purpose and can resolve CommonJS modules as well.
enhanced-resolve
This package is used internally by webpack to resolve module paths. It is highly configurable and can handle complex resolution scenarios, including loaders and plugin systems. While it offers more features than import-meta-resolve, it is also more complex and tailored to webpack's ecosystem.
import-meta-resolve
Resolve paths in node when using es modules (or commonjs). Ponyfill for import.meta.resolve
as it is behind a flag.
Installation
npm i import-meta-resolve
Features
- can be used in node using es modules & commonjs
- resolves folders (
require.resolve
dos not support it) - mimics
import.meta.url
so can hopefully be replaced by it once it is no longer behind a flag
Usage
import { importMetaResolve } from 'import-meta-resolve';
await importMetaResolve('@foo/bar/some-folder', import.meta.url);
await importMetaResolve('@foo/bar', import.meta.url);
await importMetaResolve('@foo/bar/some-file.js', import.meta.url);
await importMetaResolve('./foo.js', import.meta.url);
Will throw if file could not be resolved
try {
await importMetaResolve('@foo/bar/non-existing-file.js', import.meta.url);
} catch (error) {
}
Usage CommonJs
const { importMetaResolve } = require('import-meta-resolve');
await importMetaResolve('@foo/bar/some-folder', __file);
Motivation
require.resolve
enables you to resolve package files which can be useful if you need to manually load or check for files in packages.
However when using es modules in node require.resolve
is not allowed.
Which means there is currently no replacement for this functionality.
There is however an experimental api import.meta.resolve
which you can enable via a cli flag --experimental-import-meta-resolve
.
Experimental flags are however tough to integrate everywhere bins, other tools, ... and additionally it is tough to explain to your users that the have to do this.
Because of this a transition package like this may be useful.