question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Svelte gives "Error: Function called outside component initialization", only in Dev Build.

See original GitHub issue

Describe the bug This issue is not just with Svelte Kit, It happened with vite + routify combo as well, and only in dev build.

Basically, this issue happens upon doing a normal dev build. image

Logs

Logs Failed to init component <Root> Error: Function called outside component initialization at get_current_component (http://localhost:3000/node_modules/.pnpm/svelte@3.35.0/node_modules/svelte/internal/index.mjs?v=df472783:649:15) at setContext (http://localhost:3000/node_modules/.pnpm/svelte@3.35.0/node_modules/svelte/internal/index.mjs?v=df472783:679:5) at instance (http://localhost:3000/.svelte/dev/generated/root.svelte?import:480:2) at init (http://localhost:3000/home/abhijit/Documents/Experiments/QuillerBee/Sub%20Experiments/job-listing/node_modules/.pnpm/svelte@3.35.0/node_modules/svelte/internal/index.mjs?v=df472783:1474:11) at new Root (http://localhost:3000/.svelte/dev/generated/root.svelte?import:574:3) at createProxiedComponent (http://localhost:3000/node_modules/.pnpm/svelte-hmr@0.13.3_svelte@3.35.0/node_modules/svelte-hmr/runtime/svelte-hooks.js:245:9) at new ProxyComponent (http://localhost:3000/node_modules/.pnpm/svelte-hmr@0.13.3_svelte@3.35.0/node_modules/svelte-hmr/runtime/proxy.js:240:20) at new Proxy<Root> (http://localhost:3000/node_modules/.pnpm/svelte-hmr@0.13.3_svelte@3.35.0/node_modules/svelte-hmr/runtime/proxy.js:340:11) at Renderer.start (http://localhost:3000/.svelte/dev/runtime/internal/start.js:409:15) at async start (http://localhost:3000/.svelte/dev/runtime/internal/start.js:811:2)

To Reproduce

  1. Svelte Kit - Just following along the latest Svelte doc caused the issue.
  • pnpm init svelte@next
  • pnpm i
  • npm run dev
  1. Svelte + Vite + Routify - Also I created a repo, reproducing the same bug but with different setup. Vitte: vite-routify-issue branch
  • pnpm i
  • npm run dev:routify

In both the cases, the dev build fails while the production build succeeds.

Expected behavior I want to be able to run the dev build, the same way the production build works.

If not svelte-kti, maybe vite+routify+svelte combo can work!

Stacktraces

Stack Trace Failed to init component <Root> Error: Function called outside component initialization at get_current_component (http://localhost:3000/node_modules/.pnpm/svelte@3.35.0/node_modules/svelte/internal/index.mjs?v=df472783:649:15) at setContext (http://localhost:3000/node_modules/.pnpm/svelte@3.35.0/node_modules/svelte/internal/index.mjs?v=df472783:679:5) at instance (http://localhost:3000/.svelte/dev/generated/root.svelte?import:480:2) at init (http://localhost:3000/home/abhijit/Documents/Experiments/QuillerBee/Sub%20Experiments/job-listing/node_modules/.pnpm/svelte@3.35.0/node_modules/svelte/internal/index.mjs?v=df472783:1474:11) at new Root (http://localhost:3000/.svelte/dev/generated/root.svelte?import:574:3) at createProxiedComponent (http://localhost:3000/node_modules/.pnpm/svelte-hmr@0.13.3_svelte@3.35.0/node_modules/svelte-hmr/runtime/svelte-hooks.js:245:9) at new ProxyComponent (http://localhost:3000/node_modules/.pnpm/svelte-hmr@0.13.3_svelte@3.35.0/node_modules/svelte-hmr/runtime/proxy.js:240:20) at new Proxy<Root> (http://localhost:3000/node_modules/.pnpm/svelte-hmr@0.13.3_svelte@3.35.0/node_modules/svelte-hmr/runtime/proxy.js:340:11) at Renderer.start (http://localhost:3000/.svelte/dev/runtime/internal/start.js:409:15) at async start (http://localhost:3000/.svelte/dev/runtime/internal/start.js:811:2)

Information about your SvelteKit Installation:

  • The output of npx envinfo --system --npmPackages svelte,@sveltejs/kit --binaries --browsers System:
    OS: Linux 5.4 Zorin OS 15.3
    CPU: (4) x64 Intel® Core™ i5-7200U CPU @ 2.50GHz
    Memory: 9.60 GB / 15.52 GB
    Container: Yes
    Shell: 4.4.20 - /bin/bash
    Binaries:
    Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node
    npm: 7.6.3 - ~/.nvm/versions/node/v14.16.0/bin/npm
    Browsers:
    Chrome: 88.0.4324.182
    Firefox: 86.0.1
    npmPackages:
    @sveltejs/kit: next => 1.0.0-next.58
    svelte: ^3.29.0 => 3.35.0

  • Your browser - Chrome

  • Your adapter - I just tried to run locally, using Dev command.

Severity It’s not letting me play with Svelte Kit at all. Also same as not letting me run Vite + Svelte + Routify.

Additional context I have raised 2 issues in Vite & Routify repos respectively.

  1. Vite Issue
  2. Routify Issue

P.S I am trying to write a blog post about Vite + Svelte + Routify combo, till Svelte Kit becomes production ready. Also, I have a project in work, which uses Rollup and has a super slow development experience, which led me to explore other build tools such as Snowpack & Vite. See: Quiller Bee Repo

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
skrifblokkcommented, Mar 24, 2021

Workaround: try again in a path with no spaces in any folder names (“Sub Experiments”)

Probable duplicate of #634

0reactions
derpycodercommented, Mar 24, 2021

It works. Yay!

Thank you @skrifblokk.

Read more comments on GitHub >

github_iconTop Results From Across the Web

I get "Function called outside component initialization" when ...
The problem is that you can't do a setContext() outside of a Svelte component, you need to call it explicitly in the <script>...
Read more >
Svelte - Function called outside component initialization from ...
Hi there,. I have got a very weird behaviour when I'm creating new Sveltekit app.
Read more >
How to fix "Function called outside component initialization ...
But when I moved svelte to a dependency I got an error saying Function called outside component initialization . This is documented on...
Read more >
Svelte's lifecycle methods can be used anywhere - Geoff Rich
We can move this code to a separate module, as long as the function calling onMount is called when the component is initializing....
Read more >
Hypothesis
Should Svelte libraries include external: ['svelte'] in rollup.config.js? ("Function called outside component initialization" getContext in {#each}.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found