Search code examples
javascriptnode.jses6-modulescommonjses6-module-loader

Can't import "mysql2/promise" into ES module (MJS) on Node.js 13 / 14


While migrating from CommonJS (.cjs) to ES Modules (.mjs), I've faced an issue of importing namespaced CJS to MJS:

import mysqlPromise from "mysql2/promise";

returns an error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\User\IdeaProjects\…\node_modules\mysql2\promise' imported from…

I checked checked the relevant thread ES Module support? Node 13, but don't see a clear solution.

Is there any way to import a namespaced CJS to ES Module/MJS?


Solution

  • Try using:

    import mysqlPromise from "mysql2/promise.js";