pdf-lib icon indicating copy to clipboard operation
pdf-lib copied to clipboard

Multiple Typescript compile errors TS2307: Cannot find module

Open tiholic opened this issue 3 years ago • 4 comments

What were you trying to do?

run tsc after importing pdf-lib into my node project

How did you attempt to do it?

npm i pdf-lib@latest import { PDFDocument } from 'pdf-lib';

const pdfDoc = await PDFDocument.load(
      file_path,
      { updateMetadata: false },
    );

What actually happened?

Compilation failed with errors:

node_modules/pdf-lib/src/api/PDFPageOptions.ts:1:23 - error TS2307: Cannot find module 'src/api/colors' or its corresponding type declarations.

1 import { Color } from 'src/api/colors';
                        ~~~~~~~~~~~~~~~~

node_modules/pdf-lib/src/api/PDFPageOptions.ts:2:21 - error TS2307: Cannot find module 'src/api/PDFFont' or its corresponding type declarations.

2 import PDFFont from 'src/api/PDFFont';
                      ~~~~~~~~~~~~~~~~~

node_modules/pdf-lib/src/api/PDFPageOptions.ts:3:26 - error TS2307: Cannot find module 'src/api/rotations' or its corresponding type declarations.

3 import { Rotation } from 'src/api/rotations';
                           ~~~~~~~~~~~~~~~~~~~

node_modules/pdf-lib/src/api/PDFPageOptions.ts:4:30 - error TS2307: Cannot find module 'src/api/operators' or its corresponding type declarations.

4 import { LineCapStyle } from 'src/api/operators';
                               ~~~~~~~~~~~~~~~~~~~


Found 4 errors.

What did you expect to happen?

Compile successfully.

How can we reproduce the issue?

I'm using this tsconfig.json

{
  "compilerOptions": {
    "incremental": true,
    "target": "ES2019",
    "module": "commonjs",
    "declaration": true,
    "sourceMap": true,
    "outDir": "./build",
    "removeComments": true,
    "strict": true,
    "baseUrl": "./",
    "typeRoots": [
      "./typings",
      "./node_modules/@types"
    ],
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true
  },
  "exclude": ["node_modules", "test", "**/*.spec.ts", "build"]
}

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

  • [X] My report includes a Short, Self Contained, Correct (Compilable) Example.
  • [X] I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

in pdf-lib's tsconfig.json, "moduleResolution" is configured as "node", but the relative imports to project root are used, like: 'src/api/colors' (see: https://github.com/Hopding/pdf-lib/blob/master/src/api/PDFPageOptions.ts)

I'm using node moduleResolution in my project and pdf-lib seems to follow classic moduleResolution strategy. Is there a way out to solve this issue?

tiholic avatar Dec 22 '21 19:12 tiholic

Found the issue. It was due to a bad import:

import { PDFPageDrawImageOptions } from 'pdf-lib/src/api/PDFPageOptions'; should have been import { PDFPageDrawImageOptions } from 'pdf-lib';

And this confusion was created due to the src folder being available in the installed package. Not sure if it a good design, but I think the src folder shouldn't be included in npm releases.

I found the above issue only when I manually deleted the node_modules/pdf-lib/src folder.

tiholic avatar Jan 04 '22 05:01 tiholic

This is still an issue for vite users. It constantly alerts when importing from pdf-lib

dorsharonfuse avatar Jan 30 '22 16:01 dorsharonfuse

Not sure if it a good design, but I think the src folder shouldn't be included in npm releases.

Agree, I also feel the src/ directory shouldn't be in the npm release. It is included in the files to publish in package.json

btielen avatar Feb 04 '22 11:02 btielen

This is still an issue for vite users. It constantly alerts when importing from pdf-lib

i use: import { PDFDocument } from 'pdf-lib/dist/pdf-lib.js' instead of import { PDFDocument } from 'pdf-lib'

this way all the sourcemap warings are gone :-)

valueerrorx avatar Apr 03 '22 20:04 valueerrorx