0

How to determine if NPM package supports ES5 or ES6 type of module ?

Matko
  • 3,386
  • 4
  • 21
  • 35
  • For node.js packages, just see whether they use the `.mjs` extension. You can also [look at the package.json](https://stackoverflow.com/q/42708484/1048572) – Bergi Apr 30 '20 at 21:38
  • You can check this - https://www.npmjs.com/package/are-you-es5 – Kiran Jun 17 '22 at 11:12

1 Answers1

0

This is a difficult question to answer with certainty, because it requires parsing JS -- a notoriously difficult problem.

If you're okay with a solution that gives the right answer most of the time -- I have a build system which transpiles ESMs with Babel in a mixed CommonJS/ESM environment with automatic dependency discovery. How I figure out if a given file is ESM or not is

egrep '^\s*import\s+.*\sfrom\s+' "$filename"

There are obvious situations where this will false-trigger, but it has served me well.

Wes
  • 950
  • 5
  • 12