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

fix-es-imports

v0.1.4

Published

Fixes your ES import paths - from Node-style to explicit filenames

Downloads

17

Readme

fix-es-imports

Fixes your ES import paths - from Node-style to explicit filenames

What

Turns:

import fs from 'fs';
import x from './x';
import { y } from './y';
export { default as z } from './z';

Into:

import fs from 'fs';
import x from './x.mjs';
import { y } from './y/index.js';
export { default as z } from './z.json';

How

npx fix-es-imports

Why

Did you use ES module syntax before it was ~cool~ implemented? And then maybe didn't completely respect the exact rules around explicit filenames? But it was okay, right, since Babel didn't really mind...

And then you used --experimental-modules with .mjs files from Node 8.9.0, and it pretty much worked perfectly. And you were super happy you could finally stop using Babel with Node code.

And then Node 11.4.0 came along, and it all stopped working. They were actually enforcing the rules now! Oops. And maybe there are a few thousand references to deal with, so maybe you left it for a while.

At some point, --es-module-specifier-resolution=node came along, and it saved the day - things worked as normal again! But you knew this wouldn't be a long term solution - one day you'd have to fix the imports once and for all.

And now Node 13.2.0 has shipped. ES module support is no longer experimental. You conclude you've pushed it off long enough. The time has come.

This tool is for you.

Caveats

Regex-based parsing

Currently this tool is implemented with a fairly primitive regex, rather than any kind of real parser. As such, it is not expected to work in every situation.

Some cases known not to work:

  • inclusion of any quote characters within the path (even if escaped)
  • anything prior to the declaration on the same line (including whitespace)
  • declarations not containing any whitespace (e.g. import'x';)
  • declarations where the path string is not immediately followed by a semicolon and the end of the line

The reason for the use of regex rather than e.g. Babel is because we want to leave every other part of the file exactly as-is, with absolutely no unnecessary changes.

If you're keen to implement a better approach, please open a PR!

Non-JS modules

import foo from './foo.json';

At this stage, using module declarations with non-JS files is still experimental and the syntax for doing this will almost certainly change due to security issues affecting the web.

This tool will work with such files if the correct flag is enabled, but you should really look to load these without module declarations until the final syntax is determined.

Package files

import fs from 'mz/fs';

Not currently supported. Each instance found will be outputted to stderr. You will need to manually change these.

In this case, to:

import fs from 'mz/fs.js';

If you can, best not to use this kind of import for now. Likely to result in unexpected breaking changes by package developers unfamiliar with the explicit filename rules.

Support

Please open an issue on this repository.

Authors

License

MIT licensed - see LICENSE file