Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Function called outside component initialization #592

Closed
btakita opened this issue Mar 4, 2019 · 11 comments
Closed

Function called outside component initialization #592

btakita opened this issue Mar 4, 2019 · 11 comments

Comments

@btakita
Copy link
Contributor

btakita commented Mar 4, 2019

I'm getting a Function called outside component initialization error from a sapper v0.26.0-alpha.10 project.

A component is being loaded from a index.html route component. This component is calling onMount, beforeUpdate, afterUpdate, or onDestroy.

I tried moving onMount into a setTimeout & the same issue occurs.

Unfortunately, I'm not able to reproduce it in the repl. Hopefully, this will be fixed or a reproduction for this issue will emerge.

@btakita
Copy link
Contributor Author

btakita commented Mar 4, 2019

The generated code calls .$$render instead of .render on the component.

@btakita
Copy link
Contributor Author

btakita commented Mar 4, 2019

This is a svelte issue. sveltejs/svelte#2156

@btakita
Copy link
Contributor Author

btakita commented Mar 6, 2019

It looks like the issue stems from having "svelte" as a dependency instead of a devDependencies in package.json within the sapper project.

This causes import 'svelte' to load the rollup excluded npm package's set_current_component instead of from within the sapper generated server.js.

See

Excluded packages in rollup.config.js:
https://github.com/sveltejs/sapper-template/blob/rollup-v3/rollup.config.js#L69

svelte as a devDependency so the npm package is not used by the server runtime: https://github.com/sveltejs/sapper-template/blob/rollup-v3/package.json#L22

@mdskinner
Copy link

mdskinner commented Jun 19, 2019

The above wasn't relevant for me - from the docs:
A <script> block contains JavaScript that runs when a component instance is created.

A <script> tag with a context="module" attribute runs once when the module first evaluates, rather than for each component instance.

You can't call a function outside of component initialization...
You can't call a function within <script context="module">

Moving it to the standard <script> tag works fine

@hugosenari
Copy link

I know that since we bundle it, makes no difference if svelte is dep or devDep, but makes sense to me set things that my code import as dep and even eslint complains about it.

DX:

  • start sapper project; 😄
  • configure eslint; 😃
  • eslint say that svelt should be dep; 🤔
  • update package.json; 🤷‍♂️
  • build fails with crypt error; 😢
  • try to figure what the hell; 😕
  • google it; 🤞
  • come here (if you have luck); 🍀
  • revert package.json; 😞
  • add ignore error to eslint; 😞

Maybe we should offer better solution for this.

@Conduitry
Copy link
Member

Conduitry commented Mar 20, 2020

What eslint rule or plugin says that svelte should be a prod dependency and not a dev dependency? That seems to be where the real problem is.

Having the bundler config bundle dev deps and leave prod deps as external seems to be the sanest thing it could default to, and I don't see us wanting to change that.

@hugosenari
Copy link

hugosenari commented Mar 20, 2020

Thank you for replying fast. ☮️

ESlint rule about dependencies.

We could at least try to offer better error message for this, before it becomes our next NullPointerException, Segmentation Fault or Kernel Panic (more memes).

When the message say function was called outside component initialization first will look at my code and last at my configuration.

In the last case we could suggest dependency-cruiser or similar in templates.

edit: I wasn't aware that yarn had dependency validation.

@Conduitry
Copy link
Member

According to the readme for that plugin, it should allow imports of devDependencies unless you specifically configure that to be disallowed, so I'm not sure what you're seeing there. And the Sapper template can't really guard against it if you decide to move something from devDependencies to dependencies. I don't think we need more tooling here.

@ANovokmet
Copy link

ANovokmet commented May 6, 2020

Got this issue migrating my project to Svelte 3.
The problem was with my rollup.config.js.
plugins: [svelte(), commonjs(), nodeResolve(), typescript()]
did not result in a working bundle. When fixed to
plugins: [svelte(), nodeResolve(), commonjs(), typescript()]
the error disappeared.

@jagrut-18
Copy link

This Function called outside component initialization error occurred in my case because I called createEventDispatcher() inside a function.
Error in this case:
image

Fix in my case:
image

@webdev23
Copy link

Instead of the common form

export async function myFunc(){ }

this works similarly like this:

export const myFunc = async () => { }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants