vercel / ncc

Compile a Node.js project into a single file. Supports TypeScript, binary addons, dynamic requires.
https://npmjs.com/@vercel/ncc
MIT License
8.99k stars 285 forks source link

Confusing error message "Package path . is not exported from package" #1167

Closed raxod502 closed 4 months ago

raxod502 commented 4 months ago

I received the following error on trying to build someone else's abandoned project that uses ncc, after updating ncc to the latest supported version:

> ncc build index.js --minify

ncc: Version 0.38.1
ncc: Compiling file index.js into CJS
Error: Module not found: Error: Package path . is not exported from package /home/raxod502/files/code/utils/fresh-bot/node_modules/@octokit/plugin-throttling (see exports field in /home/raxod502/files/code/utils/fresh-bot/node_modules/@octokit/plugin-throttling/package.json)
Did you mean './@octokit/plugin-throttling'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.
    at /home/raxod502/files/code/utils/fresh-bot/node_modules/@vercel/ncc/dist/ncc/index.js.cache.js:38:1896272
    at /home/raxod502/files/code/utils/fresh-bot/node_modules/@vercel/ncc/dist/ncc/index.js.cache.js:38:396262
    at _done (eval at create (/home/raxod502/files/code/utils/fresh-bot/node_modules/@vercel/ncc/dist/ncc/index.js.cache.js:21:75523), <anonymous>:9:1)
    at eval (eval at create (/home/raxod502/files/code/utils/fresh-bot/node_modules/@vercel/ncc/dist/ncc/index.js.cache.js:21:75523), <anonymous>:34:22)

Note the claim that Package path . is not exported in @octokit/plugin-throttling. Yet, when inspecting the package.json file in the error message, it shows:

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

This seemingly contradicts the error message.

styfle commented 4 months ago

Does the code work when you run node index.js without ncc?

It sounds like there might be a require('@octokit/plugin-throttling') since the default is CJS which is why you're seeing the log ncc: Compiling file index.js into CJS

Since @octokit/plugin-throttling is missing main, then it will fail with CJS.

raxod502 commented 4 months ago

Yeah, it has become clear that this is not an error in ncc, because you are correct that the application code was loading the package with require, which (despite their documentation explicitly stating the contrary) is wrong. That documentation issue is https://github.com/octokit/plugin-throttling.js/issues/677.

It would be helpful if the error message from ncc stated specifically what key it is looking at in the nested package.json because right now the error sounds like it's saying there is no exports entry for ., which isn't the case. But the fact that ncc reports an error is correct.