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

Compile issue with TypeScript and CommonJS with the shipped types for fast-xml-parser #724

Open
3 of 7 tasks
mpodwysocki opened this issue Feb 24, 2025 · 2 comments
Open
3 of 7 tasks

Comments

@mpodwysocki
Copy link

  • Are you running the latest version?
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool?
  • Have you checked the docs for helpful APIs and examples?

Description

When targeting both ESM and CommonJS for TypeScript, the CommonJS targeting will error with the following:

error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("fast-xml-parser")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts' or create a local package.json file with `{ "type": "module" }`.

4 import { XMLBuilder, XMLParser, XMLValidator } from "fast-xml-parser";

The issue is that you are shipping a single set of types for both ESM and CommonJS as noted here:

  "exports": {
    ".": {
      "import": "./src/fxp.js",
      "require": "./lib/fxp.cjs",
      "types": "./src/fxp.d.ts",
      "default": "./src/fxp.js"
    }
  },

In this case you are shipping your ESM types, ./src/fxp.d.ts for both ESM and CJS, which is a bug as noted on Are the Types Wrong? - fast-xml-parser

This gives helpful hints that this library is masquerading as ESM, and instead you should ship both a set of types for CommonJS and ESM such as the following:

  "exports": {
    ".": {
      "import": {
        "types": "./dist/esm/index.d.ts",
        "default": "./dist/esm/index.js"
      },
      "require": {
        "types": "./dist/commonjs/index.d.ts",
        "default": "./dist/commonjs/index.js"
      }
    }
  },

Input

Upgrading the @azure/core-xml project to use the latest fast-xml-parser as noted here: Azure/azure-sdk-for-js#33172 (comment)

Code

This code will fail if we are using the targeting for CommonJS:

import { XMLBuilder, XMLParser, XMLValidator } from "fast-xml-parser";

We can get rid of the overall compiler error with @ts-ignore but this is not ideal

@ts-ignore
import { XMLBuilder, XMLParser, XMLValidator } from "fast-xml-parser";

Output

error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("fast-xml-parser")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts' or create a local package.json file with `{ "type": "module" }`.

4 import { XMLBuilder, XMLParser, XMLValidator } from "fast-xml-parser";

expected data

Expected to compile, however does not unless using @ts-ignore.

Would you like to work on this issue?

  • Yes
  • No

Bookmark this repository for further updates. Visit SoloThought to know about recent features.

Copy link

We're glad you find this project helpful. We'll try to address this issue ASAP. You can vist https://solothought.com to know recent features. Don't forget to star this repo.

mpodwysocki added a commit to Azure/azure-sdk-for-js that referenced this issue Feb 24, 2025
### Packages impacted by this PR

- @azure/core-xml

### Issues associated with this PR

- #33172

### Describe the problem that is addressed by this PR

Updates to latest `fast-xml-parser`, however, has issues with the types
as noted in the PR in the `fast-xml-parser` repo
NaturalIntelligence/fast-xml-parser#724

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
amitguptagwl added a commit that referenced this issue Feb 25, 2025
@amitguptagwl
Copy link
Member

Added the typings for CJS. Can you please check once?

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

No branches or pull requests

2 participants