Socket
Book a DemoInstallSign in
Socket

@knighted/specifier

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@knighted/specifier

Node.js tool for updating your ES module and CommonJS specifiers.

2.0.7
latest
Source
npmnpm
Version published
Weekly downloads
1K
-53.06%
Maintainers
1
Weekly downloads
 
Created
Source

@knighted/specifier

CI codecov NPM version

Node.js tool for parsing imports to change ESM and CJS specifiers.

  • Rewrite specifier values.
  • Read metadata about a specifier's AST node.
  • Updates files or strings.
  • Parses import, import(), import.meta.resolve(), export, require, and require.resolve().

Example

Given a file with some imports and exports:

// file.ts

import { someLib } from 'some-package'
import { foo } from './path/to/foo.js'

export { bar } from './path/to/bar.js'

You can use specifier to change the values:

import { specifier } from '@knighted/specifier'

const update = await specifier.update('file.ts', ({ value }) => {
  if (value === 'some-package') {
    return 'some-package/esm'
  }

  return value.replace('.js', '.mjs')
})

console.log(update)

/*
import { someLib } from 'some-package/esm'
import { foo } from './path/to/foo.mjs'

export { bar } from './path/to/bar.mjs'
*/

Or collect the AST nodes:

import { type Spec, specifier } from '@knighted/specifier'

const nodes: { node: Spec['node']; parent: Spec['parent'] }[] = []

await specifier.update('file.ts', ({ node, parent }) => {
  nodes.push({ node, parent })
})

nodes.forEach(({ node, parent }) => {
  // Do something with the metadata
})

Keywords

parse imports

FAQs

Package last updated on 26 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.