asunrey
asunrey

Reputation: 875

How to import js in npm package with typescript and javascript

I pulled down a library that contains Typescript and it's javascript version.

When declaring import Library from "@scope/library", my app can only access the typescript, even when I add the js extension.

How can I import the js file instead? I get errors on types.

Upvotes: 0

Views: 379

Answers (2)

Arash Motamedi
Arash Motamedi

Reputation: 10682

You can override the package's default entry point:

const myObj = require("../node_modules/library/path/to/dist/file.js");

like you would reference any other file in your project structure.

Another way to get Typescript to shut up about typings is to require instead of import:

const myObj = require("@scope/library");

This way you don't have to worry about resolving exactly the right js file, and can still benefit from the Node module resolution mechanism.

Upvotes: 1

JamesQuigley
JamesQuigley

Reputation: 16

If you're getting issues with the types, you either need to install the types for the package

npm install -D @types/<package> yarn add -D @types/<package>

Or you can ignore types by requiring instead of importing:

const package = require('package')

Upvotes: 0

Related Questions