Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERR_PACKAGE_PATH_NOT_EXPORTED error in test/config.js in Node.js nightly when requiring a subpath of @elastic/elasticsearch #2350

Closed
trentm opened this issue Sep 27, 2021 · 1 comment · Fixed by #2351
Assignees
Labels
agent-nodejs Make available for APM Agents project planning.

Comments

@trentm
Copy link
Member

trentm commented Sep 27, 2021

Recently a test run against the latest node v17 nightly build started failing as follows:

$ node test/config.js
...
# disableInstrumentations
node:internal/modules/cjs/loader:488
      throw e;
      ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports" in /Users/trentm/el/apm-agent-nodejs/node_modules/@elastic/elasticsearch/package.json
    at new NodeError (node:internal/errors:371:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:416:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:669:3)
    at resolveExports (node:internal/modules/cjs/loader:482:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Test.<anonymous> (/Users/trentm/el/apm-agent-nodejs/test/config.test.js:863:19) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node.js v17.0.0-nightly20210923c7da13c7c4

It started failing on this nightly build:

PASS v17.0.0-nightly202109220991dfcece/                 22-Sep-2021 07:00
FAIL v17.0.0-nightly20210923c7da13c7c4/                 23-Sep-2021 07:30

which means one of these commits: nodejs/node@0991dfc...c7da13c
and almost certainly this PR nodejs/node#40121
which changed a DeprecationWarning, e.g.:

% node
Welcome to Node.js v16.9.1.
Type ".help" for more information.
> require('@elastic/elasticsearch/package.json').version
'7.14.0'
> (node:95433) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at /Users/trentm/el/apm-agent-nodejs/node_modules/@elastic/elasticsearch/package.json.
Update this package.json to use a subpath pattern like "./*".
(Use `node --trace-deprecation ...` to show where the warning was created)

to a runtime error:

% ~/tmp/node-v17.0.0-nightly20210923c7da13c7c4-darwin-x64/bin/node
Welcome to Node.js v17.0.0-nightly20210923c7da13c7c4.
Type ".help" for more information.
> require('@elastic/elasticsearch/package.json').version
Uncaught:
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports" in /Users/trentm/el/apm-agent-nodejs/node_modules/@elastic/elasticsearch/package.json
    at __node_internal_captureLargerStackTrace (node:internal/errors:464:5)
    at new NodeError (node:internal/errors:371:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:416:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:669:3)
    at resolveExports (node:internal/modules/cjs/loader:482:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

The code in question doing this, in this case, is here:

var esVersion = require('@elastic/elasticsearch/package.json').version

Note that the agent does this kind of thing in other places, including in the runtime, not just the test suite:

lib/agent.js
230:    // Attempt to load package.json from process.argv.
234:      pkg = require(path.join(basedir, 'package.json'))

lib/instrumentation/index.js
190:      pkg = path.join(basedir, 'package.json')

I'm not yet sure if this can/will affect instrumentation of @elastic/elasticsearch.

Note as well that the ES client already has elastic/elasticsearch-js#1465 regarding the DeprecationWarning.

@trentm trentm self-assigned this Sep 27, 2021
@github-actions github-actions bot added the agent-nodejs Make available for APM Agents project planning. label Sep 27, 2021
@trentm trentm changed the title ERR_PACKAGE_PATH_NOT_EXPORTED error in test/config.js in Node.js nightly when requiring a subpath of @elastic/elasticsearc ERR_PACKAGE_PATH_NOT_EXPORTED error in test/config.js in Node.js nightly when requiring a subpath of @elastic/elasticsearch Sep 27, 2021
@trentm
Copy link
Member Author

trentm commented Sep 27, 2021

Looking at runtime usage of require('.../package.json'):

  1. In "lib/instrumentation/index.js", the package.json is being read and parsed via version = JSON.parse(fs.readFileSync(pkg)).version so is not susceptible to this "exports" issue.
  2. In "lib/agent.js", an attempt is made to require("$CWD/package.json") for debug logging on startup. This is guarded in a try/catch, so won't crash the app. It is a separate discussion if that require could be improved or dropped.
  3. Reading of package.json in the 'read-pkg-up' dep uses fs.readFileSync and JSON.parse, so we are also good there.

tl;dr: So far this is only impacting tests, which is good.

trentm added a commit that referenced this issue Sep 27, 2021
The tests were using `require('@elastic/elasticsearch/package.json')`
which runs afoul of EOL'd folder mappings in "exports"
(https://nodejs.org/api/all.html#DEP0148) with current ES client
versions and the latest node v17 nightly.

Fixes: #2350
trentm added a commit that referenced this issue Sep 27, 2021
The tests were using `require('@elastic/elasticsearch/package.json')`
which runs afoul of EOL'd folder mappings in "exports"
(https://nodejs.org/api/all.html#DEP0148) with current ES client
versions and the latest node v17 nightly.

Fixes: #2350
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-nodejs Make available for APM Agents project planning.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant