Skip to content

fix: main field in package.json should correspond to cjs artifacts#5756

Merged
jasonsaayman merged 4 commits intoaxios:v1.xfrom
thebanjomatic:patch-1
Dec 30, 2025
Merged

fix: main field in package.json should correspond to cjs artifacts#5756
jasonsaayman merged 4 commits intoaxios:v1.xfrom
thebanjomatic:patch-1

Conversation

@thebanjomatic
Copy link
Contributor

@thebanjomatic thebanjomatic commented Jun 29, 2023

When #4787 was implemented, the project was switched to "type": "module" and "./index.js" became an esm file instead of commonjs, however, the "main" entry in package.json still points to "index.js". As a result, consumers using this field may get unexpected behavior since the main field is supposed to be commonjs if the entry is provided.

Many consumers won't run into this as a practical problem (for example when just doing const axios = require('axios').default from inside of a cjs file in node) because the "exports" map takes precedence over the main/module fields, but tools that don't parse the exports map when resolving still run into problems here. As an example, I encountered this problem when using the resolve package as part of a workflow for a jest config.

The fix for this is to just point the "main" entry-point to the commonjs artifacts located at "./dist/node/axios.cjs".

I also added a module entrypoint to improve compatibility for the cases where the export map is not used (webpack 4 for example) since that would likely be reading the cjs "main" entrypoint now that main has switched back to cjs.

When axios#4787 was implemented, the project was switched to `"type": "module"` and "./index.js" became an esm file instead of commonjs, however, the "main" entry in package.json still points to "index.js". As a result, consumers using this field may get unexpected behavior since the main field is supposed to be commonjs if the entry is provided.

Many consumers won't run into this as a practical problem (for example when just doing `const axios = require('axios').default` from inside of a cjs file in node) because the "exports" map takes precedence over the main/module fields, but tools that don't parse the object map when resolving still run into problems here.

The fix for this is to just point the "main" entry-point to the commonjs artifacts located at "./dist/node/index.cjs".

I also added a module entrypoint to improve compatability for the cases where the export map is not used (webpack 4 for example) since that would likely be reading the cjs "main" entrypoint now that main has switched back to cjs.
@thebanjomatic
Copy link
Contributor Author

