npm-pdfreader icon indicating copy to clipboard operation
npm-pdfreader copied to clipboard

[ERR_REQUIRE_ESM] require() of ES Module is not supported

Open DianaIonita opened this issue 1 year ago • 11 comments

Describe the bug I am unable to import or require the module to use in my nodejs application.

To Reproduce

Create a typescript file (I named it read-pdf.ts).

import { PdfReader } from "pdfreader";
const path = './doc.pdf';

new PdfReader().parseFileItems(path, (err, item) => {
    if (err) console.error("error:", err);
    else if (!item) console.warn("end of file");
    else if (item.text) console.log(item.text);
});

I then ran

yarn tsc read-pdf.ts

which generated:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var pdfreader_1 = require("pdfreader");
var path = './doc.pdf';
new pdfreader_1.PdfReader().parseFileItems(path, function (err, item) {
    if (err)
        console.error("error:", err);
    else if (!item)
        console.warn("end of file");
    else if (item.text)
        console.log(item.text);
});

Then finally,

node read-pdf.js

Expected behavior I expected the program to run without errors.

Screenshots, outputs or logs

var pdfreader_1 = require("pdfreader");
                  ^

Error [ERR_REQUIRE_ESM]: require() of ES Module ...\node_modules\pdfreader\index.js from ...\read-pdf.js not supported.
Instead change the require of index.js in ...\read-pdf.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (...\read-pdf.js:3:19) {
  code: 'ERR_REQUIRE_ESM'
}

Desktop (please complete the following information):

~- OS: (e.g. iOS)~ ~- Browser: (e.g. chrome, safari)~ ~- Version: (e.g. 22)~

Using nodejs 16.16.0, server side only, no browser required. Intended to run across OSes.

DianaIonita avatar Jul 10 '23 17:07 DianaIonita

I just resolve this issue using firebase cloud functions

I'm using [email protected], set this to your package.json

"type: "module""

Since your working with Typescript, try this config to your tsconfig.ts

{ "compilerOptions": { "module": "ES2022", "noImplicitReturns": true, "moduleResolution": "Node16", "noUnusedLocals": true, "outDir": "lib", "sourceMap": true, "strict": true, "target": "es2017" }, "compileOnSave": true, "include": [ "src" ] }

Vscode could help you resolve some import issues :)

Sorvereign avatar Jul 11 '23 02:07 Sorvereign

I just resolve this issue using firebase cloud functions

I'm using [email protected], set this to your package.json

"type: "module""

Since your working with Typescript, try this config to your tsconfig.ts

{ "compilerOptions": { "module": "ES2022", "noImplicitReturns": true, "moduleResolution": "Node16", "noUnusedLocals": true, "outDir": "lib", "sourceMap": true, "strict": true, "target": "es2017" }, "compileOnSave": true, "include": [ "src" ] }

Vscode could help you resolve some import issues :)

Thanks for your help, @Sorvereign, but it doesn't solve my problem. I did as you suggested, updated package.json and tsconfig.json, then compiled the typescript file and:

.../read-pdf.js:2
Object.defineProperty(exports, "__esModule", { value: true });
                      ^

ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '...\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

I took the hint and renamed the compiled js file to read-pdf.cjs and tried:

> node read-pdf.cjs 

...\read-pdf.cjs:3
var pdfreader_1 = require("pdfreader");
                  ^

Error [ERR_REQUIRE_ESM]: require() of ES Module ...\node_modules\pdfreader\index.js from ...\read-pdf.cjs not supported.
Instead change the require of index.js in ...\read-pdf.cjs to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (...\read-pdf.cjs:3:19) {
  code: 'ERR_REQUIRE_ESM'
}

DianaIonita avatar Jul 11 '23 14:07 DianaIonita

I used NodeJS 18.17.1, I've tried using the following setup and it worked.

Add type: module to your package.json

And for tsconfig.json, you should add the following

{
  "module": "ES2022",
  "moduleResolution": "Node",
}

irfandyj avatar Sep 01 '23 15:09 irfandyj

Good job!

Would you be interested to submit these changes in a pull request?

Adrien

Le ven. 1 sept. 2023 à 17:05, Jip Irfandy @.***> a écrit :

I used NodeJS 18.17.1, I've tried using the following setup and it worked.

Add type: module to your package.json

And for tsconfig.json, you should add the following

{ "module": "ES2022", "moduleResolution": "Node",}

— Reply to this email directly, view it on GitHub https://github.com/adrienjoly/npm-pdfreader/issues/133#issuecomment-1702902258, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEB2ROY7TQLYQFS7NW5XITXYH2T5ANCNFSM6AAAAAA2E2QDXI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

adrienjoly avatar Sep 02 '23 04:09 adrienjoly

Thanks for the offer! I'll gladly help, but I currently have my hands full right now. But maybe if we choose to use this library in the future, I might be able to help.

irfandyj avatar Sep 03 '23 09:09 irfandyj

Maybe for others, from what I've looked, and basically, the problem is, that the package did not support CommonJS, which is the default module used by TypeScript settings. It could be a concern since it's a default TypeScript setting after all. So to fix those things up, I just changed my TypeScript settings to use ES Modules instead of CommonJS.

irfandyj avatar Sep 03 '23 09:09 irfandyj

this article may help: How to Create a Dual-Mode Cross-Runtime Package

adrienjoly avatar Sep 07 '23 14:09 adrienjoly

I tried to rollup to cjs, however pdf2json has the same issue - so before this package can rollup to cjs, pdf2json has to do that aswell. At least as far as I could tell

update: I've created a PR in pdf2json

isimisi avatar Sep 22 '23 11:09 isimisi

Any date to fix this issue? The @isimisi pr from pdf2json has been merged

VitorBrangioni avatar Nov 22 '23 19:11 VitorBrangioni

I don't have time to do it at the moment. Feel free to send a pull request, I'll review and merge it.

adrienjoly avatar Nov 23 '23 06:11 adrienjoly

Hi all!

@kvnglvz just sent a PR intending to fix that problem. (See the mention above this message)

Can any of you test it in their project and/or provide review suggestions on how to improve it before merging it, please?

(On the top of my head, I would prefer to reduce the number of changes, e.g. avoid duplicating the contents of index.js and/or updating so many deps)

adrienjoly avatar Dec 01 '23 06:12 adrienjoly

@adrienjoly I created another PR to address this topic without having the index file duplicated in the repo. Would be great to get this reviewed and merged :)

I tested with my typescript project locally and it resolved the issues I had.

Pipo93 avatar Jul 29 '24 17:07 Pipo93

Should be fixed now that #156 was merged.

=> released in v3.0.3.

Thank you for your contribution, Pipo93!

adrienjoly avatar Aug 03 '24 10:08 adrienjoly