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]: No "exports" main defined in package.json #382

Closed
borkdude opened this issue Apr 13, 2022 · 29 comments
Closed

Comments

@borkdude
Copy link

borkdude commented Apr 13, 2022

Nbb, a ClojureScript interpreter for Node.js uses dynamic import to load libraries. To resolve which JS file to load, it uses code like the following:

import { createRequire } from 'module';

const req = createRequire(import.meta.url);

console.log(req.resolve('zx'));

This code worked in zx 5.3.0 but stopped working in 6.0.7 with the following error:

$ node script.mjs
path
node:internal/errors:465
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /private/tmp/test-nbb/node_modules/zx/package.json
    at new NodeError (node:internal/errors:372:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:433:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:657:7)
    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.resolve (node:internal/modules/cjs/helpers:108:19)
    at file:///private/tmp/test-nbb/script.mjs:6:17
    at ModuleJob.run (node:internal/modules/esm/module_job:197:25)
    at async Promise.all (index 0) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
@antonmedv
Copy link
Collaborator

Can't tell what is wrong.

@antonmedv
Copy link
Collaborator

No looking as zx issue,

@borkdude
Copy link
Author

@antonmedv The structure of package.json did change between 5.3.0 and 6.0.0, could it have something to do with that?

@antonmedv
Copy link
Collaborator

IDK

@antonmedv
Copy link
Collaborator

Can't repoduce.

@borkdude
Copy link
Author

What did you try to reproduce? Here is a detailed reproduction using Node 14 and Node 17:

borkdude@MBP2019 /tmp $ mkdir zxtest
borkdude@MBP2019 /tmp $ cd zxtest
borkdude@MBP2019 /tmp/zxtest $ npm install zx
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'zx@6.1.0',
npm WARN EBADENGINE   required: { node: '>= 16.0.0' },
npm WARN EBADENGINE   current: { node: 'v14.18.0', npm: '8.5.4' }
npm WARN EBADENGINE }

added 52 packages, and audited 53 packages in 3s

9 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
borkdude@MBP2019 /tmp/zxtest $ cat package.json
{
  "dependencies": {
    "zx": "^6.1.0"
  }
}
borkdude@MBP2019 /tmp/zxtest $ cat index.mjs
import { createRequire } from 'module';

const req = createRequire(import.meta.url);

console.log(req.resolve('zx'));
borkdude@MBP2019 /tmp/zxtest $ node index.mjs
internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /private/tmp/zxtest/node_modules/zx/package.json
    at new NodeError (internal/errors.js:322:7)
    at throwExportsNotFound (internal/modules/esm/resolve.js:322:9)
    at packageExportsResolve (internal/modules/esm/resolve.js:511:7)
    at resolveExports (internal/modules/cjs/loader.js:450:36)
    at Function.Module._findPath (internal/modules/cjs/loader.js:490:31)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:888:27)
    at Function.resolve (internal/modules/cjs/helpers.js:99:19)
    at file:///private/tmp/zxtest/index.mjs:5:17
    at ModuleJob.run (internal/modules/esm/module_job.js:183:25)
    at async Loader.import (internal/modules/esm/loader.js:178:24) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
borkdude@MBP2019 /tmp/zxtest $ node --version
v14.18.0
borkdude@MBP2019 /tmp/zxtest $ nvm use 17
Now using node v17.8.0 (npm v8.5.5)
borkdude@MBP2019 /tmp/zxtest $ node --version
v17.8.0
borkdude@MBP2019 /tmp/zxtest $ node index.mjs
node:internal/errors:465
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /private/tmp/zxtest/node_modules/zx/package.json
    at new NodeError (node:internal/errors:372:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:448:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:672:7)
    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.resolve (node:internal/modules/cjs/helpers:108:19)
    at file:///private/tmp/zxtest/index.mjs:5:17
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node.js v17.8.0

@antonmedv
Copy link
Collaborator

Node 14 not supported.

@borkdude
Copy link
Author

It is also is reproducible using 16 and 17 (as shown above for 17).

@antonmedv
Copy link
Collaborator

Let me again

@antonmedv
Copy link
Collaborator

antonmedv commented Apr 15, 2022

Debugged. zx does not provide CJS version starting with v6.

The error ERR_PACKAGE_PATH_NOT_EXPORTED, is thrown by using require() on zx. This is not supported by node. Use import().

You can try this:

console.log(await import.meta.resolve('zx'));
❯ node --experimental-import-meta-resolve index.mjs
file:///Users/anton/dev/zx-playground/node_modules/zx/src/index.mjs

@borkdude
Copy link
Author

borkdude commented Apr 15, 2022

Thanks for debugging. I think it is supported as you can see in this ESM documentation:

https://nodejs.org/api/module.html#modulecreaterequirefilename

I am not calling require, I'm only using its resolve mechanism. It works on libraries like execa which are also ESM only.

Unfortunately I can't use import.meta.resolve.

