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

TypeError: form_data_1.default is not a constructor when adding attachment #327

Closed
xpawk opened this issue Oct 17, 2024 · 6 comments · Fixed by #333 or #341
Closed

TypeError: form_data_1.default is not a constructor when adding attachment #327

xpawk opened this issue Oct 17, 2024 · 6 comments · Fixed by #333 or #341
Assignees
Labels
enhancement Denotes a suggestion or request aimed at improving or adding new features to the project.
Milestone

Comments

@xpawk
Copy link

xpawk commented Oct 17, 2024

When attempting to add an attachment using the latest version of jira.js in an Astro project with Node.js v20.10.0, I encounter the following error:

TypeError: form_data_1.default is not a constructor

Steps to Reproduce:

  1. Initialize a new Astro project with TypeScript.
  2. Install jira.js and xlsx-populate.
  3. Use the following code to add an attachment:
import xlsx from "xlsx-populate";
import { Version3Client } from "jira.js";

export const jiraClient = new Version3Client({
	host: env.JIRA_HOST,
	authentication: {
		basic: {
			email: env.JIRA_PERSONAL_EMAIL,
			apiToken: env.JIRA_PERSONAL_API_TOKEN,
		},
	},
});

const objectToXLSX = async (object: any): Promise<Buffer> => {
  const workbook = await xlsx.fromBlankAsync();
  const sheet = workbook.sheet(0);
  
  // Add headers and values
  Object.keys(object).forEach((key, index) => {
    sheet.row(1).cell(index + 1).value(key).style("bold", true);
    sheet.row(2).cell(index + 1).value(object[key]);
  });
  
  return await workbook.outputAsync() as Buffer;
};

export const addJiraAttachment = async () => {
  const file = await objectToXLSX({ test: "123" });
  try {
    await JiraClient.issueAttachments.addAttachment({
      issueIdOrKey: "123",
      attachment: {
        file: file,
        filename: "user.xlsx",
      },
    });
    console.log("Attachment added successfully.");
  } catch (error) {
    console.error("Error adding attachment:", error);
  }
};

Environment:

Node.js: v20.10.0
typescript: "^5.6.2"
Astro: "^4.15.11",
jira.js: "^4.0.2",
xlsx-populate: "^1.21.0"
form-data: "^4.0.1",

Additional Context:

The error originates from issueAttachments.ts#L426 where it attempts to instantiate form_data_1.default. It appears that the form-data module isn't being imported correctly in an ESM context.

At version "jira.js": "^2.15.5", everything works correctly.

@axelvugts
Copy link

I'm also experiencing this issue using just plain NodeJS. Any advice would be appreciated!

@yoggu
Copy link

yoggu commented Nov 13, 2024

I'm also having the issue

krouma pushed a commit to krouma/jira.js that referenced this issue Dec 13, 2024
The newer library better supports modules and is written in TypeScript.
The FormData interface is a drop-in replacement except for getHeaders(),
but that didn't do anything useful, so it can be dropped.

Closes MrRefactoring#327

Signed-off-by: Matyáš Kroupa <[email protected]>
MrRefactoring added a commit that referenced this issue Jan 2, 2025
The newer library better supports modules and is written in TypeScript.
The FormData interface is a drop-in replacement except for getHeaders(),
but that didn't do anything useful, so it can be dropped.

Closes #327

Signed-off-by: Matyáš Kroupa <[email protected]>
Co-authored-by: Matyáš Kroupa <[email protected]>
Co-authored-by: Vladislav Tupikin <[email protected]>
@john-malone
Copy link

Any hope of getting a patch release on the 4.x line with this fix? We've recently started running into this issue and are looking for a fix or workaround.

@MrRefactoring
Copy link
Owner

I've just reviewed the formdata-node package and confirmed that it works seamlessly in both the browser and Node.js. It seems we can replace form-data with formdata-node without introducing any breaking changes.

@MrRefactoring MrRefactoring self-assigned this Jan 3, 2025
MrRefactoring added a commit that referenced this issue Jan 3, 2025
- Implemented `Buffer` to `File` conversion for attachment uploads.
- Added `attachment.mimeType` an optional property.
- Added automatic MIME type detection based on file extensions.
@MrRefactoring MrRefactoring added the enhancement Denotes a suggestion or request aimed at improving or adding new features to the project. label Jan 3, 2025
MrRefactoring added a commit that referenced this issue Jan 3, 2025
… v18.x.x by using `File` from `formdata-node`.

- Enhanced documentation with TSDoc and examples for better clarity.
MrRefactoring added a commit that referenced this issue Jan 3, 2025
…port (#341)

* #327: Package `form-data` replaced to `formdata-node` for add esm support

* #327: Add `Buffer` type support using `formdata-node` package

- Implemented `Buffer` to `File` conversion for attachment uploads.
- Added `attachment.mimeType` an optional property.
- Added automatic MIME type detection based on file extensions.

* #327: Fixed the issue with the absence of the `File` class in Node.js v18.x.x by using `File` from `formdata-node`.

- Enhanced documentation with TSDoc and examples for better clarity.
@MrRefactoring MrRefactoring added this to the v4.0.4 milestone Jan 3, 2025
@MrRefactoring
Copy link
Owner

Thank you to everyone who participated in this discussion. Special thanks to Matyáš Kroupa (@krouma) for proposing the fix. I plan to include this improvement in version 4.0.4.

MrRefactoring added a commit that referenced this issue Jan 3, 2025
- Improved the tree shaking process to ensure unused code is properly eliminated across the entire library.
- Refactored the codebase to resolve circular dependencies, improving tree shaking and maintainability.
MrRefactoring added a commit that referenced this issue Jan 4, 2025
The newer library better supports modules and is written in TypeScript.
The FormData interface is a drop-in replacement except for getHeaders(),
but that didn't do anything useful, so it can be dropped.

Closes #327

Signed-off-by: Matyáš Kroupa <[email protected]>
Co-authored-by: Matyáš Kroupa <[email protected]>
Co-authored-by: Vladislav Tupikin <[email protected]>
MrRefactoring added a commit that referenced this issue Jan 4, 2025
* #327: Package `form-data` replaced to `formdata-node` for add esm support (#341)

* #327: Package `form-data` replaced to `formdata-node` for add esm support

* #327: Add `Buffer` type support using `formdata-node` package

- Implemented `Buffer` to `File` conversion for attachment uploads.
- Added `attachment.mimeType` an optional property.
- Added automatic MIME type detection based on file extensions.

* #327: Fixed the issue with the absence of the `File` class in Node.js v18.x.x by using `File` from `formdata-node`.

- Enhanced documentation with TSDoc and examples for better clarity.

* #320: Fix tree shaking mechanism and remove circular dependencies (#343)

- Improved the tree shaking process to ensure unused code is properly eliminated across the entire library.
- Refactored the codebase to resolve circular dependencies, improving tree shaking and maintainability.
@john-malone
Copy link

Thank you! Looks like this is working well now (with 4.0.5)! I did need to add Buffer.from when using string content, and am now passing an explicit mimeType.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Denotes a suggestion or request aimed at improving or adding new features to the project.
Projects
None yet
5 participants