svelte: Using parse inside sveltekit's server endpoint fails when we make a build.
Describe the bug
When we use parse method on server from svelte/compiler it work perfectly in dev but when we make build using vite build in sveltekit project it fails.
Error: Cannot find module '../data/patch.json'
This ../data/patch.json file is part of css-tree.
In v3 it use to work perfectly on both server and client but not with v4.
Reproduction
Reproduction GitHub - Explanation
Logs
:internal/event_target:1006
process.nextTick(() => { throw err; });
^
Error: Cannot find module '../data/patch.json'
Require stack:
- /Users/shivammeena/WebstormProjects/svelte-h2j/.svelte-kit/output/server/chunks/svelteToJsx.js
at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
at Module._load (node:internal/modules/cjs/loader:841:27)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at file:///Users/shivammeena/WebstormProjects/svelte-h2j/.svelte-kit/output/server/chunks/svelteToJsx.js:12150:15
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
at async analyse (file:///Users/shivammeena/WebstormProjects/svelte-h2j/node_modules/.pnpm/@sveltejs+kit@1.24.0_svelte@4.2.1_vite@4.4.9/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:83:16)
at async MessagePort.<anonymous> (file:///Users/shivammeena/WebstormProjects/svelte-h2j/node_modules/.pnpm/@sveltejs+kit@1.24.0_svelte@4.2.1_vite@4.4.9/node_modules/@sveltejs/kit/src/utils/fork.js:22:16)
Emitted 'error' event on Worker instance at:
at [kOnErrorMessage] (node:internal/worker:290:10)
at [kOnMessage] (node:internal/worker:301:37)
at MessagePort.<anonymous> (node:internal/worker:202:57)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:731:20)
at exports.emitMessage (node:internal/per_context/messageport:23:28) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/shivammeena/WebstormProjects/svelte-h2j/.svelte-kit/output/server/chunks/svelteToJsx.js'
]
}
Node.js v18.12.1
System Info
System:
OS: macOS 13.5.2
CPU: (8) arm64 Apple M1
Memory: 55.41 MB / 8.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 8.19.2 - ~/.nvm/versions/node/v18.12.1/bin/npm
pnpm: 8.8.0 - ~/Library/pnpm/pnpm
Browsers:
Brave Browser: 117.1.58.127
Chrome: 108.0.5359.94
Safari: 16.6
npmPackages:
svelte: ^4.2.1 => 4.2.1
Severity
annoyance
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 15 (7 by maintainers)
Commits related to this issue
- workaround css-tree issue from https://github.com/sveltejs/svelte/issues/9288 — committed to sillsdev/languageforge-lexbox by hahn-kev 3 years ago
- workaround css-tree issue from https://github.com/sveltejs/svelte/issues/9288 — committed to sillsdev/languageforge-lexbox by hahn-kev 3 years ago
- workaround css-tree issue from https://github.com/sveltejs/svelte/issues/9288 — committed to sillsdev/languageforge-lexbox by hahn-kev 3 years ago
The original issue related to
css-treeis fixed in Svelte 5, since we’re not reyling on that dependency anymore@theetherGit I ran into this issue when using sveltekit-og and was able to work around it by adding css-tree as a (non-dev) dependency:
and then marking
css-treeas an external dependency in myvite.config.js:prisma is just part of my setup, not part of solution This setup works for me when deployed using adapter-node inside a Docker container. I tried the
css-tree-resolvercustom vite plugin that @theetherGit suggested, as well as the one packaged withsveltekit-og, but in both cases I still got the dreadedError: Cannot find module '../data/patch.json'.I am finding that the most reliable approach with external dependencies that include non-code artifacts, like JSON or
.nodebinaries, is to avoid bundling the dependency altogether by including it inbuild.rollupOptions.external.This only works if you only use these packages in
+(page.)server.(js/ts)files and/or modules under$lib/server. But node binaries don’t run in the browser anyway, and it practically speaking doesn’t matter how much code you stuff into a docker container, so the upside for optimizing these dependencies is slight. I have found that it’s less of a headache to just avoid bundling them altogether.update: forgot to mention that I took inspiration from this sveltekit & resvg-js solution by @bartosjiri
I found a solution for anyone using Rollup.
1. Install
css-treeand@rollup/plugin-virtualon your dependencies (or devDependencies, if you will)2. On your configuration file for Rollup, add
css-treeas a virtual package before every other plugin:@theetherGit thank you for the suggestion! I’m using Rollup instead of Vite, and I’m now trying to understand how I can correctly “translate” this fix in a function that would be valid inside Rollup’s plugin context (without success so far).