Search code examples
vitesveltesveltekit

Running standalone scripts and long-running applications with SvelteKit codebase


I have a SvelteKit project and'd like to run some maintenance scripts from the command line. I know how to run scripts with node. The maintenance scripts would refer modules in SvelteKit's $lib folder which then import other modules and $env.

How can I run a script in a way that the SvelteKit framework specific functionality imports like $lib and $env are available inside the script code?

E.g.

node src/scripts/myscript.js   # How can import $lib here
  • How SvelteKit sets up its framework specific modules and imports?
  • What of these can be used in command line applications? Naturally some like navigator cannot be made available.

Solution

  • As kind people in SvelteKit discord pointed out there is vite-node package that does exactly this:

    npx vite-node src/scripts/my-sveltekit-script.js
    
    • $lib works
    • Public $env works
    • For private environment variables you still need to access process.env directly