Can we import from CommonJS modules in Node now using ESM import syntax?
So if commonjsmodule
is a cjs module that exports like this:
exports.add = function() {...}
Can we import from it like this assuming that we are working within ESM?
import { add } from "commonjsmodule";
In this YouTube video the author is demoing the type
property and what it does in package.json
.
And he does it by creating one cjs file and one mjs file and an app and in the app he's importing from both using ESM import syntax, and I just assumed that additional tooling would be needed for the cjs file.
Yes, in Node.js it's possible to import
CommonJS modules from ECMAScript modules. The documentation has more information.
This has been the case for as long as Node.js has supported ECMAScript module syntax (I just tested to verify that it works as far back as v8.5.0).