2

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.
1
  • $lib is just an alias. Not sure how exactly $env is implemented.
    – brunnerh
    Apr 15 at 7:26

2 Answers 2

0

I would proceed like that:

1 Place your script in a directory like src/scripts. For example, src/scripts/myscript.js.

2

npm install --save-dev esbuild vite

3

import { defineConfig } from 'vite';
import { sveltekit } from '@sveltejs/kit/vite';

export default defineConfig({
  plugins: [sveltekit()],
  build: {
    outDir: './out',
    rollupOptions: {
      input: './src/scripts/myscript.js'
    }
  }
});

4 This configuration ensures that Vite uses the SvelteKit plugin to resolve module paths and other configurations as it would in your SvelteKit application.

c. Modify your script to use async imports if necessary: Depending on what you are importing from $lib or other SvelteKit managed directories, you might need to adjust how imports are handled:

5

// Example using dynamic import
(async () => {
  const { myFunction } = await import('$lib/myLibModule');
  myFunction();
})();

6 Add a script to package.json: Add a command in your package.json to run your script through Vite:

"scripts": {
  "run-script": "vite build --config vite.config.script.js && node ./out/myscript.js"
}

7

npm run run-script

This setup ensures that your Node.js script can use all SvelteKit-specific aliases and functionalities, except those that are strictly browser-specific (like navigator or DOM APIs).

Limitations and Considerations: Environment Variables: Ensure that environment variables used by $env are available in the Node.js runtime environment when the script is executed. Browser-Specific APIs: Clearly, APIs that depend on a browser context won't work in this setup and should either be mocked or avoided.

0

Here is a repo with a simple demo:

  • esrun to run scripts with imports.
  • dotenv for environment variables.

Note: using $env from inside a module that is imported by the CLI script will result in an error. Any files in the dependency graph of the CLI script must use dotenv to access the environment variables.


Using modules/env in component: https://kit-demos-bwml8l9ea-leftium.vercel.app/

Using modules/env from CLI:

enter image description here


update: An alternative solution is to create an API endpoint that exposes the SvelteKit-specific functionality like $env. The CLI script can then call this API.

  • This will give full access to anything a SvelteKit endpoint has access to (and is serializable)...
  • ...but will be slower due to the network request.

You can mix-and-match this method with the first method above as needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.