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

esm-utils

v4.3.0

Published

Utilities you'll need when migrating to ESModule.

Downloads

310,544

Readme

esm-utils

Build Status Coverage Npm Version MIT License

Utilities you'll need when migrating to ESModule.

Install

yarn add esm-utils

Usage

import createEsmUtils from 'esm-utils'

const {
  dirname,
  filename,
  require,
  importModule,
  resolve,
  readJson,
  readJsonSync,
} = createEsmUtils(import.meta)
/* Those named exports only accept absolute path or URL */
import {
  importModule,
  readJson,
  loadJson,
  readJsonSync,
  loadJsonSync,
} from 'esm-utils'

API

createEsmUtils(import.meta | URL | 'string')

Returns an object with the following properties:

  • dirname (alias __dirname)
  • filename (alias __filename)
  • require
  • importModule (alias import)
  • resolve
  • readJson (alias loadJson)
  • readJsonSync (alias loadJsonSync)

Please read this note before you use dirname and filename

Sync version of readJson.

utils.importModule(string | URL, options?)

Same as import(), but accepts absolute path (on Windows, import('C:\\foo.js') error throws when pass a absolute path starts with a drive letter).

options.traceSyntaxError

type: boolean
default: false

Due to this Node.js issue, Node.js does not emit the location of the syntax error in the error thrown in dynamic import().

When set traceSyntaxError: true, we'll try to get a better error message by running node <file> in a child process.

utils.readJson(string | URL)

Returns Promise<jsonObject>.

utils.readJsonSync(string | URL)

Sync version of utils.readJson

utils.resolve(string | URL)

Ponyfill for import.meta.resolve.

If import.meta.resolve exits, use it directly, otherwise returns a simple wrapper of import-meta-resolve.

Import json file

With Import Assertions

import foo from './foo.json' assert {type: 'json'}
await import('./foo.json', {assert: {type: 'json'}})

With require, like CommonJS

import createEsmUtils from 'esm-utils'

const {require} = createEsmUtils(import.meta)
const foo = require('./foo.json')

With readJson or readJsonSync

import createEsmUtils from 'esm-utils'

const {readJson} = createEsmUtils(import.meta)
const foo = await readJson('./foo.json')
import createEsmUtils from 'esm-utils'

const {readJsonSync} = createEsmUtils(import.meta)
const foo = readJsonSync('./foo.json')

importModule()

Same as utils.importModule(), but only accept absolute path or URL.

readJson() (alias loadJson)

Same as utils.readJson(), but only accept absolute path or URL.

readJsonSync() (alias loadJsonSync)

Same as utils.readJsonSync(), but only accept absolute path or URL.

You don't need dirname and filename

The dirname and filename supposed to be a quick solution when migrating to ES Modules. In most cases, you don't need them, because many APIs accept URL directly.

/* BAD */
import fs from 'node:fs/promises'
import path from 'node:path'
import createEsmUtils from 'esm-utils'

const {dirname} = createEsmUtils(import.meta)
const buffer = await fs.readFile(
  path.join(dirname, './path/to/file')
)
/* GOOD */
import fs from 'node:fs/promises'

const buffer = await fs.readFile(
  new URL('./path/to/file', import.meta.url)
)