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

[BUG]: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in #667

Closed
1 task done
Labels
released Status: Needs Info Full requirements are not yet known, so implementation should not be started Type: Bug Something isn't working as documented

Comments

@IchanZX1
Copy link

What happened?

How To fix?

Versions

Octokit.js v6.0.1, Node v18.19.1

Relevant log output

node:internal/errors:496
    ErrorCaptureStackTrace(err);
    ^
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /home/container/node_modules/@octokit/core/package.json
    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:584:36)
    at Module._findPath (node:internal/modules/cjs/loader:658:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1120:27)
    at Module._load (node:internal/modules/cjs/loader:975:27)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at Object.<anonymous> (/home/container/index.js:31:21) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
Node.js v18.19.1

Code of Conduct

  • I agree to follow this project's Code of Conduct
@IchanZX1 IchanZX1 added Status: Triage This is being looked at and prioritized Type: Bug Something isn't working as documented labels Mar 13, 2024
Copy link
Contributor

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

@wolfy1339
Copy link
Member

Please share reproducible code example

@wolfy1339
Copy link
Member

That specific error message suggest you are not using ESM.

Please see the usage section in the README:
https://github.com/octokit/core.js?tab=readme-ov-file#usage

@wolfy1339 wolfy1339 moved this from 🆕 Triage to 🛑 Blocked/Awaiting Response in 🧰 Octokit Active Mar 13, 2024
@wolfy1339 wolfy1339 added Status: Needs Info Full requirements are not yet known, so implementation should not be started and removed Status: Triage This is being looked at and prioritized labels Mar 13, 2024
@clementhemidy
Copy link

clementhemidy commented Mar 14, 2024

Hi,

I have the same issue.

Error: No "exports" main defined in ***/node_modules/@octokit/core/package.json
    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:584:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:658:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1120:27)
    at Function.Module._load (node:internal/modules/cjs/loader:975:27)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at Object.<anonymous> (***/src/github/github.repository.ts:1:1)

Octokit.js: v6.0.1
Node version: v18.19.1
Npm version: 10.2.4

I already use ESM to import the Octokit class:
import {Octokit} from "@octokit/core";

@wolfy1339
Copy link
Member

Are you using typescript at all? In typescript just because you are importing using ESM imports doesn't mean you are using ESM

@wolfy1339
Copy link
Member

If anyone is experiencing this issue, please give a small reproducible example in a separate repo.

Include the package.json and the tsconfig.json as well as a file that has the least code in order to reproduce the problem.

Also don't forget to specify the environment (Browser/Node/Deno as well as the version)

It would be very helpful in diagnosing this issue and helping out users.

You may also want to read up on ESM packages, https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

@iamnotstatic
Copy link

Anyone found a workaround for this, i'm also experiencing same issue

@wolfy1339
Copy link
Member

Anyone found a workaround for this, i'm also experiencing same issue

@iamnotstatic Please read my comment above.
Please provide all relevant information

@Darker935
Copy link

Darker935 commented Mar 20, 2024

@wolfy1339 https://github.com/Darker935/MdgWa-TEST

Run with npm run dev

@wolfy1339
Copy link
Member

wolfy1339 commented Mar 20, 2024

@Darker935 @octokit/rest is not an ESM module yet, you will have to downgrade @octokit/auth-token

Also, you are still using CJS in your example which explains some of the errors

@iamnotstatic
Copy link

Anyone found a workaround for this, i'm also experiencing same issue

@iamnotstatic Please read my comment above. Please provide all relevant information

Downgrading to 5.1.0 worked fine for me i will just stick to that for now

@wolfy1339
Copy link
Member

I understand that downgrading has solved the issue, I really wish to diagnose and fix this issue for users

@iamnotstatic
Copy link

I understand that downgrading has solved the issue, I really wish to diagnose and fix this issue for users

Okay, this was my initial config

"@octokit/core": "6.0.1"

tsconfig

