core.js icon indicating copy to clipboard operation
core.js copied to clipboard

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

Open IchanZX1 opened this issue 1 year ago • 12 comments

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

  • [X] I agree to follow this project's Code of Conduct

IchanZX1 avatar Mar 13 '24 01:03 IchanZX1

👋 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! 🚀

github-actions[bot] avatar Mar 13 '24 01:03 github-actions[bot]

Please share reproducible code example

wolfy1339 avatar Mar 13 '24 01:03 wolfy1339

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 avatar Mar 13 '24 02:03 wolfy1339

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";

clementhemidy avatar Mar 14 '24 09:03 clementhemidy

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

wolfy1339 avatar Mar 14 '24 16:03 wolfy1339

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

wolfy1339 avatar Mar 14 '24 23:03 wolfy1339

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

iamnotstatic avatar Mar 18 '24 22:03 iamnotstatic

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

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

wolfy1339 avatar Mar 18 '24 22:03 wolfy1339

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

Run with npm run dev

Darker935 avatar Mar 20 '24 14:03 Darker935

@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

wolfy1339 avatar Mar 20 '24 14:03 wolfy1339

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

iamnotstatic avatar Mar 20 '24 15:03 iamnotstatic

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

wolfy1339 avatar Mar 20 '24 16:03 wolfy1339

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,
  }
}```

iamnotstatic avatar Mar 21 '24 17:03 iamnotstatic

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

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

wolfy1339 avatar Mar 21 '24 18:03 wolfy1339

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

iamnotstatic avatar Mar 23 '24 09:03 iamnotstatic

@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 });

JoaoScheleder avatar Mar 23 '24 20:03 JoaoScheleder

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: https://github.com/TypeStrong/ts-node/issues/2094 https://github.com/TypeStrong/ts-node/issues/2100

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

wolfy1339 avatar Mar 23 '24 20:03 wolfy1339

@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.

jeremy-daley-kr avatar Apr 04 '24 15:04 jeremy-daley-kr

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?

wolfy1339 avatar Apr 04 '24 15:04 wolfy1339

@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"
    }
  },

jeremy-daley-kr avatar Apr 04 '24 15:04 jeremy-daley-kr

@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 :+1:

JoaoScheleder avatar Apr 04 '24 15:04 JoaoScheleder

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.

There isn't any specific reason. It's just how I have interpreted the specs. Feel free to send in PRs

wolfy1339 avatar Apr 08 '24 15:04 wolfy1339

:tada: This issue has been resolved in version 6.1.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Apr 09 '24 21:04 github-actions[bot]

I just released a new version with the fix as described by @jeremy-daley-kr

Please open up a new issue if you still have issues.

wolfy1339 avatar Apr 09 '24 21:04 wolfy1339

Is it CJS? I'm getting octokit The current file is a CommonJS module while trying to import it import { Octokit, App } from "octokit"; after installing via npm install octokit in my newly create VSCode Extension.

My tsconfig:

{
	"compilerOptions": {
		"module": "Node16",
		"target": "ES2022",
		"lib": [
			"ES2022"
		],
		"sourceMap": true,
		"rootDir": "src",
		"strict": true
	}
}

MihaelIsaev avatar Sep 09 '24 13:09 MihaelIsaev

Nope it is not CJS. You haven't defined your file as an ESM module.

Either use the .mjs extension or specify "type": "module" in your package.json

wolfy1339 avatar Sep 09 '24 13:09 wolfy1339