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

Error(sveltekit): Package subpath './vite' is not defined by "exports" #346

Closed
thenbe opened this issue Nov 11, 2023 · 5 comments
Closed
Labels
bug Something isn't working

Comments

@thenbe
Copy link
Contributor

thenbe commented Nov 11, 2023

Knip errors on fresh sveltekit project.

npm create svelte@latest knip-demo
# choice 1: SvelteKit demo app
# choice 2: Yes, using typescript syntax.
cd knip-demo
npm i
npx knip
$ npx knip
Analyzing workspace ....
Error loading /home/nbe/projects/playground/knip-demo/vite.config.ts
Reason: Package subpath './vite' is not defined by "exports" in /home/nbe/projects/playground/knip-demo/node_modules/@sveltejs/kit/package.json

Relevant #99

@thenbe thenbe added the bug Something isn't working label Nov 11, 2023
@webpro
Copy link
Collaborator

webpro commented Nov 13, 2023

Thanks for the report. The only solution I can offer at this point is to update Knip to v2.41.1 and rename vite.config.tsvite.config.js (or vite.config.mjs without updating).

The issue you link to is indeed relevant, but in the Vite plugin is now loading the Vite config file, since it might contain Vitest configuration that should be parsed for dependencies.

Not sure yet how to deal with the Jiti issue, though.

@webpro
Copy link
Collaborator

webpro commented Nov 13, 2023

The situation has further improved in v2.41.2, but it's not fixed yet.

The ESM issue and loading TS in general works fine, but calling sveltekit() results in:

[ '[...]/vite.config.ts' ]
[...]/knip/dist/util/loader.js:37
        throw new LoaderError(`Error loading ${filePath}`, { cause: error });
              ^

LoaderError: Error loading [...]/vite.config.ts
    at load ([...]/knip/dist/util/loader.js:37:15)
    at Module.findViteDependencies ([...]/knip/dist/plugins/vite/index.js:9:31)
    ... 5 lines matching cause stack trace ...
    at async [...]/knip/dist/cli.js:74:1 {
  [cause]: [...]/node_modules/@sveltejs/kit/src/core/sync/ts.js:5
    exports.ts = ts = (await Promise.resolve().then(() => require('typescript'))).default;
                             ^^^^^^^
  
  SyntaxError: Unexpected identifier 'Promise'
      at new Script (node:vm:93:7)
      at createScript (node:vm:248:10)
      at Object.runInThisContext (node:vm:296:10)
      at evalModule ([...]/knip/node_modules/jiti/dist/jiti.js:1:256254)
      at jiti ([...]/knip/node_modules/jiti/dist/jiti.js:1:254371)

@thenbe
Copy link
Contributor Author

thenbe commented Dec 16, 2023

jiti seems to choke when it hits this file in the vite sveltekit plugin:

/** @type {import('typescript')} */
// @ts-ignore
export let ts = undefined;
try {
	ts = (await import('typescript')).default;
} catch {}

It prints this error:

> exper-jiti-vite@1.0.0 start /home/nbe/projects/playground/exper-jiti-vite
> jiti src/jiti.ts

[jiti] [transpile] [esm] /home/nbe/projects/playground/exper-jiti-vite/ts.js (4.047ms)
/home/nbe/projects/playground/exper-jiti-vite/ts.js:5
  exports.ts = ts = (await Promise.resolve().then(() => require('typescript'))).default;
                           ^^^^^^^

SyntaxError: Unexpected identifier 'Promise'
    at new Script (node:vm:99:7)
    at createScript (node:vm:247:10)
    at Object.runInThisContext (node:vm:295:10)
    at evalModule (/home/nbe/projects/playground/exper-jiti-vite/node_modules/.pnpm/jiti@1.21.0/node_modules/jiti/dist/jiti.js:1:256254)
    at jiti (/home/nbe/projects/playground/exper-jiti-vite/node_modules/.pnpm/jiti@1.21.0/node_modules/jiti/dist/jiti.js:1:254371)
    at /home/nbe/projects/playground/exper-jiti-vite/src/jiti.ts:25:13
    at evalModule (/home/nbe/projects/playground/exper-jiti-vite/node_modules/.pnpm/jiti@1.21.0/node_modules/jiti/dist/jiti.js:1:256443)
    at jiti (/home/nbe/projects/playground/exper-jiti-vite/node_modules/.pnpm/jiti@1.21.0/node_modules/jiti/dist/jiti.js:1:254371)
    at Object.<anonymous> (/home/nbe/projects/playground/exper-jiti-vite/node_modules/.pnpm/jiti@1.21.0/node_modules/jiti/bin/jiti.js:16:1)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49

Node.js v20.10.0
 ELIFECYCLE  Command failed with exit code 1.

I'm not very confident I understand what's happening, but it looks like we want to instruct jiti to avoid transforming this file, as it is "already ESM"? I found this jiti thread as well where sveltekit is mentioned.

@webpro
Copy link
Collaborator

webpro commented Mar 14, 2024

Knip v5.1 introduce a new workaround: bunx --bun knip

@webpro
Copy link
Collaborator

webpro commented Mar 21, 2024

See #565

@webpro webpro closed this as completed Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants