Dynamicnotion
Dynamicnotion

Reputation: 313

Import javascript file in svelte

So today I discovered Svelte and I absolutley love the concept. I only got one problem I wrote a small helper.js file and can't seem to import it. Every time I try to reference the class I get

ReferenceError: Helper is not defined


main.js file:

import App from './App.svelte';
import './helper.js';

var app = new App({
    target: document.body
});
export default app;


App.svelte file:

<script>
    let helper = new Helper();
</script>

<h1>Hello</h1>


helper.js file:

class Helper {
  constructor() {
    console.log("working");
  }
}

Upvotes: 20

Views: 32423

Answers (1)

Rich Harris
Rich Harris

Reputation: 29615

You need to import it into the file that uses it:

<script>
  import Helper from './helper.js';
  let helper = new Helper();
</script>

<h1>Hello</h1>

Upvotes: 29

Related Questions