dolanmiu / docx

Easily generate and modify .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
https://docx.js.org/
MIT License
4.44k stars 486 forks source link

nodenext: Module '"docx"' has no exported member 'Paragraph'. #2635

Closed Spitzbua closed 1 month ago

Spitzbua commented 8 months ago

Cannot import any other Types but document using Basic Usage Example

import { Document, Packer, Paragraph, TextRun } from "docx";

leads to following error: Module '"docx"' has no exported member 'Paragraph'

I tried several import variants, but none of them was working: https://github.com/dolanmiu/docx/issues/1201, https://github.com/dolanmiu/docx/issues/2365

Is it a misconfiguration or an library issue? Any help appreciated!

Options:

{ "compilerOptions": { "module": "nodenext", "moduleResolution": "nodenext", "noImplicitReturns": true, "noUnusedLocals": false, "skipLibCheck": true, "outDir": "lib", "sourceMap": true, "strict": true, "target": "es6", "typeRoots": [ "./node_modules/@types", "./types" ] }, "compileOnSave": true, "include": [ "src" ] }

OperationsOfficerPRMG commented 7 months ago

I'm also experiencing this, is anyone able to please help?

I was able to use this module in pure JS but when switching to TS it won't compile and gives many errors which look like this:

Property 'Packer' does not exist on type 'typeof import("./node_modules/docx/build/index", { with: { "resolution-mode": "import" } })'.

The compiler will only seem to accept something like the following (non-sensical) code.

const { Document } = docx; const { Packer, Paragraph, Table, TableCell, TableRow, Header, HeadingLevel, Footer, LevelFormat, TextRun, WidthType, AlignmentType, VerticalAlign, convertMillimetersToTwip, TableBorders, ShadingType, BorderStyle, ImageRun, HorizontalPositionAlign, VerticalPositionAlign, TextWrappingType, UnderlineType, FrameAnchorType } = Document;

Which gives errors like

TypeError: convertMillimetersToTwip is not a function

Here's my tsconfig,json

{ "$schema": "https://json.schemastore.org/tsconfig", "display": "Node 21", "_version": "21.0.0", "files": [ "index.mts", "index.d.ts" ], "compilerOptions": { "lib": [ "es2023", "es5", "es6", "dom", "dom.iterable" ], "module": "node16", "target": "es2022", "strict": true, "esModuleInterop": true, "skipLibCheck": true,
"forceConsistentCasingInFileNames": true, "moduleResolution": "node16" } }

tothdanielax commented 7 months ago

Same here, even if it seems to work in local context.

import { IPatch, PatchType, TextRun, patchDocument } from 'docx';

TS could not infer any of these types, tsc tells this: > error TS2305: Module '"docx"' has no exported member 'patchDocument'.

UrfTheManatee commented 5 months ago

I can confirm the same issue.

sflanker commented 3 months ago

This issue stems from the fact that with recent versions of Node and ECMAScript, import statements in modules must include the actual post-transpilation file extension. TSC and Vite do not (and seemingly will not ever) rewrite import paths. The actual JavaScript module produced by the build is flattened into a single .mjs file, but the type declarations were being left expanded across multiple files.

As an alternative to this simple and trivial fix in PR #2714 another option would be to update all relative path imports to include a file extension, but surely nobody would be foolish enough to... https://github.com/dolanmiu/docx/compare/master...IntelliBridge-pwheeler:docx:modern-esm-typescript-support 🤦

cjreimer commented 2 months ago

Hello,

Is there any update on this issue? I am also experiencing this. Thanks to @sflanker and anyone else working on this!

Lancelot1976 commented 1 month ago

Hello,

Is there any update on this issue? I am also experiencing this. Thanks to @sflanker and anyone else working on this!

Hi I also have this issue and would be very happy to get a solution