question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: SyntaxError: The requested module 'puppeteer' does not provide an export named 'ElementHandle'

See original GitHub issue

Bug description

Steps to reproduce the problem:

  1. source code

app.ts

import { ElementHandle, Page } from 'puppeteer';

class CustomElement {
	print(): void {
		console.log(`${Page.name} ${ElementHandle.name}`);
	}
}

const elem = new CustomElement();
elem.print();

package.json

{
	"module": "./app.js",
	"types": "./app.d.ts",
	"type": "module",
	"dependencies": {
		"puppeteer": "^16.2.0",
		"ts-node": "^10.9.1",
		"typescript": "^4.7.4"
	}
}

tsconfig.json

{
	"compilerOptions": {
		"declaration": true,
		"declarationMap": true,
		"sourceMap": true,
		"moduleResolution": "node",
		"module": "ESNext",
		"target": "ESNext",
		"lib": [
			"ESNext",
			"DOM",
		],
		"allowSyntheticDefaultImports": true,
		"emitDecoratorMetadata": true,
		"experimentalDecorators": true,
		"esModuleInterop": true,
	},
	"exclude": [ "node_modules" ],
}
  1. run the program $(npm bin)/ts-node --esm app.ts or node --loader ts-node/esm app.ts

Puppeteer version

16.2.0

Node.js version

16.8.0

npm version

7.14.0

What operating system are you seeing the problem on?

macOS

Relevant log output

user@MacBook-Pro app % $(npm bin)/ts-node --esm app.ts

file://app.ts:1
import { ElementHandle, Page } from 'puppeteer';
         ^^^^^^^^^^^^^
SyntaxError: The requested module 'puppeteer' does not provide an export named 'ElementHandle'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:179:5)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12)

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
OrKoNcommented, Sep 12, 2022

I don’t think we export those classes. We only export types for them. You can implement duck-typing currently but not the instanceof checks, e.g.,

class CustomElement {
	verify(obj: puppeteer.Page|puppeteer.ElementHandle): boolean {
		if ('goto' in obj) { // likely a Page
		   return true;
                 }
                 if ('click' in obj) { // likely an ElementHandle
                    return true;
                 }
                 return false;
	}
}
1reaction
Qlub53commented, Sep 12, 2022

Similarly,

import { ElementHandle, Page } from 'puppeteer';

class CustomElement {
	verify(obj: any): boolean {
		return obj instanceof Page || obj instanceof ElementHandle;
	}
}

const elem = new CustomElement();
elem.verify({});

leads to SyntaxError: The requested module 'puppeteer' does not provide an export named 'ElementHandle' and SyntaxError: The requested module 'puppeteer' does not provide an export named 'Page'

which may only be surppressed by either

		return obj instanceof puppeteer.Page || obj instanceof puppeteer.ElementHandle;

or

import puppeteer from 'puppeteer';
const { ElementHandle, Page } = puppeteer;

but will end up with a new error:

TypeError: Right-hand side of 'instanceof' is not an object
Read more comments on GitHub >

github_iconTop Results From Across the Web

"The requested module ' does not provide an export named ...
You've confirmed that the package.json in the module's folder doesn't have "type": "module" in it. That's the issue. It's using the older ...
Read more >
the requested module 'node-fetch' does not provide an export ...
node-postgres pg library that you are trying to import is transpiled. This means that you can't use named imports the way you are...
Read more >
node_modules/puppeteer/lib/types.d.ts - devtools ... - Google Git
A Browser is created when Puppeteer connects to a Chromium instance, either through ... JavaScript Coverage doesn't include anonymous scripts by default.
Read more >
export/import on Node.js is driving me crazy. - Reddit
But I keep getting this error: ... polygonCovalentConfig.js' does not provide an export named ... SyntaxError: The requested module '.
Read more >
Puppeteer documentation - DevDocs
Puppeteer Documentation. Overview. Puppeteer is a Node library which provides a high-level API to control Chromium or Chrome over the DevTools Protocol. The ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found