Struggling to integrate the Animejs library into my TypeScript and webpack project

Transitioning from ES5 to TypeScript and webpack, I decided to incorporate the Three.js library seamlessly. Alongside this, I wanted to utilize the Anime.js library for its impressive timeline animation features. However, my efforts yesterday were fraught with challenges as I attempted to integrate Anime.js into my project, resulting in a familiar scenario of multiple tabs open simultaneously. Following the instructions outlined in the Anime.js documentation,

npm install animejs --save

and then

import anime from 'animejs/lib/anime.es.js';

...I encountered an error:

Could not find a declaration file for module 'animejs/lib/anime.es.js'

I made sure to install them using the command:

npm i --save-dev @types/animejs

Upon verifying the presence of an Anime.js folder with an index.d.ts file within the @types directory, I also confirmed that my tsconfig was configured to inspect this folder for declaration files.

{
    "compilerOptions": {
        "moduleResolution": "node",
        "strict": true
    },
    "typeRoots": [
        "./node_modules/@types/"
    ],
    "noImplicitAny": false,
    "include": ["**/*.ts", "test"]
}

After conducting some research, I discovered that simply modifying the implementation method could resolve the error. While Anime.js' documentation suggested the following:

import anime from 'animejs/lib/anime.es.js';

I opted for this alternative approach:

import * as anime from 'animejs';

This adjustment successfully eliminated the previous error, although as a newcomer to this development pipeline, I struggled to grasp how it impacted the outcome. Nonetheless, a new hurdle emerged: upon defining a new anime animation (referencing sample code from Anime.js),

anime({
  targets: '.css-selector-demo .el',
  translateX: 250
});

The compilation process proceeded smoothly, yet an issue surfaced in the JavaScript console (specifically in Chrome):

Uncaught TypeError: anime is not a function

This predicament has left me feeling stuck, without any peers to seek guidance from. I'm in need of assistance, please!

Answer №1

Thank you for the assistance so far, it was greatly appreciated. After some trial and error, I eventually managed to resolve the issue. My solution involved importing the anime framework with this line of code:

import anime from 'animejs/lib/anime.es.js';

I then proceeded to uninstall and reinstall the animejs framework, along with its @types. Here is a snippet of my updated tsconfig.json file (with some modifications advised by a friend):

{
    "compilerOptions": {
        "moduleResolution": "node",
        "strict": true, 
        "allowJs": true,
    },
    "typeRoots": [
        "../node_modules/@types/"
    ],
    "noImplicitAny": false,
    "include": ["**/*.ts", "test"]
}

Although I'm not certain which specific change led to the resolution, it ultimately worked like a charm. Hopefully, those facing a similar challenge may find this information helpful!

Answer №2

Give this a shot

import anime from 'animejs';

This is my go-to solution that I use consistently. I find that there's no need for me to use @types/animejs, so you should be able to remove the package without any issues.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to utilize Angular's structural directives within the innerHtml?

Can I insert HTML code with *ngIf and *ngFor directives using the innerHtml property of a DOM element? I'm confused about how Angular handles rendering, so I'm not sure how to accomplish this. I attempted to use Renderer2, but it didn't wor ...

Error in Typescript persists even after short-circuit evaluation is used

Kindly review the provided code sample type type1 = string[] | undefined; let variable1 : type1 = undefined; console.log(variable1 && variable1.length); Upon attempting to run this code in Typescript Playground, an error is generated stating Pro ...

I am encountering unexpected behavior with NextJS's getInitialProps function, as it is giving me a compiler error stating "varName not found on type {}"

I seem to be stuck on a simple syntax issue while working with NextJs. I am attempting to perform dynamic server-side fetches using the getInitialProps pattern. However, the compiler is unable to recognize the return of getInitialProps in the regular func ...

Sorting an object array by date is causing a problem

UPDATE: Finally cracked the code on this issue. I initially assumed that Date was interpreting the date ("29-04-2020") as DD-MM-YYYY, when it should actually be MM-DD-YYYY. For instance, here's an object array I'm working with: let t ...

Filter an array of objects in Typescript by using another array of strings

