Node module resolution that is flexible, synchronous and requires no builtin dependencies.
npm install resolve-sync
import { resolveSync } from "resolve-sync";
const result = resolveSync(id, options);
id
: The module id or path to resolve (e.g."./file"
,"some-pkg"
,"#alias"
).options
: An object controlling resolution (see below).
This module is designed to optimize speed while resolving in the same way node and bundlers do.
It avoids using any node:
built in modules to make using in any environment (including browsers) easy.
- main field (and other fields via
options.fields
) - browser field
- custom extensions
- export conditions
- import conditions
The absolute path to the source file (not a directory) where the resolution is initiated. Determines the starting directory and influences relative resolution.
An optional root boundary directory. Module resolution won't ascend beyond this directory when traversing parent paths. Defaults to /
if unspecified.
An optional array of file extensions to try when resolving files without explicit extensions. Defaults to:
[".js", ".json"];
Specifies the priority order of package.json fields to look at when resolving package main entries. Defaults to:
["module", "main"]
ifbrowser
isfalse
or unset.["browser", "module", "main"]
ifbrowser
istrue
.
When true
, this flag enables require
conditions in exports
maps. Default is false
.
Partially implements the package.json browser field specification.
- Prioritizes the
browser
field in package.json when selecting entry points. - Enables
browser
remapping for internal paths (e.g.,browser: { "./main.js": "./shim.js" }
).
Note: Remaps defined in the
browser
field are only applied when resolving the module as a dependency (e.g.,some-module/foo
). They do not affect relative paths like./foo
that are resolved from within the module itself. Module remaps in the browser field likewise are not supported.
A list of custom export conditions to use during resolution of package exports
and imports
fields. These conditions are matched in order and function identically to how conditions are interpreted by the wonderful resolve.exports
module.
fs?: { isFile(file: string): boolean; readPkg(file: string): unknown; realpath?(file: string): string; }
A partial filesystem interface used by the resolver. If running in node, and not provided, a default node:fs
based implementation is used. Must implement:
isFile(file: string): boolean
– checks if the file exists and is a file.readPkg(file: string): unknown
– reads and parses a JSON file (e.g.,package.json
).realpath?(file: string): string
– optionally resolves symlinks or returns the canonical path.
import { resolveSync } from "resolve-sync";
const result = resolveSync("./file", {
from: "/project/src/index.js",
});
console.log(result); // => "/project/src/file.js"
const result = resolveSync("./file", {
from: "/project/src/index.js",
exts: [".ts", ".js"],
});
// Tries ./file.ts, then ./file.js
const result = resolveSync("some-pkg", {
from: "/project/src/index.js",
});
// Looks for /project/node_modules/some-pkg/package.json and resolves its main/module field
const result = resolveSync("some-pkg", {
from: "/project/src/index.js",
browser: true,
});
// Adds the `browser` export condition.
// If no `exports` field is found will check the "browser" field in package.json
const result = resolveSync("some-pkg", {
from: "/project/src/index.js",
require: true,
});
// Replaces the "node" export condition with "require".
const result = resolveSync("some-pkg", {
from: "/project/src/index.js",
fields: ["module", "jsnext:main", "main"],
});
// Looks for "module", then "jsnext:main" and finally "main" in package.json
const result = resolveSync("some-pkg", {
from: "/project/src/index.js",
conditions: ["worker"],
});
// Adds "worker" to conditions, in this case ["default", "worker", "import", "node"]
const importResult = resolveSync("#alias", {
from: "/project/src/index.js",
});
// Uses the "imports" field in the nearest package.json to find "#alias"
const result = resolveSync("some-pkg", {
from: "/project/src/index.js",
root: "/project", // Do not search above /project
});
// Will not resolve modules outside of /project
This module has no dependencies hard dependencies on node:
apis and can easily be used
in browser environments (eg a repl). When doing so the fs
option is required.
import { resolveSync } from "resolve-sync";
const files = {
"/project/src/file.js": "",
"/project/package.json": JSON.stringify({ main: "src/file.js" }),
};
const result = resolveSync("./file", {
from: "/project/src/index.js",
// Provide a minimal fs interface:
fs: {
isFile(file) {
return file in files;
},
readPkg(file) {
return JSON.parse(files[file]);
},
},
});
console.log(result); // => "/project/src/file.js"