Node-mysql2: ERR_PACKAGE_PATH_NOT_EXPORTED with version > 2.1.0

Created on 23 Sep 2020  路  8Comments  路  Source: sidorares/node-mysql2

If I install [email protected] and run the following:

"use strict";

const mysql = require("mysql2");
const ClientFlags = require("mysql2/lib/constants/client.js");

It throws:

internal/modules/cjs/loader.js:518
  throw new ERR_PACKAGE_PATH_NOT_EXPORTED(basePath, mappingKey);
  ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/constants/client.js' is not defined by "exports" in /Users/VisualStudio/node_modules/mysql2package.json

With [email protected] there is no error.

Not sure if its the cause but note the missing slash at the end of the error: node_modules/mysql2package.json

Most helpful comment

@chrisveness I'd like to avoid promoting access to ./lib as "official" solution to any problem and rather think of some api accessible from top level import

All 8 comments

I have the same issue but I'm using mysql2/promise.js:

import mysql from "mysql2/promise.js";

Defining a pool and executing the query:

const pool = await mysql.createPool({
    connectionLimit: CONFIG_ENV.dbConnLimit,
    database: CONFIG_ENV.dbName,
    host: CONFIG_ENV.dbHost,
    password: CONFIG_ENV.dbPass,
    queueLimit: 0,
    user: CONFIG_ENV.dbUser,
    waitForConnections: true
});

await pool.query(QUERIES.myQuery, [鈥);

When I run my app, I get the following error:

internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './promise.js' is not defined by "exports" in C:\鈥node_modules\mysql2\package.json imported from C:\鈥src\server\bin\DAL\DALDriver.m
js
at new NodeError (internal/errors.js:253:15)
at throwExportsNotFound (internal/modules/esm/resolve.js:290:9)
at packageExportsResolve (internal/modules/esm/resolve.js:513:3)
at packageResolve (internal/modules/esm/resolve.js:644:14)
at moduleResolve (internal/modules/esm/resolve.js:696:18)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)
at Loader.resolve (internal/modules/esm/loader.js:85:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:229:28)
at ModuleWrap. (internal/modules/esm/module_job.js:51:40)
at link (internal/modules/esm/module_job.js:50:36) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

With [email protected] there was no such issue.

What's strange, after rollback from 2.2.5 to 2.1.0 I still observe such issue:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './promise.js' is not defined by "exports" in C:\鈥node_modules\mysql2\package.json imported from C:\鈥src\server\bin\DAL\DALDriver.m
js
?[90m at new NodeError (internal/errors.js:253:15)?[39m
?[90m at throwExportsNotFound (internal/modules/esm/resolve.js:290:9)?[39m
?[90m at packageExportsResolve (internal/modules/esm/resolve.js:513:3)?[39m
?[90m at packageResolve (internal/modules/esm/resolve.js:644:14)?[39m
?[90m at moduleResolve (internal/modules/esm/resolve.js:696:18)?[39m
?[90m at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)?[39m
?[90m at Loader.resolve (internal/modules/esm/loader.js:85:40)?[39m
?[90m at Loader.getModuleJob (internal/modules/esm/loader.js:229:28)?[39m
?[90m at ModuleWrap. (internal/modules/esm/module_job.js:51:40)?[39m
?[90m at link (internal/modules/esm/module_job.js:50:36)?[39m {
code: ?[32m'ERR_PACKAGE_PATH_NOT_EXPORTED'?[39m
}

PR #1100 specified 'promise' as an export, which meant that previous implicit exports were no longer available.

I have offered a PR (#1217) which restored 'promise.js' as an export; @jaydenseric suggested a number of further exports including everything in ./lib/ (https://github.com/sidorares/node-mysql2/pull/1100#issuecomment-616126810).

I don't know whether there might be any risk associated with any other exports, but I would think exporting ./lib/ should be safe, and would fix this issue.

@sidorares would you like me to offer another PR?

@pubmikeb I believe you can resolve your issue with the current version by using import mysql from "mysql2/promise"; (without the .js).

@chrisdew, thanks it worked out for me!

@pubmikeb after next release you should be able to use both mysql2/promise and mysql2/promise.js

@chrisveness I'd like to avoid promoting access to ./lib as "official" solution to any problem and rather think of some api accessible from top level import

@sidorares, thanks a lot.
BTW, is there any difference in approaches importing mysql2/promise and mysql2/promise.js? Or it is just a matter of a taste/aesthetic?

I personally prefer full path as to me it's a bit more deterministic ( if you have require('./path/a/b)` you can change what is acutually required bu adding different files - folder "b" with index.js, b.json etc etc. Also might be a bit more performant ( it's a tiny bit, but when you have 1000 of files required at startup bu dependencies of dependencies that can add up )

Was this page helpful?
0 / 5 - 0 ratings