I'm having trouble with my TypeScript filter function. Here is an array of objects: [ { "id": 12345, "title": "Some title", "complexity": [ { "slug": "1", // This is my search term "name": "easy" }, { ...

Challenges faced while deploying a TypeScript and React application using Express on Heroku

I encountered some errors when attempting to host my application: 2018-07-03T23:32:25.175363+00:00 heroku[web.1]: Starting process with command `npm start` 2018-07-03T23:32:28.093779+00:00 heroku[web.1]: State changed from starting to crashed 2018-07-03T2 ...

Optimizing your data layer in Angular2: A Guide to Best Practices

As a newcomer to Angular2, I am diving into hands-on learning. My current project involves building multiple views with parent components, child components, and database services. After successfully creating one view, I am now gearing up to implement other ...

Having trouble locating the name 'it' in Jest TypeScript

After setting up Jest in React + TypeScript, I encountered an error when trying to run a test using the command npm test. The error message displayed was: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try ` ...

Mastering Typescript lookup types - effectively limit the properties included in a merge operation with the Partial type

Exploring lookup types, I'm interested in creating a safe-merge utility function that can update an entity of type T with a subset of keys from another object. The objective is to leverage the TypeScript compiler to catch any misspelled properties or ...

Loading Angular page

I'm currently working on a personal project in Angular and I have a requirement to display a specific page for a brief period of time when the site is loaded, before redirecting to the home component. Is there a way to achieve this or create a loading ...

Specifying data type in the fetch method

Screenshot depicting fetch function I'm currently working on a project using Next.js with Typescript and trying to retrieve data from a Notion database. I've encountered an error related to setting up the type for the database_id value. Can anyon ...

Limiting the height of a grid item in MaterialUI to be no taller than another grid item

How can I create a grid with 4 items where the fourth item is taller than the others, determining the overall height of the grid? Is it possible to limit the height of the fourth item (h4) to match the height of the first item (h1) so that h4 = Grid height ...

Reorganize code in JavaScript and TypeScript files using VSCode

Is there a way to efficiently organize the code within a .js / .ts file using Vscode? Specifically, when working inside a Class, my goal is to have static variables at the top, followed by variables, then methods, and so on automatically. I did some resea ...

The function that was provided as a parameter is not functioning as expected

I have a WsConnector class responsible for connecting to my backend application and subscribing to a WebSocket topic. export class WsConnector { private stompClient: any; connect(onMessage: (msg) => void) { this.stompClient = Stomp.over ...

When trying to use preact as an alias for react, the error "Module not found: 'react/jsx-runtime'" is thrown

Avoid using the outdated guide I linked; follow the one provided in the answer instead I am trying to transition from react to preact by following their migration guide. I updated my webpack.config.js to include: alias: { "react": "pr ...

Is there a way for me to retrieve the header values of a table when I click on a cell?

I have a project where I am developing an application for booking rooms using Angular 2. One of the requirements is to be able to select a cell in a table and retrieve the values of the vertical and horizontal headers, such as "Room 1" and "9:00". The data ...

Type '{}' is lacking the subsequent attributes in type 'Record'

This is the code snippet I am working with: const data : Record<MediaType, Person[]> = {}; I encountered an error while initializing the 'data' object as shown above. The error message specifies that certain properties are missing from typ ...

The child object in Typescript is characterized by its strong typing system

Looking to convert plain AngularJS code to Typescript? Take a look at this example: app.someController = function ($scope) { // var $scope.Data = null; var $scope.Data: SomeCollection = null; I need to associate Data with scope and specify it as type ...

Component remains populated even after values have been reset

My child component is structured as shown below, ChildComponent.html <div> <button type="button" data-toggle="dropdown" aria-haspopup="true" (click)="toggleDropdown()"> {{ selectedItemName }} <span></span> </but ...

What is the correct way to route requests to /api/ressource in a production environment?

In my development environment, I have set up a webpack dev configuration where my front-end server runs on port 8080 and my backend server runs on port 3000. Here is how I configured my webpack dev server: proxy: { '/api': 'http://localh ...