0

I have included the following js library in my project: Particles.js. Now in my component I import it and load it like this:

import { Component, h } from '@stencil/core';
import * as particlesJS from 'particles.js';

@Component({
    tag: 'app-home',
    styleUrl: 'app-home.css'
})
export class AppHome {

    componentDidLoad() {
        particlesJS.load('particles-js', 'assets/particlesjs-config.json', function() {
            console.log('callback - particles.js config loaded');
        });
    }

    render() {
        return (
            <div id="particles" class='app-home w-full h-full fixed bg-blue-200'>

            </div>
        );
    }
}

Now the problem is when I build and run my code I get the following error:

TypeError: "undefined is not a function"

particlesJS always seems to be undefined for some reason even though I have imported it at the top.

2 Answers 2

1

particles.js does not export any variables but instead sets a variable on the global window object, so you can't import it like this. Instead try

import 'particles.js';

Then you can access it directly from the window object:

window.particlesJS.load(...)

Related issue: https://github.com/VincentGarreau/particles.js/issues/265

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you soo much! This really helped. Had to use window["particlesJS"].load(...) coz typescript kept underlining .particleJS. :)
0

You can importing js functions to the ts file with require helping

const particlesJS = require('./particles');

But all functions should be exported like module.exports in the particles.js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.