This PR has been sitting for a few months but the underlying issue has been causing me all sorts of headaches as we need to keep adding the following to our jest.config.js to work around these issues which has been confusing for our end users (since they aren't importing axios directly and its a transitive dependency):

  moduleNameMapper: {
    '^axios$': require.resolve('axios'),
  },

@DigitalBrainJS I don't know who the current project owners are, but is this something you are able to review and release?

@DigitalBrainJS
Copy link
Collaborator

the project was switched to "type": "module" and "./index.js" became an esm file instead of commonjs, however, the "main" entry in package.json still points to "index.js".

Why do you think this should not be so? Axios is ESM module and according to the documentation, the main field must point to an ESM module if the package has "type: module". "Module" field is not in npm spec and it is just a proprietary field for some bundlers. According to the npm documentation, backward compatibility with commonjs for an ESM package should be handled by conditional export.
Axios should follow the official specification, and not adapt to special cases arising from bugs, obsolescence, and inconsistencies of third-party libraries. If a third-party library has a bug or lack of support for functionality, then it must be resolved on its part.
In addition, node v12.0-12.7 does not support conditional export so the main field will be used.

@thebanjomatic
Copy link
Contributor Author

thebanjomatic commented Oct 6, 2023

Why do you think this should not be so? Axios is ESM module and according to the documentation, the main field must point to an ESM module if the package has "type: module".

I haven't found that in the node documentation specifically that implies the main field must be the es-module the package type is module. I could only find documentation that suggested that it could point to cjs or esm (in which case the package would not be consumable from a require() expression because you can't require esm files.

For example, there is the following quote:

All currently supported versions of Node.js and modern build tools support the "exports" field. For projects using an older version of Node.js or a related build tool, compatibility can be achieved by including the "main" field alongside "exports" pointing to the same module:

{
 "main": "./index.js",
 "exports": "./index.js"
} 

While that says the can point to the same module, their example doesn't use conditional exports or specify "type": "module", so I think the documentation is unclear. That certainly would be compatible with old versions of node assuming index.js is commonjs (which it would be given the extension and lack of type entry), but if that was an es-module it would fail to be require in old versions of node, and you would only support import in node 12.0-7. Since you can both import and require cjs entries from cjs and esm, it follows that setting the main entrypoint to point to the commonjs entry would be the most compatible way to specify the main entry for cases where exports is not used.

https://nodejs.org/api/packages.html
https://nodejs.org/api/esm.html#modules-ecmascript-modules

Axios should follow the official specification, and not adapt to special cases arising from bugs, obsolescence, and inconsistencies of third-party libraries [...] In addition, node v12.0-12.7 does not support conditional export so the main field will be used.

So I tested this in node 12.0 and as you say, the conditional export isn't used and so the following code fails to work in older versions of node where the main field is used instead of the export map:

const {default: axios} = require('axios');
axios.get('http://www.google.com').then(result => console.log(result.data));

Fails with:

import axios from './lib/axios.js';
       ^^^^^
SyntaxError: Unexpected identifier

But node versions 12.0-7 were never LTS versions, and they are certainly out of date, so maybe we don't need the main field at all if your intention is to not support special cases as a result of obsolescence.

I guess my question is, if we are relying on the exports map for this functionality to work correctly in node, what is the intended use-case for setting the "main" entrypoint to point to the esm version of the package? The field will never be used in current versions of node and breaks in old versions, so maybe it is because that is what would be used by bundlers that don't support the exports map? If that's the case, those same bundlers support the proprietary "module" and "browsers" entry-points.

There's also a ton of other entries already in this package.json for supporting use-cases outside of the npm specificaton:
https://github.com/axios/axios/blob/7a60e50ab97860a908cee831f99d00a1e3b4ef0c/package.json#L140-L146C32

I can maybe see the case for using:

"browser": {
  ".": "./index.js",

instead of "module": "index.js" assuming the two would be equivalent if you didn't wan't to add another non-standard field, but I still think setting the main field to point to commonjs is within specifications and improves compatibility.

@thebanjomatic
Copy link
Contributor Author

@DigitalBrainJS I have worked around this issue on my end by removing my dependency on resolve/browser-resolve. I still think it would be a beneficial change to make, but if you disagree, feel free to close this PR. Thanks!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a packaging issue where the main field in package.json was pointing to an ESM file (index.js) instead of the CommonJS artifact after the project switched to "type": "module". This caused compatibility issues for tools that don't parse the exports map and rely solely on the main field.

Key changes:

  • Changed main field from "index.js" to "./dist/node/axios.cjs" to point to the CommonJS build
  • Added module field pointing to "./index.js" for better compatibility with bundlers that don't support exports maps (e.g., webpack 4)

Gudahtt added a commit to Gudahtt/axios that referenced this pull request Feb 9, 2026
Browserify and React Native projects (that aren't using
`unstable_enablePackageExports`) use the `main` field as the package

entrypoint. A recent PR (axios#5756) updated `main` to use a CommonJS bundle
for Node.js, which caused Browserify and React Native projects to
use the Node.js bundle. This led to many reports of broken React Native
builds.

This has been fixed by adding an entry to `browser` and `react-native`
to re-map the Node.js CommonJS bundle to the browser CommonJS bundle.
jasonsaayman pushed a commit that referenced this pull request Feb 10, 2026
Browserify and React Native projects (that aren't using
`unstable_enablePackageExports`) use the `main` field as the package

entrypoint. A recent PR (#5756) updated `main` to use a CommonJS bundle
for Node.js, which caused Browserify and React Native projects to
use the Node.js bundle. This led to many reports of broken React Native
builds.

This has been fixed by adding an entry to `browser` and `react-native`
to re-map the Node.js CommonJS bundle to the browser CommonJS bundle.
sgmakgg added a commit to boringstudio-org/mcp-gitea that referenced this pull request Mar 13, 2026
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [axios](https://axios-http.com) ([source](https://github.com/axios/axios)) | dependencies | patch | [`1.13.2` → `1.13.4`](https://renovatebot.com/diffs/npm/axios/1.13.2/1.13.4) |

---

### Release Notes

<details>
<summary>axios/axios (axios)</summary>

### [`v1.13.4`](https://github.com/axios/axios/releases/tag/v1.13.4)

[Compare Source](axios/axios@v1.13.3...v1.13.4)

#### Overview

The release addresses issues discovered in v1.13.3 and includes significant CI/CD improvements.

**Full Changelog**: [v1.13.3...v1.13.4](axios/axios@v1.13.3...v1.13.4)

#### What's New in v1.13.4

##### Bug Fixes

- **fix: issues with version 1.13.3** ([#&#8203;7352](axios/axios#7352)) ([ee90dfc](axios/axios@ee90dfc))
  - Fixed issues discovered in v1.13.3 release
  - Cleaned up interceptor test files
  - Improved workflow configurations

##### Infrastructure & CI/CD

- **refactor: ci and build** ([#&#8203;7340](axios/axios#7340)) ([8ff6c19](axios/axios@8ff6c19))
  - Major refactoring of CI/CD workflows
  - Consolidated workflow files for better maintainability
  - Added mise configuration for the development environment
  - Improved sponsor block update automation
  - Enhanced issue and PR templates
  - Added automatic release notes generation
  - Implemented workflow cancellation for concurrent runs

- **chore: codegen and some updates to workflows** ([76cf77b](axios/axios@76cf77b))
  - Code generation improvements
  - Workflow optimisations

#### Migration Notes

##### Breaking Changes

None in this release.

##### Deprecations

None in this release.

#### Contributors

Thank you to all contributors who made this release possible! Special thanks to:

- [jasonsaayman](https://github.com/jasonsaayman) - Release management and CI/CD improvements

### [`v1.13.3`](https://github.com/axios/axios/blob/HEAD/CHANGELOG.md#1133-2026-01-20)

[Compare Source](axios/axios@v1.13.2...v1.13.3)

##### Bug Fixes

- **http2:** Use port 443 for HTTPS connections by default. ([#&#8203;7256](axios/axios#7256)) ([d7e6065](axios/axios@d7e6065))
- **interceptor:** handle the error in the same interceptor ([#&#8203;6269](axios/axios#6269)) ([5945e40](axios/axios@5945e40))
- main field in package.json should correspond to cjs artifacts ([#&#8203;5756](axios/axios#5756)) ([7373fbf](axios/axios@7373fbf))
- **package.json:** add 'bun' package.json 'exports' condition. Load the Node.js build in Bun instead of the browser build ([#&#8203;5754](axios/axios#5754)) ([b89217e](axios/axios@b89217e))
- silentJSONParsing=false should throw on invalid JSON ([#&#8203;7253](axios/axios#7253)) ([#&#8203;7257](axios/axios#7257)) ([7d19335](axios/axios@7d19335))
- turn AxiosError into a native error ([#&#8203;5394](axios/axios#5394)) ([#&#8203;5558](axios/axios#5558)) ([1c6a86d](axios/axios@1c6a86d))
- **types:** add handlers to AxiosInterceptorManager interface ([#&#8203;5551](axios/axios#5551)) ([8d1271b](axios/axios@8d1271b))
- **types:** restore AxiosError.cause type from unknown to Error ([#&#8203;7327](axios/axios#7327)) ([d8233d9](axios/axios@d8233d9))
- unclear error message is thrown when specifying an empty proxy authorization ([#&#8203;6314](axios/axios#6314)) ([6ef867e](axios/axios@6ef867e))

##### Features

- add `undefined` as a value in AxiosRequestConfig ([#&#8203;5560](axios/axios#5560)) ([095033c](axios/axios@095033c))
- add automatic minor and patch upgrades to dependabot ([#&#8203;6053](axios/axios#6053)) ([65a7584](axios/axios@65a7584))
- add Node.js coverage script using c8 (closes [#&#8203;7289](axios/axios#7289)) ([#&#8203;7294](axios/axios#7294)) ([ec9d94e](axios/axios@ec9d94e))
- added copilot instructions ([3f83143](axios/axios@3f83143))
- compatibility with frozen prototypes ([#&#8203;6265](axios/axios#6265)) ([860e033](axios/axios@860e033))
- enhance pipeFileToResponse with error handling ([#&#8203;7169](axios/axios#7169)) ([88d7884](axios/axios@88d7884))
- **types:** Intellisense for string literals in a widened union ([#&#8203;6134](axios/axios#6134)) ([f73474d](axios/axios@f73474d)), closes [/github.com/microsoft/TypeScript/issues/33471#issuecomment-1376364329](https://github.com//github.com/microsoft/TypeScript/issues/33471/issues/issuecomment-1376364329)

##### Reverts

- Revert "fix: silentJSONParsing=false should throw on invalid JSON ([#&#8203;7253](axios/axios#7253)) ([#&#8203;7](https://github.com/axios/axios/issues/7)…" ([#&#8203;7298](axios/axios#7298)) ([a4230f5](axios/axios@a4230f5)), closes [#&#8203;7253](axios/axios#7253) [#&#8203;7](axios/axios#7) [#&#8203;7298](axios/axios#7298)
- **deps:** bump peter-evans/create-pull-request from 7 to 8 in the github-actions group ([#&#8203;7334](axios/axios#7334)) ([2d6ad5e](axios/axios@2d6ad5e))

##### Contributors to this release

- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/175160345?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Ashvin Tiwari](https://github.com/ashvin2005 "+1752/-4 (#&#8203;7218 #&#8203;7218 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/71729144?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Nikunj Mochi](https://github.com/mochinikunj "+940/-12 (#&#8203;7294 #&#8203;7294 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/128113546?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Anchal Singh](https://github.com/imanchalsingh "+544/-102 (#&#8203;7169 #&#8203;7185 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/4814473?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [jasonsaayman](https://github.com/jasonsaayman "+317/-73 (#&#8203;7334 #&#8203;7298 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/377911?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Julian Dax](https://github.com/brodo "+99/-120 (#&#8203;5558 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/184285082?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Akash Dhar Dubey](https://github.com/AKASHDHARDUBEY "+167/-0 (#&#8203;7287 #&#8203;7288 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/145687605?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Madhumita](https://github.com/madhumitaaa "+20/-68 (#&#8203;7198 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/24915252?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Tackoil](https://github.com/Tackoil "+80/-2 (#&#8203;6269 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/145078271?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Justin Dhillon](https://github.com/justindhillon "+41/-41 (#&#8203;6324 #&#8203;6315 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/184138832?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Rudransh](https://github.com/Rudrxxx "+71/-2 (#&#8203;7257 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/146366930?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [WuMingDao](https://github.com/WuMingDao "+36/-36 (#&#8203;7215 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/46827243?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [codenomnom](https://github.com/codenomnom "+70/-0 (#&#8203;7201 #&#8203;7201 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/189698992?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Nandan Acharya](https://github.com/Nandann018-ux "+60/-10 (#&#8203;7272 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/7225168?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Eric Dubé](https://github.com/KernelDeimos "+22/-40 (#&#8203;7042 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/915045?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Tibor Pilz](https://github.com/tiborpilz "+40/-4 (#&#8203;5551 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/23138717?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Gabriel Quaresma](https://github.com/joaoGabriel55 "+31/-4 (#&#8203;6314 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/21505?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Turadg Aleahmad](https://github.com/turadg "+23/-6 (#&#8203;6265 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/4273631?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [JohnTitor](https://github.com/kiritosan "+14/-14 (#&#8203;6155 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/39668736?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [rohit miryala](https://github.com/rohitmiryala "+22/-0 (#&#8203;7250 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/30316250?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Wilson Mun](https://github.com/wmundev "+20/-0 (#&#8203;6053 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/184506226?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [techcodie](https://github.com/techcodie "+7/-7 (#&#8203;7236 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/187598667?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Ved Vadnere](https://github.com/Archis009 "+5/-6 (#&#8203;7283 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/115612815?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [svihpinc](https://github.com/svihpinc "+5/-3 (#&#8203;6134 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/123884782?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [SANDESH LENDVE](https://github.com/mrsandy1965 "+3/-3 (#&#8203;7246 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/12529395?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Lubos](https://github.com/mrlubos "+5/-1 (#&#8203;7312 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/709451?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Jarred Sumner](https://github.com/Jarred-Sumner "+5/-1 (#&#8203;5754 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/17907922?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Adam Hines](https://github.com/thebanjomatic "+2/-1 (#&#8203;5756 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/177472603?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Subhan Kumar Rai](https://github.com/Subhan030 "+2/-1 (#&#8203;7256 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/6473925?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Joseph Frazier](https://github.com/josephfrazier "+1/-1 (#&#8203;7311 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/184906930?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [KT0803](https://github.com/KT0803 "+0/-2 (#&#8203;7229 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/6703955?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Albie](https://github.com/AlbertoSadoc "+1/-1 (#&#8203;5560 )")
- <img src="proxy.php?url=https://avatars.githubusercontent.com/u/9452325?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Jake Hayes](https://github.com/thejayhaykid "+1/-0 (#&#8203;5999 )")

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0Mi45NC4zIiwidXBkYXRlZEluVmVyIjoiNDIuOTQuMyIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: Renovate Bot <[email protected]>
Co-authored-by: ChubbChubbs <[email protected]>
Reviewed-on: https://git.boringstudio.by/BoringStudio/mcp-gitea/pulls/18
Co-authored-by: boring-bot <[email protected]>
Co-committed-by: boring-bot <[email protected]>
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

Successfully merging this pull request may close these issues.

4 participants