How to check if an NPM module is an ESM module
So you need to check if a file is in ESM module. Apparently there's two things that you need to look for in package.json
You need the type to be module
, so you are looking for something like:
{
"type": "module"
}
Another hint you are looking at an ESM module is the exports directive:
{
"exports": {
"./feature": "./src/feature.js"
}
}
If you want to check to see which modules in your node_modules
directory are ESM modules, you could use this shell command:
fgrep -l '"type": "module"' node_modules/*/package.json | xargs fgrep -l exports
That should show you all file names which have both type==module and have an exports section present.
Oh wait, here is a version which should be a little more robust to unusual formatting :
grep -l 'type.module' node_modules//package.json | xargs fgrep -l exports
That's it, that's one way to confirm an ESM module, or find ESM modules in a node_modules directory.