vite-plugin-svelte
vite-plugin-svelte copied to clipboard
Allow specifying path of `package.json`
Describe the problem
In my project, the package.json
is located in the folder one level up from where the Svelte Kit project is located (and where vite build
command is run).
Because of that, I get this warning:
[vite-plugin-svelte] no package.json found, findRootSvelteDependencies failed
Describe the proposed solution
Can you please add a config option to manually specify the location of package.json
?
Alternatives considered
None
Importance
would make my life easier
vite-plugin-svelte checks vite root and cwd https://github.com/sveltejs/vite-plugin-svelte/blob/a1d141e890ac0d1572a46e2bec705aa090236560/packages/vite-plugin-svelte/src/utils/dependencies.ts#L6
Please add a link to a small repository that reproduces the directory layout you use to help understand the usecase. Sveltekit projects do come with a package.json in their directory by default.
I am not able to share a link to a repository, but at a high level the structure is:
project-root/
|-- package.json
|-- client/
|-- svelte-kit project
The cwd is project-root/client
.
Perhaps just changing findRootSvelteDependencies
to search in parent folders would work? Similarly to how Node.js module resolution behaves (if there's no package.json
in the current folder, it looks in the parent, and it continues until /
).
You don't have to provide the real project with all code. Just rebuild the structure with the sveltekit demo project.
We need to understand how exactly you are calling vite dev
and vite build
and how dependencies are being resolved.
Not having a package.json in the root of your sveltekit project is highly unusual and before we start adding new features we need to understand the usecase and see if it is something that vite-plugin-svelte should support or if there are other ways for you to solve it within the current feature-set.
In the project structure as above, the build command is cd client && vite build
.
It's a standard svelte-kit project except that the project.json
is located in a folder one level up. I understand this is unusual; the reason is that the code that svelte-kit outputs is included as part of a larger project.
The only other workaround I can think of is symliking project.json
into the client
folder. But I am concerned that may have some side effects I do not foresee at this time.
I'm more concerned that your svelte-kit project isn't working as intended. But without a project to look at it's hard to tell and i'm not doing guesswork. This is not moving forward until a repository has been provided
closing for now as it hasn't been clearly demonstrated how this feature would be beneficial or that alternatives have been considered and found not to be feasible.
This is not "we won't do it", but we need proper information and evaluation first.
Please give me some time to respond. You asked for some work and I haven’t had time to do it yet…
A use case for this would be if you have a mono-repo and you want to manage dependencies in a single package.json file in the root of the mono-repo. However if you also want to use vite dependency prebundling it cannot find the package.json.
The warning issues by vite-plugin-svelte no package.json found, findRootSvelteDependencies failed
goes away when you put a package.json without any dependencies in the root of you project. I would not expect this to include the deps in prebundling, but vite does generate a list of files in the node_modules/.vite/deps in the root of the project. Does this mean the optimization has run? If I understand correctly vite actually determines the deps from the imports in de the code https://vitejs.dev/guide/dep-pre-bundling.html#automatic-dependency-discovery.
please use vite or svelte discord for questions. And when you ask there, please include a link to a repository showing your setup to help people understand what you are trying to do.
Note:
both create-vite
and create-svelte
output a package.json at the root of your project and that is the standard way of managing dependencies.
There are working monorepo approaches by pnpm, yarn, npm and a few others that all also have a package.json at the root of every project contained in them.