adrienjoly / npm-pdfreader

🚜 Parse text and tables from PDF files.
https://www.npmjs.com/package/pdfreader
MIT License
631 stars 84 forks source link

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

Closed DianaIonita closed 3 months ago

DianaIonita commented 1 year ago

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.

Sorvereign commented 1 year ago

I just resolve this issue using firebase cloud functions

I'm using node@18.15.0, 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 :)

DianaIonita commented 1 year ago

I just resolve this issue using firebase cloud functions

I'm using node@18.15.0, 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'
}
irfandyj commented 1 year ago

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",
}
adrienjoly commented 1 year ago

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: @.***>

irfandyj commented 1 year ago

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 commented 1 year ago

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.

adrienjoly commented 1 year ago

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

isimisi commented 1 year ago

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

VitorBrangioni commented 11 months ago

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

adrienjoly commented 11 months ago

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 commented 11 months ago

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)

Pipo93 commented 3 months ago

@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.

adrienjoly commented 3 months ago

Should be fixed now that #156 was merged.

=> released in v3.0.3.

Thank you for your contribution, Pipo93!