J
J
J. Snow2021-01-08 13:47:55
JavaScript
J. Snow, 2021-01-08 13:47:55

NextJS: How to import JS libraries?

I am writing a project in TypeScript using NextJs (under the hood it has WebPack).
You need to import a library that is not designed as a js module.
This library stupidly creates variables and functions in its scope and that's it:

// my-library.js
var var1 = ...
function fn1() { ... }

Task:
We need to import this library so that we can use var1 and fn1 .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2021-01-08
@Aetae

import './my-library.js'; // без from
If without allowJs, then add the my-library.d.ts file with declarations:

declare let var1: number;
declare function fn1(): void;
// ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question