# @import-maps/resolve

> Parse and resolve imports via an import map

Latest version **2.0.0** (published 2023-06-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @import-maps/resolve
pnpm add @import-maps/resolve
yarn add @import-maps/resolve
bun add @import-maps/resolve
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2023-06-09 |
| First published | 2019-06-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 96.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2398 |
| Author | open-wc |
| Maintainers | d4kmor, modern-web |
| Keywords | import-map, import-maps |

## Links

- npm: https://www.npmjs.com/package/@import-maps/resolve
- Repository: https://github.com/open-wc/open-wc
- Homepage: https://github.com/open-wc/open-wc/tree/master/packages/import-maps-resolve
- Issues: https://github.com/open-wc/open-wc/issues
- npm.io page: https://npm.io/package/@import-maps/resolve

## Recent versions

- 2.0.0 (latest) — 2023-06-09
- 1.0.1 — 2020-10-28
- 1.0.0 — 2020-08-19
- 0.3.2 — 2020-05-07
- 0.3.1 — 2020-05-07
- 0.2.6 — 2020-04-12
- 0.2.5 — 2020-03-24
- 0.2.4 — 2020-02-09
- 0.2.3 — 2020-02-03
- 0.2.2 — 2020-02-02
- 0.2.1 — 2019-11-24
- 0.2.0 — 2019-11-19
- 0.1.5 — 2019-10-25
- 0.1.4 — 2019-10-23
- 0.1.3 — 2019-08-05
- … 3 more at https://npm.io/package/@import-maps/resolve/versions

## README

# Resolve import-maps

Library for parsing and resolving [import maps](https://github.com/WICG/import-maps).

## Usage

```bash
npm i --save-dev @import-maps/resolve
```

### Base URL

Parsing and resolving import maps requires a base URL. This is an instance of the `URL` constructor.

This can be a browser URL:

```js
const myUrl = new URL('https://www.example.com/');
```

Or a file URL when working with a file system. The `pathToFileURL` function is useful for converting a file path to a URL object:

```js
import path from 'path';
import { pathToFileURL } from 'url';

const fileUrl1 = new URL('file:///foo/bar');
const fileUrl2 = pathToFileURL(path.join(process.cwd(), 'foo', 'bar'));
```

### Parsing an import map from a string

The `parseFromString` function parses an import map from a JSON string. It returns the parsed import map object to be used when resolving import specifiers.

```js
import { parseFromString } from '@import-maps/resolve';

// get the import map from somewhere, for example read it from a string
const importMapString = '{ "imports": { "foo": "./bar.js" } }';
// create a base URL to resolve imports relatively to
const baseURL = new URL('https://www.example.com/');

const importMap = parseFromString(importMapString, baseURL);
```

### Parsing an import map from an object

If you already have an object which represents the import map, it still needs to be parsed to validate it and to prepare it for resolving. You can use the `parse` function for this.

```js
import { parse } from '@import-maps/resolve';

// get the import map from somewhere, for example read it from a string
const rawImportMap = { imports: { foo: './bar.js' } };
// create a base URL to resolve imports relatively to
const baseURL = new URL('https://www.example.com/');

const importMap = parse(rawImportMap, baseURL);
```

### Resolving imports

Once you've created a parsed import map, you can start resolving specifiers. The `resolve` function returns an object with the resolved URL as well as a boolean whether the import was matched. When a bare import is not found in the import map, resolve returns null. When a relative import isn't found, the resolved URL is returned, and matched will be set to false.

```js
import { resolve } from '@import-maps/resolve';

const importMapString = '{ "imports": { "foo": "./bar.js" } }';
const baseURL = new URL('https://www.example.com/');
const importMap = parseFromString(importMapString, baseURL);

const scriptUrl = new URL('https://www.example.com/my-app.js');

// resolvedImport: https://www.example.com/bar.js, matched: true
const { resolvedImport, matched } = resolve('foo', baseURL, scriptUrl);

// resolvedImport: https://www.example.com/x.js, matched: false
const { resolvedImport, matched } = resolve('./x.js', baseURL, scriptUrl);

// resolvedImport: null, matched: false
const { resolvedImport, matched } = resolve('bar', baseURL, scriptUrl);
```

If you need to use the resolved path on the file system, you can use the `fileURLToPath` utility:

```js
import { fileURLToPath } from 'url';
import { resolve } from '@import-maps/resolve';

const { resolvedImport } = resolve(importMapString, baseURL, scriptUrl);

// the fully resolved file path
console.log(fileURLToPath(resolvedImport));
```

## Acknowledgments

This implementation is heavily based on the [import-maps reference implementation](https://github.com/WICG/import-maps/tree/master/reference-implementation).

---
_Source: https://npm.io/package/@import-maps/resolve · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
