Open rmunn opened 1 year ago
Since the reproduction involves creating a fresh skeleton project, running one pnpm
command, and then addding a single line of code, which takes less than a minute to do, it seems silly to create a repro repo that will be around forever with just a single line of code difference from the skeleton Svelte-Kit project. But if that's required, I'll create one.
Assuming this is the same issue, @gtm-nayan gave a workaround here: https://github.com/sveltejs/svelte/issues/9288#issuecomment-1748034687
Thanks, I'm not totally sure how his code was suppoed to work. But this worked for me:
{
resolveId(id: string): string | undefined {
if (id === 'css-tree') {
return './node_modules/css-tree/dist/csstree.esm.js';
}
}
},
I resolved this by installing:
npm install mdn-data
I'm using Rollup to bundle our code, which uses svelte/compiler
, importing it with:
import { compile } from 'svelte/compiler';
Before bundling, everything works fine. But after it, we get the same error informed by OP:
Error: Cannot find module '../data/patch.json'
Update:
The error happens both when using npm
or pnpm
. I even reinstalled everything from scratch with npm
only, but the error persists.
Rollup fix Do someone knows how to apply the suggested fix on Rollup config?
I found a solution for anyone using Rollup.
1. Install css-tree
and @rollup/plugin-virtual
on your dependencies (or devDependencies, if you will)
2. On your configuration file for Rollup, add css-tree
as a virtual package before every other plugin:
import virtual from '@rollup/plugin-virtual';
// ...
import { readFileSync } from 'fs';
export default {
input: 'your_input_here.js',
output: {
file: 'your_output_here.js'
// ...
// ...
},
plugins: [
virtual({ 'css-tree': readFileSync('./node_modules/css-tree/dist/csstree.esm.js', 'utf8') }),
// ...
]
};
FWIW the mdsvex plugin also has a compile export that seems to also trigger this issue as well and doing the workarounds for css-tree do not work even though css-tree is in the tree of dependencies and the same code is output in the built app.
I ran into this same error message while using a package that relies on svelte/compiler in server-side code under adapter-node, and was able to solve it by having rollup treat css-tree as an external dependency. Details/snippets in this issue comment. @stearnsbq-aem I'm curious to know whether this fix addresses your issue with mdsvex since I'm planning to use that soon...
FWIW the mdsvex plugin also has a compile export that seems to also trigger this issue as well and doing the workarounds for css-tree do not work even though css-tree is in the tree of dependencies and the same code is output in the built app.
Yup. Ran into this as well.
In case someone is running into the same issue with mdsvex, I solved the problem by simply setting mdsvex as a non-dev dependency.
In case someone is running into the same issue with mdsvex, I solved the problem by simply setting mdsvex as a non-dev dependency.
Thanks. This was my problem as well.
However, in my case, setting mdsvex as a non-dev dependency was incorrect, though it got rid of the error. My site uses compile
function when it builds a stats of all .svx pages. The intention was to use the compile
function only during build. Therefore it should be a dev dependency and not a runtime dependency.
The error (reads "mistake") was because the module that used compile
got imported into a +page.server.js
and made it into the runtime. My fix was to put the function into a dedicated folder outside of src
for all build-time functions, and make sure they did not get imported anywhere inside src
.
Lesson learned: server is a server runtime, not a build environment.
If you're building an app that compiles svx based on user request, then @vlimki is exactly right. In this case, mdsvex
is a runtime dependency of the application, and therefore should be set as a non-dev dependency.
The original issue related to css-tree
is fixed in Svelte 5, since we're not reyling on that dependency anymore
Describe the bug
After upgrading a Svelte-Kit project to Svelte 4, it becomes impossible to build the project if you have imported anything from
svelte/compiler
. The result is an error about "Cannot find module '../data/patch.json'". See below for reproduction steps; to my surprise, the minimal repro involves adding just one line of code to a freshly-created skeleton Svelte-Kit project using Svelte 4.This is blocking us from upgrading our project to Svelte 4 as we use the
svelte/compiler
package in one part of our code. We could rewrite that part of the code at considerable effort, but I'd rather just get the build error resolved.Reproduction
pnpm create svelte@next
pnpm add svelte@4 @sveltejs/kit@1.25.1
to upgrade to Svelte 4pnpm i
(not strictly necessary aspnpm add
will take care of it)src/routes/+server.js
containing just one line,import { compile } from 'svelte/compiler';
pnpm run build
Error: Cannot find module '../data/patch.json'
Logs
System Info
Severity
blocking an upgrade
Additional Information
The
../data/patch.json
code in question comes from css-tree, in ./node_modules/.pnpm/css-tree@2.3.1/node_modules/css-tree/lib/data-patch.js. It contains:That code ends up bundled into the
_server.js
file looking something like this:The relative path in the
require
call is not being adjusted by the Vite bundling process, so it fails because the current working directory is no longer deep inside the css-tree source (where thedata/patch.json
file could be found) but rather at the root of the Svelte-Kit project's repo.