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 🙏

© 2025 – Pkg Stats / Ryan Hefner

transform-esm-to-commonjs

v0.3.0

Published

transfrom esm import to commonjs require.

Readme

transform-modules-to-commonjs

transfrom import to require.

usage

install

npm i transform-modules-to-commonjs

sample

import transformModulesToCommonjs from 'transform-modules-to-commonjs'

const module = `
import React, { useEffect } from 'react'
import data from './bar.json' with { type: 'json' }
import * as vue from 'vue'

export default async function App() {
  const yaml = await import('yaml')
  return <div>app</div>
}

const foo: string = 'bar'
export { foo }

export const c = ''
`.trim()

transformModulesToCommonjs(module)

output:

let __require__ = (...args) => new Promise((r) => r(require(...args)))
let React = require('React')
let { useEffect } = React
let data = require('data')
let vue = require('vue')

exports.default = async function App() {
  const yaml = await __require__('yaml')
  return <div>app</div>
}

const foo: string = 'bar'
exports.foo = foo

exports.c = c

note

The results of this library differ babel and sucrase.

Syntax like: import * as React from 'react' will be same as import React from 'react'.

Part of the reason is that this repository only uses part of the oxc-parser functionality and does not involve AST, and the other part is that this repository is not used for bundle, see the next section to learn more.

why build this repo

oxc only transfrom code esm format, commonjs is not planned, see https://github.com/oxc-project/oxc/issues/4050.

commonjs is easy to run with Function or eval, this is useful for live react/vue component. For example:

import React from 'react'

const commonjs = `
const React = require('react')
exports.default = function() {
  return React.createElement('div', 'app')
}`
const scope = {
  react: React,
}
const exports = {}
new Function('exports', 'require', commonjs)(exports, (key) => scope[key])

console.log(exports.default) // will be react component

Based on above code, The esm format code like:

import * as React from 'react'
export default function () {
  return React.default.createElement('div', 'bar')
}

transfrom to:

const React = require('react')
export default function () {
  return React.default.createElement('div', 'bar')
}

To get react component by syntax import * as React from 'react', the code should be:

// just like esm format code import statement
import * as React from 'react'
const scope = {
  react: React,
}

const esmCode = `
import * as React from 'react'
export default function () {
  return React.default.createElement('div', 'bar')
}
`.trim()

const cjsCode = transformModulesToCommonjs(esmCode)
const exports = {}
new Function('exports', 'require', commonjs)(exports, (key) => scope[key])

console.log(exports.default) // react component