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 🙏

© 2024 – Pkg Stats / Ryan Hefner

just-import

v1.0.0

Published

Fix ERR_REQUIRE_ESM with this module.

Downloads

5

Readme

npm bundle size npm type definitions npm version npm license

just-import

If you are getting the following error, this simple library can help:

Error [ERR_REQUIRE_ESM]: require() of ES Module ... from ... not supported.
Instead change the require of ... in ... to a dynamic import() which is available in all CommonJS modules.

You are likely getting this error because you are transpiling ESM or TypeScript to CommonJS, and no matter what, the emitted code uses require to import libraries.

Libraries written in ESM style cannot be required'd. They must use the native Node import function.

This library does nothing except wrap the import function in justImport, isolating it from your compile process.

Here is how you can fix your problem using split-chars as an example:

import { justImport } from 'just-import'

async function splitChars (input: string) {
  const splitChars = await justImport<Iterable<string>>('split-chars')
  return splitChars.default(input)
}

or in TypeScript

import { justImport } from 'just-import'

type SplitChars = {
  default: (input: string) => Iterable<string>
}

async function splitChars (input: string): Promise<Iterable<string>> {
  const { default:splitChars } = await justImport<SplitChars>('split-chars')
  return await splitChars(input)
}

Please see EXAMPLES for more examples.

Can't the offending module be imported just like any other so the function doesn't have to be async?

Sadly, no. Since import returns a promise, so too mush justImport. require is synchronous, but again, the module can't me imported using require.

If you're not going to use the module right away, there is an unsafe way to import it:

import { justImport } from 'just-import'

let splitChars = null
justImport('split-chars').then(mod => {
  splitChars = mod.default
})

function myCode () {
  return splitChars('🍍+🍕=😋')
}

or in TypeScript

import { justImport } from 'just-import'

let splitChars: (input: string) => Iterable<string> = null
justImport('split-chars').then(mod => {
  splitChars = mod.default
})