Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Typescript compile errors TS2307: Cannot find module #1126

Open
2 tasks done
tiholic opened this issue Dec 22, 2021 · 4 comments
Open
2 tasks done

Multiple Typescript compile errors TS2307: Cannot find module #1126

tiholic opened this issue Dec 22, 2021 · 4 comments

Comments

@tiholic
Copy link

tiholic commented Dec 22, 2021

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

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • 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
Copy link
Author

tiholic commented Jan 4, 2022

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.

@dorsharonfuse
Copy link

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

@btielen
Copy link

btielen commented Feb 4, 2022

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

@valueerrorx
Copy link

valueerrorx commented Apr 3, 2022

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 :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants