Reputation: 5289
I'm trying to use Puppeteer with TypeScript, @types/puppeteer
, and Jest.
Puppeteer uses a default export, and this works as pptr.test.js
:
import pptr from 'puppeteer'
pptr.launch(
// ... config here
)
However, when I install @types/puppeteer
and rename to pptr.test.ts
, I get this TS error:
Module '"/home/jack/Documents/Extensions/messages-example/node_modules/@types/puppeteer/index"' has no default export.
The code does transpile and run in Jest using Babel.
Neither of these work, but do pass TS checks:
import { launch } from 'puppeteer'
import * as pptr from 'puppeteer'
Both fail with a TypeError like "... is not a function".
My tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017",
}
}
Is @types/puppeteer
wrong? What is the best way to handle a TypeScript situation like this?
Upvotes: 2
Views: 1055
Reputation: 3654
You need to set esModuleInterop
and allowSyntheticDefaultImports
under the compilerOptions
prop in your tsconfig.json
You should be able to import it as a default then.
Upvotes: 5