sindresorhus/execa

How do I fix ERR_PACKAGE_PATH_NOT_EXPORTED ?

Closed this issue · 7 comments

Steps to reproduce

  1. Write code in TypeScript+ESM
import { execa } from 'execa';
async function main() {
    const { stdout } = await execa`echo hello!`;
    console.log(stdout);
}
main()
  1. Run with tsx (tsx index.ts)
  2. Terminal will output error (see below)

Log:

$ tsx src/index.ts
node:internal/modules/esm/resolve:304
  return new ERR_PACKAGE_PATH_NOT_EXPORTED(
         ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in C:\Users\ringoXD\Documents\GitHub\exec-pj\node_modules\unicorn-magic\package.json
    at exportsNotFound (node:internal/modules/esm/resolve:304:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:594:13)
    at resolveExports (node:internal/modules/cjs/loader:634:36)
    at Module._findPath (node:internal/modules/cjs/loader:724:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1211:27)
    at a._resolveFilename (C:\Users\ringoXD\Documents\GitHub\exec-trigger\node_modules\tsx\dist\cjs\index.cjs:1:1729)
    at Module._load (node:internal/modules/cjs/loader:1051:27)
    at Module.require (node:internal/modules/cjs/loader:1311:19)
    at require (node:internal/modules/helpers:179:18)
    at C:\Users\ringoXD\Documents\GitHub\exec-trigger\node_modules\npm-run-path\index.js:2:1361 {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node.js v20.17.0
error Command failed with exit code 1.

Hi @ringo360,

I cannot reproduce. I get the expected output hello!.

I am using Node 20.17.0 (like you are) and latest Execa 9.4.0, latest tsx 4.19.1.

Could you please create a GitHub repository that reproduces this?

Here is my repo. I am using

  • Node v20.17.0
  • tsx 4.7.1
  • execa 9.4.0

I can reproduce:

tsx: v4.19.1
node: v18.20.4
execa: 9.4.0
module: commonjs

reproduce repo: https://github.com/tjx666/reproduce-execa-issue

image
Error Output

❯ pnpm test
Found existing alias for "pnpm". You should use: "p"

@ test /Users/yutengjing/code/reproduce-execa-issue
tsx index.ts

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

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/yutengjing/code/reproduce-execa-issue/node_modules/.pnpm/npm-run-path@6.0.0/node_modules/unicorn-magic/package.json
at __node_internal_captureLargerStackTrace (node:internal/errors:496:5)
at new NodeError (node:internal/errors:405:5)
at exportsNotFound (node:internal/modules/esm/resolve:366:10)
at packageExportsResolve (node:internal/modules/esm/resolve:656:13)
at resolveExports (node:internal/modules/cjs/loader:590:36)
at Module._findPath (node:internal/modules/cjs/loader:664:31)
at Module._resolveFilename (node:internal/modules/cjs/loader:1126:27)
at nextResolveSimple (/Users/yutengjing/code/OpenArt/web/node_modules/.pnpm/tsx@4.19.1/node_modules/tsx/dist/register-DpmFHar1.cjs:3:942)
at /Users/yutengjing/code/OpenArt/web/node_modules/.pnpm/tsx@4.19.1/node_modules/tsx/dist/register-DpmFHar1.cjs:2:2550
at /Users/yutengjing/code/OpenArt/web/node_modules/.pnpm/tsx@4.19.1/node_modules/tsx/dist/register-DpmFHar1.cjs:2:1624
at resolveTsPaths (/Users/yutengjing/code/OpenArt/web/node_modules/.pnpm/tsx@4.19.1/node_modules/tsx/dist/register-DpmFHar1.cjs:3:760)
at /Users/yutengjing/code/OpenArt/web/node_modules/.pnpm/tsx@4.19.1/node_modules/tsx/dist/register-DpmFHar1.cjs:3:1038
at m._resolveFilename (file:///Users/yutengjing/code/OpenArt/web/node_modules/.pnpm/tsx@4.19.1/node_modules/tsx/dist/register-Swi4xIvk.mjs:1:789)
at Module._load (node:internal/modules/cjs/loader:981:27)
at Module.require (node:internal/modules/cjs/loader:1231:19)
at require (node:internal/modules/helpers:177:18)
at pathKey (/Users/yutengjing/code/reproduce-execa-issue/node_modules/.pnpm/npm-run-path@6.0.0/node_modules/npm-run-path/index.js:4:38)
at Object. (/Users/yutengjing/code/reproduce-execa-issue/node_modules/.pnpm/npm-run-path@6.0.0/node_modules/npm-run-path/index.js:47:30)
at Module._compile (node:internal/modules/cjs/loader:1364:14)
at Object.transformer (/Users/yutengjing/code/OpenArt/web/node_modules/.pnpm/tsx@4.19.1/node_modules/tsx/dist/register-DpmFHar1.cjs:2:1186)
at Module.load (node:internal/modules/cjs/loader:1203:32)
at Module._load (node:internal/modules/cjs/loader:1019:12)
at Module.require (node:internal/modules/cjs/loader:1231:19)
at require (node:internal/modules/helpers:177:18)
at process (/Users/yutengjing/code/reproduce-execa-issue/node_modules/.pnpm/execa@9.4.0/node_modules/execa/lib/arguments/options.js:4:29)
at Object. (/Users/yutengjing/code/reproduce-execa-issue/node_modules/.pnpm/execa@9.4.0/node_modules/execa/lib/arguments/options.js:82:16)
at Module._compile (node:internal/modules/cjs/loader:1364:14)
at Object.transformer (/Users/yutengjing/code/OpenArt/web/node_modules/.pnpm/tsx@4.19.1/node_modules/tsx/dist/register-DpmFHar1.cjs:2:1186)
at Module.load (node:internal/modules/cjs/loader:1203:32)
at Module._load (node:internal/modules/cjs/loader:1019:12)
at Module.require (node:internal/modules/cjs/loader:1231:19)
at require (node:internal/modules/helpers:177:18)
at (/Users/yutengjing/code/reproduce-execa-issue/node_modules/.pnpm/execa@9.4.0/node_modules/execa/lib/methods/main-sync.js:3:32)
at Object. (/Users/yutengjing/code/reproduce-execa-issue/node_modules/.pnpm/execa@9.4.0/node_modules/execa/lib/methods/main-sync.js:135:23)
at Module._compile (node:internal/modules/cjs/loader:1364:14)
at Object.transformer (/Users/yutengjing/code/OpenArt/web/node_modules/.pnpm/tsx@4.19.1/node_modules/tsx/dist/register-DpmFHar1.cjs:2:1186)
at Module.load (node:internal/modules/cjs/loader:1203:32)
at Module._load (node:internal/modules/cjs/loader:1019:12)
at Module.require (node:internal/modules/cjs/loader:1231:19)
at require (node:internal/modules/helpers:177:18)
at isPlainObject (/Users/yutengjing/code/reproduce-execa-issue/node_modules/.pnpm/execa@9.4.0/node_modules/execa/lib/methods/create.js:4:29)
at Object. (/Users/yutengjing/code/reproduce-execa-issue/node_modules/.pnpm/execa@9.4.0/node_modules/execa/lib/methods/create.js:47:24)
at Module._compile (node:internal/modules/cjs/loader:1364:14)
at Object.transformer (/Users/yutengjing/code/OpenArt/web/node_modules/.pnpm/tsx@4.19.1/node_modules/tsx/dist/register-DpmFHar1.cjs:2:1186)
at Module.load (node:internal/modules/cjs/loader:1203:32)
at Module._load (node:internal/modules/cjs/loader:1019:12)
at Module.require (node:internal/modules/cjs/loader:1231:19)
at require (node:internal/modules/helpers:177:18)
at import_command (/Users/yutengjing/code/reproduce-execa-issue/node_modules/.pnpm/execa@9.4.0/node_modules/execa/index.js:1:27)
at Object. (/Users/yutengjing/code/reproduce-execa-issue/node_modules/.pnpm/execa@9.4.0/node_modules/execa/index.js:22:18)
at Module._compile (node:internal/modules/cjs/loader:1364:14)
at Object.transformer (/Users/yutengjing/code/OpenArt/web/node_modules/.pnpm/tsx@4.19.1/node_modules/tsx/dist/register-DpmFHar1.cjs:2:1186)
at Module.load (node:internal/modules/cjs/loader:1203:32)
at Module._load (node:internal/modules/cjs/loader:1019:12)
at Module.require (node:internal/modules/cjs/loader:1231:19)
at require (node:internal/modules/helpers:177:18)
at (/Users/yutengjing/code/reproduce-execa-issue/index.ts:1:19)
at Object. (/Users/yutengjing/code/reproduce-execa-issue/index.ts:3:1)
at Module._compile (node:internal/modules/cjs/loader:1364:14)
at Object.transformer (/Users/yutengjing/code/OpenArt/web/node_modules/.pnpm/tsx@4.19.1/node_modules/tsx/dist/register-DpmFHar1.cjs:2:1186)
at Module.load (node:internal/modules/cjs/loader:1203:32)
at Module._load (node:internal/modules/cjs/loader:1019:12)
at ModuleWrap. (node:internal/modules/esm/translators:203:29)
at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:337:24)
at async loadESM (node:internal/process/esm_loader:34:7)
at async handleMainPromise (node:internal/modules/run_main:106:12) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node.js v18.20.4
 ELIFECYCLE  Test failed. See above for more details.

  ~/code/reproduce-execa-issue   main ✘ 1  18.20.4

execa is a pure esm package.

Workarounds:

  • Set "type": "module" in your package.json
  • Change index.ts to index.mts
  • use const { execa } = await import('execa')

I am using ESM and still have this problem...

@ringo360 I downloaded your repository and can reproduce your problem.

As mentioned by @ntnyq, you need to set "type": "module" in your package.json. When doing so, the ERR_PACKAGE_PATH_NOT_EXPORTED error does not appear anymore.

However, I do not think renaming index.ts to index.mts nor using a dynamic import() is needed. Using "type": "module" is enough.

@tjx666 Same thing with your reproduction repository. I can also reproduce your problem, and setting "type": "module" in package.json also fixes your problem.

For more information, please see the link posted by @ntnyq: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

With the modification of the package.json, the code worked fine!
Thanks for fast support and close this issue 🙏