Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I import node module into svelte component

I'm new to svelte and I am trying to use an installed node module in my dependancies called momentum-slider. In the script tags of my svelte component I have:

import MomentumSlider from "../../node_modules/momentum-slider";
let slider = new MomentumSlider({
   el: ".ms-container",
});

In my component's html markup I have the suggested markup as shown in the tutorial at https://scotch.io/tutorials/building-a-fancy-countdown-timer-with-momentumsliderjs

However, I am getting a typeError in the browser console:

enter image description here

I am new to development in general and I am not sure if this is a problem with momentum-slider or an error on my part. Any insights would be much appreciated.

like image 281
stickleBrick Avatar asked May 17 '26 15:05

stickleBrick


1 Answers

Not sure how to use this library but you should take care of 2 things. First import your package like the following:

import MomentumSlider from "momentum-slider";

Second you need to initialise the MomentumSlider class when the component is mounted using onMount:

import { onMount } from "svelte";
import MomentumSlider from "momentum-slider";

let slider;

onMount(() => {
  slider = new MomentumSlider({ 
    el: ".ms-container"
  });
});

Edit exciting-bouman-dyywc

like image 189
johannchopin Avatar answered May 20 '26 12:05

johannchopin