npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

oxc-cjs2esm

v0.1.2

Published

Convert CommonJS modules to ESM modules using oxc-parser

Readme

oxc-cjs2esm

npm version License: MIT

一个使用 Oxc-Parser 将 CommonJS 模块转换为 ESM 模块的轻量级工具。

English README

安装

npm install oxc-cjs2esm
yarn add oxc-cjs2esm
pnpm add oxc-cjs2esm

使用

import { cjs2esm } from 'oxc-cjs2esm';

const cjsCode = `
const a = require('./a.js');

module.exports = {
  a: a,
};
`;

const esmCode = cjs2esm(cjsCode, { filename: 'example.js' });

console.log(esmCode);
// import a from './a.js';
//
// export default { a };

API

cjs2esm(code: string, options?: TransformOptions): string

将 CommonJS 代码转换为 ESM 代码。

参数:

| 参数 | 类型 | 必填 | 说明 | | --------- | ------------------ | ---- | ------------------------ | | code | string | 是 | 待转换的 CommonJS 源代码 | | options | TransformOptions | 否 | 转换选项 |

TransformOptions

| 属性 | 类型 | 说明 | | ---------- | -------- | ------------------------------------------- | | filename | string | 文件名,用于解析错误提示,默认 'input.js' |

返回值: 转换后的 ESM 代码字符串。

无法转换的 CJS 语法(动态 require、空解构、计算属性导出、非字面量路径的独立 require)会触发 console.warn,并输出行号、列号和代码片段。

支持的转换

| CommonJS | ESM | | ------------------------------------------ | --------------------------------------------------------------------------- | | const x = require('./x.js') | import x from './x.js' | | var x = require('./x.js') | import x from './x.js' | | let x = require('./x.js') | import x from './x.js' | | const { a } = require('./x.js') | import { a } from './x.js' | | const { a, b } = require('./x.js') | import { a, b } from './x.js' | | const { a: foo } = require('./x.js') | import { a as foo } from './x.js' | | const { a, ...rest } = require('./x.js') | import * as __cjs2esm_0 from './x.js'; const { a, ...rest } = __cjs2esm_0 | | const [a, b] = require('./x.js') | import * as __cjs2esm_0 from './x.js'; const [a, b] = __cjs2esm_0 | | const { a = 1 } = require('./x.js') | import * as __cjs2esm_0 from './x.js'; const { a = 1 } = __cjs2esm_0 | | const a = require('a'), b = require('b') | import a from 'a'; import b from 'b' | | require('./init.js') | import './init.js' | | module.exports = { b: require('./b.js') } | import __cjs2esm_0 from './b.js'; export default { b: __cjs2esm_0 } | | module.exports = expr | export default expr | | exports.foo = expr | export const foo = expr | | module.exports.foo = expr | export const foo = expr |

限制

  • 仅转换字面量路径require(),动态 require(getPath())、模板字符串 require(\./${path}`)` 保持不变
  • 计算属性形式的 exports[key] = value 不转换

License

MIT