Hopding / pdf-lib

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

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

Closed maxschulmeister closed 5 months ago

maxschulmeister commented 5 months 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

maxschulmeister commented 5 months 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.