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

require-from-es

v1.0.3

Published

This module, require-from-es, makes require, __dirname and __filename work flawlessly in ES-modules.

Downloads

1

Readme

require-from-es

This module, require-from-es, makes require, __dirname and __filename work flawlessly in ES-modules.

So you finally made the switch to using ES modules in Node.js?

Congratulations!

So you added the following in your package.json (or maybe a build tool like Vite added it...):

"type" : "module"

and you are ready to start using import and export everywhere? Those ARE good intentions.

But what about:

  • old legacy code (your own and npm modules) that only works with require?
  • or the convenience of requiring a json-file with require('x.json')?
  • or being able to read the absolute path of your file with __filename?
  • or the dir name where your file is at with __dirname?

All of this has just gone out the window...

But, fear not, just:

npm i require-from-es

And then once, at the top/start file of your application:

import 'require-from-es';

Now you can use require, __filename and __dirname anywhere in your application. No need to include something extra in each file.

More info, less important

Tested on Mac, Windows and Linux

Since requiring files requires (pun intended) a lot of juggling of file paths and those being slightly different on different OS:es, we've tested require-from-es on both Mac, Linux and Windows.

The result of our tests: It works!

How? (Only for nerds)

You're a nerd? That's good. (If not stop reading.)

So how did we do it? Well, recreating require in ES modules is done by using the function createRequire (built in to native Node.js module 'module').

The problem is that createRequire demands you to input import.meta.url as an argument. That's the same as the path to the file your code is running in.

And it is different for each file... But we are reading this path from a stack trace (carefully parsed to find the file path of the file calling require).

Then we create getters for require, __dirname and __filename in the global namespace (globalThis)... among with some other trickery to make require work in several steps down the file hierarchy :).

This means: require is created using the correct 'base path' everywhere. And you only have to import require-from-es once in your project.