Search code examples
sveltesvelte-3

Illegal reassignment to import from other .js file


I want to import score varible from another file called 'scores.js', but It keeps give me an error when running the Svelte apps.

Any Idea how to fix the error?

store.js

import { writable } from 'svelte/store'

export let score = writable(0)

Quiz.svelte

import { score } from './stores.js'

function getANewQuiz() {
    isModalOpen = false
    score = 0
    currentQuestion = 0
    quiz = getQuiz()
  }

The errors

[!] Error: Illegal reassignment to import 'score'
src\Quiz.svelte (22:4)
20:   function getANewQuiz() {
21:     isModalOpen = false
22:     score = 0
        ^
23:     currentQuestion = 0
24:     quiz = getQuiz()
Error: Illegal reassignment to import 'score'

Solution

  • score is a store, so you can use $score = 0 in your component (or store.set(0)).

    (Working example)