@dual-bundle/import-meta-resolve

中文文档
4.2.1发布于 2025/09/01MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 3623.9万

A fork of import-meta-resolve with commonjs + ESM support at the same time, AKA dual package.

原始使用文档
文档目录16 节
  1. Installation
  2. Contents
  3. What is this?
  4. When to use this?
  5. Install
  6. Use
  7. API
  8. resolve(specifier, parent)
  9. moduleResolve(specifier, parent, conditions, preserveSymlin…
  10. ErrnoException
  11. Algorithm
  12. Differences to Node
  13. Types
  14. Compatibility
  15. Contribute
  16. License

@dual-bundle/import-meta-resolve

A fork of import-meta-resolve with commonjs + ESM support at the same time, AKA dual package.

It will rebase and try to release in order to sync with the upstream every day, see .github/workflows/rebase.yml for details.

Installation

# npm
npm install @dual-bundle/import-meta-resolve

# yarn
yarn add @dual-bundle/import-meta-resolve

import-meta-resolve

Build Coverage Downloads

Resolve things like Node.js.

Contents

What is this?

This package is a ponyfill for import.meta.resolve. It supports everything you need to resolve files just like modern Node does: import maps, export maps, loading CJS and ESM projects, all of that!

When to use this?

As of Node.js 20.0, import.meta.resolve is still behind an experimental flag. This package can be used to do what it does in Node 16–20.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install import-meta-resolve

Use

import {resolve} from 'import-meta-resolve'

// A file:
console.log(resolve('./index.js', import.meta.url))
//=> file:///Users/tilde/Projects/oss/import-meta-resolve/index.js

// A CJS package:
console.log(resolve('builtins', import.meta.url))
//=> file:///Users/tilde/Projects/oss/import-meta-resolve/node_modules/builtins/index.js

// A scoped CJS package:
console.log(resolve('@eslint/eslintrc', import.meta.url))
//=> file:///Users/tilde/Projects/oss/import-meta-resolve/node_modules/@eslint/eslintrc/lib/index.js

// A package with an export map:
console.log(resolve('micromark/lib/parse', import.meta.url))
//=> file:///Users/tilde/Projects/oss/import-meta-resolve/node_modules/micromark/lib/parse.js

// A node builtin:
console.log(resolve('fs', import.meta.url))
//=> node:fs

API

This package exports the identifiers moduleResolve and resolve. There is no default export.

resolve(specifier, parent)

Match import.meta.resolve except that parent is required (you can pass import.meta.url).

Parameters
  • specifier (string) — the module specifier to resolve relative to parent (/example.js, ./example.js, ../example.js, some-package, fs, etc)
  • parent (string, example: import.meta.url) — the absolute parent module URL to resolve from; you must pass import.meta.url or something else
Returns

Full file:, data:, or node: URL (string) to the found thing

Throws

Throws an ErrnoException.

The “Resolver Algorithm Specification” as detailed in the Node docs (which is slightly lower-level than resolve).

Parameters
  • specifier (string) — /example.js, ./example.js, ../example.js, some-package, fs, etc
  • parent (URL, example: import.meta.url) — full URL (to a file) that specifier is resolved relative from
  • conditions (Set<string>, default: new Set(['node', 'import'])) — conditions
  • preserveSymlinks (boolean, default: false) — keep symlinks instead of resolving them
Returns

A URL object (URL) to the found thing.

Throws

Throws an ErrnoException.

ErrnoException

One of many different errors that occur when resolving (TypeScript type).

Type
type ErrnoExceptionFields = Error & {
  errnode?: number | undefined
  code?: string | undefined
  path?: string | undefined
  syscall?: string | undefined
  url?: string | undefined
}

The code field on errors is one of the following strings:

  • 'ERR_INVALID_MODULE_SPECIFIER' — when specifier is invalid (example: '#')
  • 'ERR_INVALID_PACKAGE_CONFIG' — when a package.json is invalid (example: invalid JSON)
  • 'ERR_INVALID_PACKAGE_TARGET' — when a package.json exports or imports is invalid (example: when it does not start with './')
  • 'ERR_MODULE_NOT_FOUND' — when specifier cannot be found in parent (example: 'some-missing-package')
  • 'ERR_NETWORK_IMPORT_DISALLOWED' — thrown when trying to resolve a local file or builtin from a remote file (node:fs relative to 'https://example.com')
  • 'ERR_PACKAGE_IMPORT_NOT_DEFINED' — when a local import is not defined in an import map (example: '#local' when not defined)
  • 'ERR_PACKAGE_PATH_NOT_EXPORTED' — when an export is not defined in an export map (example: 'tape/index.js', which is not in its export map)
  • 'ERR_UNSUPPORTED_DIR_IMPORT' — when attempting to import a directory (example: './lib/')
  • 'ERR_UNKNOWN_FILE_EXTENSION' — when somehow reading a file that has an unexpected extensions ('./readme.md')
  • 'ERR_INVALID_ARG_VALUE' — when conditions is incorrect

Algorithm

The algorithm for resolve matches how Node handles import.meta.resolve, with a couple of differences.

The algorithm for moduleResolve matches the Resolver Algorithm Specification as detailed in the Node docs (which is sync and slightly lower-level than resolve).

Differences to Node

  • parent defaulting to import.meta.url cannot be ponyfilled: you have to explicitly pass it
  • no support for loaders (that would mean implementing all of loaders)
  • no support for CLI flags: --conditions, --experimental-default-type, --experimental-json-modules, --experimental-network-imports, --experimental-policy, --experimental-wasm-modules, --input-type, --no-addons, --preserve-symlinks, nor --preserve-symlinks-main work
  • no support for WATCH_REPORT_DEPENDENCIES env variable
  • no attempt is made to add a suggestion based on how things used to work in CJS before to not-found errors
  • prototypal methods are not guarded: Node protects for example String#slice or so from being tampered with, whereas this doesn’t

Types

This package is fully typed with TypeScript. It exports the additional type ErrnoException.

Compatibility

This package is at least compatible with all maintained versions of Node.js. As of now, that is Node.js 16 and later.

Contribute

Yes please! See How to Contribute to Open Source.

License

MIT © Titus Wormer and Node.js contributors

版本与发布记录

@dual-bundle/import-meta-resolve 共发布 4 个版本,首次发布于 2024/02/28;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.2.1latest2025/09/011.2 年前
4.2.02025/09/011.2 年前
4.1.02024/05/092 年前
4.0.02024/02/282 年前

关于 @dual-bundle/import-meta-resolve 的常见问题

@dual-bundle/import-meta-resolve 是什么?

A fork of import-meta-resolve with commonjs + ESM support at the same time, AKA dual package.

如何安装 @dual-bundle/import-meta-resolve?

在项目目录运行 npm install @dual-bundle/import-meta-resolve,或使用 pnpm add / yarn add / bun add。

@dual-bundle/import-meta-resolve 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dual-bundle/import-meta-resolve 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dual-bundle/import-meta-resolve 可以商用吗?

@dual-bundle/import-meta-resolve 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dual-bundle/import-meta-resolve 的下载量有多少?

根据 npm 官方下载统计,@dual-bundle/import-meta-resolve 在 2026/10/13 – 2026/11/11 这 30 天内被下载 36,239,444 次,日均约 1,207,981 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dual-bundle/import-meta-resolve 还在维护吗?

最新版本 4.2.1 发布于 2025/09/01(1.2 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/02/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。