Hopding / pdf-lib

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

ES import with typescript results in Could not resolve "pdf-lib" #1642

Closed max-schu closed 1 month ago

max-schu commented 1 month ago

What were you trying to do?

I'm simply trying to import "pdf-lib" for usage in a figma plugin.

How did you attempt to do it?

import { PDFDocument } from "pdf-lib";

What actually happened?

ts outputs Could not resolve "pdf-lib"

What did you expect to happen?

a clean import

How can we reproduce the issue?

tsconfig.json

{
  "compilerOptions": {
   "esModuleInterop": true,
    "isolatedModules": true,
    "jsx": "react",
    "jsxFactory": "h",
    "lib": ["DOM", "ES2020"],
    "module": "ES2020",
    "moduleResolution": "Node",
    "strict": true,
    "typeRoots": ["node_modules/@figma", "node_modules/@types"],
    "paths": {
      "@/*": ["./src/*"]
    },
    "target": "es2017"
  },
  "include": ["src/**/*.ts", "src/**/*.tsx"]
}

Version

latest

What environment are you running pdf-lib in?

Browser

Checklist

Additional Notes

No response

max-schu commented 1 month ago

this was an issue related to esbuild and its "platform" parameter.

setting mainFields for esbuild buildOptions to
mainFields: ["module", "main", "browser"],

fixed the issue.

save to close this.