Registry / serialization / parse-imports

parse-imports

JSON →
library3.0.0jsnpmunverified

parse-imports is a high-performance JavaScript utility for parsing ES module imports within a given string of code. It leverages the `es-module-lexer` (a WASM-based library) for its core parsing logic, ensuring speed and accuracy. The library is currently at version 3.0.0 and actively maintained, with major versions often introducing updated Node.js runtime requirements. Key features include identifying various module specifier types (e.g., package, relative, absolute, builtin), correctly unescaping specifier sequences, collecting default, named, and namespace imports, and handling dynamic `import()` expressions. It also provides functionality to resolve module specifier paths using `require.resolve`. This makes it a robust tool for static analysis, bundlers, and development tools that need to understand module dependencies.

npm install parse-imports
INSTALL
IMPORT
SIG · PARSE-IMPORTS
P
parse-imports
serializationjavascriptv3.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parseImports
import { parseImports } from 'parse-imports'
import parseImports from 'parse-imports'
Since v2.0.0, `parseImports` is a named export. Default imports will not work. CommonJS `require()` is not supported directly in v3.0.0.
parseImportsSync
import { parseImportsSync } from 'parse-imports'
const { parseImportsSync } = require('parse-imports')
A synchronous version of the parser, introduced in v2.0.0. Primarily for ESM consumers.
Any Symbol (CommonJS)
const { parseImports } = await import('parse-imports')
const parseImports = require('parse-imports')
For CommonJS environments, direct `require()` is problematic in v3.0.0. Use dynamic `import()` to load the ESM module.

Demonstrates parsing ES module imports from a code string, showing both asynchronous iteration and conversion to an array of import objects, highlighting different import types.

import { parseImports } from 'parse-imports' const code = ` import a from 'b' import * as c from './d' import { e as f, g as h, i } from '/j' import k, { l as m } from 'n' import o, * as p from "./q" import r, { s as t, u } from "/v" import fs from 'fs' ;(async () => { await import("w") await import("x" + "y") })() ` async function main() { console.log('--- Iterating over imports ---'); for (const $import of await parseImports(code)) { console.log(`Type: ${$import.moduleSpecifier.type}, Value: ${$import.moduleSpecifier.value || $import.moduleSpecifier.code}, Dynamic: ${$import.isDynamicImport}`) } console.log('\n--- Accessing as an array ---'); const imports = [...(await parseImports(code))]; console.log('First import:', imports[0]); console.log('Seventh import (dynamic):', imports[7]); } main().catch(console.error);
Debug
Known issues
breakingVersion 3.0.0 requires Node.js version 22 or higher. Running on older Node.js versions will result in runtime errors.
fix
Upgrade your Node.js runtime to version 22 or later.
affects: >=3.0.0
breakingVersion 2.0.0 introduced a minimum Node.js requirement of 18 or higher. Projects on older Node.js versions must remain on `parse-imports` v1.x or upgrade their Node.js environment.
fix
Upgrade your Node.js runtime to version 18 or later, or downgrade `parse-imports` to v1.x.
affects: >=2.0.0 <3.0.0
breakingSince version 2.0.0, the primary export `parseImports` changed from a default export to a named export. Using `import parseImports from 'parse-imports'` will cause a `TypeError`.
fix
Update your import statements to use named imports: `import { parseImports } from 'parse-imports'`.
affects: >=2.0.0
gotchaWhile v2.1.0 added a CommonJS entry point, v3.0.0 is primarily designed for ES Modules and Node.js environments >= 22. Attempting to use `require()` directly might lead to issues like `ERR_REQUIRE_ESM`.
fix
For CommonJS files, use dynamic `import()` (e.g., `const { parseImports } = await import('parse-imports');`) or convert your consuming module to ES Modules.
affects: >=3.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use CommonJS `require()` syntax for `parse-imports` in an ES Module context.
fix
Use ES module import syntax: `import { parseImports } from 'parse-imports'`.
TypeError: parseImports is not a function
Attempting to import `parseImports` as a default import (e.g., `import parseImports from 'parse-imports'`) instead of a named import, which changed in v2.0.0.
fix
Use named import syntax: `import { parseImports } from 'parse-imports'`.
ERR_REQUIRE_ESM: Must use import to load ES Module
Trying to `require('parse-imports')` from a CommonJS module when the package is fundamentally an ES Module.
fix
Convert your consuming file to an ES Module (e.g., change `.js` to `.mjs` or add `"type": "module"` to `package.json`), or use dynamic `import()` within CJS: `const { parseImports } = await import('parse-imports');`.
Error: This module requires Node.js 22 or later.
Running `parse-imports` v3.0.0 on an unsupported Node.js version.
fix
Upgrade your Node.js runtime to version 22 or higher. Alternatively, downgrade `parse-imports` to a version compatible with your current Node.js environment (e.g., v2.x for Node.js >=18 <22, v1.x for Node.js <18).
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
es-module-lexerrequiredCore WASM-based parser used under the hood for efficient AST traversal.
Agent activity
10 hits · last 30 days
node
8
Resources
parse-imports — npm install parse-imports · libregistry