cantoo-scribe / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
136 stars 27 forks source link

ESM broken #29

Closed thantos closed 1 year ago

thantos commented 1 year ago

What were you trying to do?

Use pdf-lib in node 18 with esm, esbuild (esm) to bundle, and run locally with nodejs in esm.

How did you attempt to do it?

import {PDFDocument} from "@cantoo/pdf-lib"
import PdfLib from "@cantoo/pdf-lib"
const {PDFDocument} = PdfLib;
import * as  PdfLib from "@cantoo/pdf-lib"

What actually happened?

Outcome of the import patterns in order

  1. node 18 complains that the module is CJS and destructuring isn't supported
  2. works for node, but esbuild complains that there is no default export for es/index.js
  3. works for esbuild, but at runtime with esm on nodejs, all of the exports are undefined (because there is no default export).

What did you expect to happen?

I expect at least import pattern 1 to work.

How can we reproduce the issue?

index.mjs:

import { PDFDocument } from "@cantoo/pdf-lib";
PDFDocument.create();
{
  "name": "pdf-lib-sample",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "devDependencies": {
    "@cantoo/pdf-lib": "^1.19.11",
  },
  "author": "",
  "license": "ISC"
}

npm i node index.mjs

Version

=1.19

What environment are you running pdf-lib in?

Node, Other

Checklist

Additional Notes

For typescript to recognize the es/index.js import, it needs a package.json with { type: "module" } in the directory at a minimum.

For node, it needs the new "exports" block in package.json

  "exports": {
    ".": {
      "types": "./cjs/index.d.ts",
      "require": "./cjs/index.js",
      "import": "./es/index.js"
    }
  },

Additionally, the es/* directory imports need to be updated to use full file paths.

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '[redacted]/pdf-lib-sample/node_modules/@cantoo/pdf-lib/es/core/errors' imported from [redacted]/pdf-lib-sample/node_modules/@cantoo/pdf-lib/es/core/index.js

Is fixed by changing es/index.js to:

export * from './api/index.js';
export * from './core/index.js';
export * from './types/index.js';
export * from './utils/index.js';

But needs to be repeated all of the way down.

My current resolution is to downgrade back to 1.17, which does not have this issue.

Sharcoux commented 1 year ago

I'll be on it. Sorry for the delay.

Sharcoux commented 1 year ago

Can you try the prepatch and tell me if it fixes the issue? Version is: 1.19.13-0

Sharcoux commented 1 year ago

@thantos Did the prepatch fix the issue? I'll be releasing version 1.19.13 now.

thantos commented 1 year ago

Haven't had a chance to try yet, will try later today.

Sharcoux commented 1 year ago

I released version 1.19.13. I decided to embark the change, as the current version is broken anyway. Not much risk to break anything, right?

bluepuma77 commented 1 year ago

Assuming this is related:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'color.js' imported from /Users/.../node_modules/@cantoo/pdf-lib/es/api/colors.js
Sharcoux commented 1 year ago

Ah, indeed. Let me fix that

Sharcoux commented 1 year ago

Fixed in 1.19.14

thantos commented 1 year ago

Its working for me so far, thanks for the fix!