Giter Site home page Giter Site logo

Comments (24)

wolfy1339 avatar wolfy1339 commented on August 16, 2024 3

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

from core.js.

wolfy1339 avatar wolfy1339 commented on August 16, 2024 1

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

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

from core.js.

github-actions avatar github-actions commented on August 16, 2024

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

from core.js.

wolfy1339 avatar wolfy1339 commented on August 16, 2024

Please share reproducible code example

from core.js.

wolfy1339 avatar wolfy1339 commented on August 16, 2024

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

from core.js.

clementhemidy avatar clementhemidy commented on August 16, 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";

from core.js.

wolfy1339 avatar wolfy1339 commented on August 16, 2024

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

from core.js.

wolfy1339 avatar wolfy1339 commented on August 16, 2024

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

from core.js.

iamnotstatic avatar iamnotstatic commented on August 16, 2024

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

from core.js.

wolfy1339 avatar wolfy1339 commented on August 16, 2024

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

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

from core.js.

Darker935 avatar Darker935 commented on August 16, 2024

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

Run with npm run dev

from core.js.

wolfy1339 avatar wolfy1339 commented on August 16, 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

from core.js.

iamnotstatic avatar iamnotstatic commented on August 16, 2024

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

from core.js.

wolfy1339 avatar wolfy1339 commented on August 16, 2024

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

from core.js.

iamnotstatic avatar iamnotstatic commented on August 16, 2024

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

from core.js.

iamnotstatic avatar iamnotstatic commented on August 16, 2024

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

from core.js.

JoaoScheleder avatar JoaoScheleder commented on August 16, 2024

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

from core.js.

jeremy-daley-kr avatar jeremy-daley-kr commented on August 16, 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.

from core.js.

wolfy1339 avatar wolfy1339 commented on August 16, 2024

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?

from core.js.

jeremy-daley-kr avatar jeremy-daley-kr commented on August 16, 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"
    }
  },

from core.js.

JoaoScheleder avatar JoaoScheleder commented on August 16, 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 👍

from core.js.

wolfy1339 avatar wolfy1339 commented on August 16, 2024

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

from core.js.

github-actions avatar github-actions commented on August 16, 2024

🎉 This issue has been resolved in version 6.1.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

from core.js.

wolfy1339 avatar wolfy1339 commented on August 16, 2024

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.

from core.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.