@antonmedv
Copy link
Collaborator

It works on libraries like execa which are also ESM only.

Can't tell why zx does not work the same way. ¯_(ツ)_/¯

@borkdude
Copy link
Author

Maybe we will one day figure it out, thanks for the time and help :).

@borkdude
Copy link
Author

@antonmedv I may have found the issue. When I edit package.json in node_modules/zx and change the main export to:

  "exports": {
    ".": "./src/index.mjs",

it starts working. That seems more like the schema they describe here:

https://nodejs.org/api/packages.html#subpath-exports

@antonmedv
Copy link
Collaborator

I see. But we can't ditch types. Also conditional exports possible: https://nodejs.org/api/packages.html#conditional-exports which zx uses.

Two possible ways: maybe an issue in require.resolve? Or how to define types not in exports?

@borkdude
Copy link
Author

You're right, I missed that. I'll file a bug with Node.js.

@borkdude
Copy link
Author

Now I'm not sure if this is a Node.js bug either since when I add:

      "import": "./src/index.mjs",
      "require": "./src/index.mjs",

then it also works again. I guess I'm stuck for now then, until I can use import.meta.resolve.

@borkdude
Copy link
Author

I can use https://github.com/wooorm/import-meta-resolve as a workaround.

@borkdude
Copy link
Author

borkdude commented Apr 16, 2022

All fixed now, nbb 0.3.7 works with zx 6.0.0+:

(ns script
  (:require ["zx" :refer [$]]
            [nbb.core :refer [await]]))

(await ($ #js ["ls" "-la"]))
(prn :foo 1 2 3)

@akefirad
Copy link

Is there any workaround for this issue? (Why did the issue get closed?) I can reproduce the issue with the latest versions (node: 16.4.1, zx: v6.1.0). Thanks.

@borkdude
Copy link
Author

borkdude commented Apr 18, 2022 via email

@akefirad
Copy link

akefirad commented Apr 19, 2022

There you go: zx-bug.zip. Forgot to mentioned I'm using it via ts-node. There's no custom code/configuration in the project whatsoever. Thanks.
Update: as other mentioned, version 4 works just fine.

@LeonAlvarez
Copy link

I confirm the same issue on the latest version, version 4.3 works fine

@borkdude
Copy link
Author

@LeonAlvarez How are you using zx? Can you create a package.json + code sample?

@idmyn
Copy link

idmyn commented May 8, 2022

I've run into this issue trying to use zx with gatsby (which appears to have some trouble with ESM)

Are newer versions of zx incompatible with CommonJS format? If so, I wonder if that's worth flagging in the readme?

code sample: https://stackblitz.com/edit/node-rzx94p?file=index.js

@vinayakkulkarni
Copy link

node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/zx/index.mjs not supported.
Instead change the require of /app/node_modules/zx/index.mjs to a dynamic import() which is available in all CommonJS modules.
    at file:///app/build/scripts/start.mjs:3:23
    at async importPath (file:///app/node_modules/zx/zx.mjs:128:3)
    at async main (file:///app/node_modules/zx/zx.mjs:50:7)
    at async file:///app/node_modules/zx/zx.mjs:26:1 {
  code: 'ERR_REQUIRE_ESM'
}
npm notice
npm notice New major version of npm available! 7.21.0 -> 8.9.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.9.0
npm notice Run npm install -g npm@8.9.0 to update!
npm notice
node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/zx/index.mjs not supported.
Instead change the require of /app/node_modules/zx/index.mjs to a dynamic import() which is available in all CommonJS modules.
    at file:///app/build/scripts/start.mjs:3:23
    at async importPath (file:///app/node_modules/zx/zx.mjs:128:3)
    at async main (file:///app/node_modules/zx/zx.mjs:50:7)
    at async file:///app/node_modules/zx/zx.mjs:26:1 {
  code: 'ERR_REQUIRE_ESM'
}
node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/zx/index.mjs not supported.
Instead change the require of /app/node_modules/zx/index.mjs to a dynamic import() which is available in all CommonJS modules.
    at file:///app/build/scripts/start.mjs:3:23
    at async importPath (file:///app/node_modules/zx/zx.mjs:128:3)
    at async main (file:///app/node_modules/zx/zx.mjs:50:7)
    at async file:///app/node_modules/zx/zx.mjs:26:1 {
  code: 'ERR_REQUIRE_ESM'
}

Still facing this issue :(

@antonmedv
Copy link
Collaborator

require() of ES Module /app/node_modules/zx/index.mjs not supported.

@cefn
Copy link

cefn commented May 20, 2022

In a reference repro this commit was enough to get a vanilla npm+typescript project to execute zx scripts with ts-node. More in the PR

@akefirad
Copy link

akefirad commented Jul 7, 2022

Hi team, is there any update regarding this issue? I appreciate that people might be busy with other stuff, but we were asked to provide sample code to reproduce it, but didn't get any reply. Thanks. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants