How to determine if NPM package supports ES5 or ES6 type of module ?
Asked
Active
Viewed 624 times
0
-
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 Answers
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