{
  "compilerOptions": {
     "lib": [
        "es5",
        "es6"
     ],
     "target": "es5",
     "module": "commonjs",
     "moduleResolution": "node",
     "outDir": "./dist",
     "emitDecoratorMetadata": true,
     "experimentalDecorators": true,
     "sourceMap": true,
     "esModuleInterop": true,
  }
}```

@wolfy1339
Copy link
Member

wolfy1339 commented Mar 21, 2024

You are using CJS and not ESM. You will need to use dynamic imports or upgrade to ESM

    "target": "es2022",
     "module": "node16",
     "moduleResolution": "node16",

@iamnotstatic
Copy link

You are using CJS and not ESM. In typescript you can use ESM with CJS (though you can't mix ESM and CJS Octokit modules together due to upgraded dependencies introducing backwards incompatible changes) with the following options you should be fine

    "target": "es2022",
     "module": "node16",
     "moduleResolution": "node16",

Alright thanks @wolfy1339

@JoaoScheleder
Copy link

@wolfy1339 I'm getting the same error and others, setting to ESM or "node16" in tsconfig
With:

"target": "es2016"
"module": "commonjs"

Results in:

[ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined

With:

"target": "es2022"
"module": "es2022"

Case package.json type = "module":

ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts".

Without package.json type = "module":

SyntaxError: Cannot use import statement outside a module and Cannot find module '@octokit/core'.

With:

"target": "es2022"
"module": "Node16"

Results in:

The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@octokit/core")' call instead. To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field "type": "module" to 'C:/Users/{user}/project/package.json'.

My default tsconfig.json file:

{
  "compilerOptions": {
    "target": "ES2016",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "module": "CommonJS",                                /* Specify what module code is generated. */
    "rootDir": "./",                                  /* Specify the root folder within your source files. */
    "outDir": "./dist",                                   /* Specify an output folder for all emitted files. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */    
    "strict": true,                                      /* Enable all strict type-checking options. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  }
}

I'm using "@octokit/core": "^6.0.1" and Node v18.17.0
my import look like this:

import { Octokit } from "@octokit/core"
const octokit = new Octokit({ auth: env.GITHUB_TOKEN });

@wolfy1339
Copy link
Member

You can use dynamic imports, upgrade your code to ESM, or stick with v5

Option 1:

async function main() {
  const { Octokit } = await import('@octokit/core');

  const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
}
main();

Option 2: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
tsconfig.json:

"moduleResolution": "node16",
"module": "node16",
"target": "es2022"

package.json:

"type": "module"

Option 3:
package.json:

"@octokit/core": "^5"

The error in question in this issue is people that their code is CJS and are importing an ESM module.

There is also bugs with ts-node where it can't handle ESM: TypeStrong/ts-node#2094 TypeStrong/ts-node#2100

At this point, there is nothing actionable on our part. This is user error

@github-project-automation github-project-automation bot moved this from 🛑 Blocked/Awaiting Response to ✅ Done in 🧰 Octokit Active Mar 23, 2024
@jeremy-daley-kr
Copy link

jeremy-daley-kr commented Apr 4, 2024

@wolfy1339 I don't think this should've been closed out. The resolution would be to add to all octokit package.json files:

".": {
  "default": "path/to/entry.js"
}

So many packages I've seen in the wild omit default for some reason.

@wolfy1339
Copy link
Member

From how I interpret the documentation, default doesn't seem needed. It should match import
Even with the default, it would still error as it's ESM and people are trying to import it in a CJS environment.

Do you have an explanation as to why thus isn't the case?

@jeremy-daley-kr
Copy link

jeremy-daley-kr commented Apr 4, 2024

@wolfy1339 I've seen this happen in ts-node, tsx, and jest when packages don't include default. The technical reason of what those are all doing under the hood, I don't know precisely, but I also don't think it's always a trivial matter for someone to change a potentially very large project configuration. Is there a specific reason not to include a fallback? What led me here to begin with is seeing this issue with tsx after upgrading @octokit/core, and adding default allowed the resolution.

@JoaoScheleder @iamnotstatic @IchanZX1 @clementhemidy
This isn't a sustainable solution obviously, and once you fix one package.json, it will lead to the next @octokit package until they're all changed... but could you humor me, and add to ./node_modules/@octokit/core/package.json:

"exports": {
    ".": {
      "types": "./dist-types/index.d.ts",
-     "import": "./dist-src/index.js"
+     "import": "./dist-src/index.js",
+     "default": "./dist-src/index.js"
    }
  },

@JoaoScheleder
Copy link

JoaoScheleder commented Apr 4, 2024

@jeremy-daley-kr
I changed my package.json to type "module" and in my tsconfig i'm using:

"moduleResolution": "Node",
"module": "ES2022",
"target": "es2022", 

And it did help, but i was still getting an error.

Important

what helped me was switching from nodemon and ts-node to tsx.
my start script look like this:

"start": "npx tsx watch index.ts"

like @wolfy1339 said, ts-node has some bugs 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment