modesty / pdf2json

converts binary PDF to JSON and text, for server-side PDF processing and command-line use.
https://github.com/modesty/pdf2json
Other
2.02k stars 377 forks source link

ERR_REQUIRE_ESM error while importing pdf2json #308

Closed RevTpark closed 6 months ago

RevTpark commented 1 year ago

Node: v18.16.1 pdf2json: 3.0.4 Using express with Typescript

When i normally import the package like import Pdfparser from 'pdf2json';, it throws the below error const pdf2json_1 = __importDefault(require("pdf2json")); [1] ^ [1] [1] Error [ERR_REQUIRE_ESM]: require() of ES Module project\node_modules\pdf2json\pdfparser.js from project\dist\routes\master-data.js not supported. [1] Instead change the require of pdfparser.js in project\dist\routes\master-data.js to a dynamic import() which is available in all CommonJS modules. [1] at Object. (project\dist\routes\master-data.js:44:36) [1] at Object. (project\dist\index.js:102:39) { [1] code: 'ERR_REQUIRE_ESM' [1] }

According to issue #282 , I tried to implement changes as suggested but I still got the following error.

[1] Object.defineProperty(exports, "__esModule", { value: true }); [1] ^ [1] [1] ReferenceError: exports is not defined in ES module scope [1] This file is being treated as an ES module because it has a '.js' file extension and 'project\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. [1] at file:///project/dist/index.js:37:23 [1] at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

isimisi commented 1 year ago

317 - until he accepts the pr I've created npm package @isimisi/pdf2json

modesty commented 6 months ago

v3.1.2 supports both esm and cls now