Skip to content

exports lib packages in package.json #5000

Closed

Description

@trim21

TLDR:

Oh, yeah I don't think we will ever export that.

#5000 (comment)

Is your feature request related to a problem? Please describe.

Yes. I'm writing a adaptor to be used in userscript like tampermonkey or Violentmonkey,
it use a xhr provided be broser extension GM.xmlHttpRequest to make cors request.

And to make it behavior like axios' xhr adaptor, I'm using some axios' helper functions directly by

import buildFullPath from "axios/lib/core/buildFullPath";
import settle from "axios/lib/core/settle";
import buildURL from "axios/lib/helpers/buildURL";
import parseHeaders from "axios/lib/helpers/parseHeaders";
import utils from "axios/lib/utils";

But after axios 1.0.0 is released, a exports field is added to package.json, and lib directory is no longer exported anymore, this make me unable to use helper function provided by axios anymore.

axios/package.json

Lines 6 to 17 in 484aa4f

"exports": {
".": {
"browser": {
"require": "./index.js",
"default": "./index.js"
},
"default": {
"require": "./dist/node/axios.cjs",
"default": "./index.js"
}
}
},

modern js bundler will raise a error for this: Package subpath './lib/core/buildFullPath' is not defined by "exports" in .../node_modules/axios/package.json by bundler.

Describe the solution you'd like

add lib dir to package.json#exports

Activity

  1. arthurfiorette commented on Oct 5, 2022

    @arthurfiorette
    Contributor

    And, if I'm not mistaken, react native users needs the package.json file to be exported as well.

    Some users of axios-cache-interceptor raised an issue about this same problem and, after some reading, this may also be the best way to deascribe your exports in a project coded by default with ESM:

    https://github.com/arthurfiorette/axios-cache-interceptor/blob/4bf049b935d47f5fc46c587ff81c0e4d15b52e8b/package.json#L9-L20

  2. arthurfiorette commented on Oct 5, 2022

    @arthurfiorette
    Contributor

    Also, it would be great if a simple export * from '../index.d.ts declaration file was shipped with the same name for each file in the dist folder, this way direct imports would work flawlessly with typescript.

    Like: import ... from 'axios/dist/node/axios.cjs'

  3. jasonsaayman commented on Oct 6, 2022

    @jasonsaayman
    Member

    See #5030 merged should fix this.

  4. trim21 commented on Oct 6, 2022

    @trim21
    ContributorAuthor

    No, this is not fixed by #5030, it's a complete different problem.

    People who are affected by this issue want to import utils from axios/lib/..., which is curretly forbidden by package.json#exports.

    Currently package.json#exports only allow us to import 'axios', but we are not just importing axios, we are importing axios/lib/..., and change exports of axios won't help.

    The other issues closed by this #5030 are indeed importing from axios, they are fixed by #5030, but not this one.

  5. trim21 commented on Oct 6, 2022

    @trim21
    ContributorAuthor

    @jasonsaayman could you re-open this issue?

  6. jasonsaayman commented on Oct 6, 2022

    @jasonsaayman
    Member

    Ok fair enough, I have re-opened and will investigate as soon as I can

  7. ivanpepelko commented on Oct 6, 2022

    @ivanpepelko
    Contributor

    No, this is not fixed by #5030, it's a complete different problem.

    People who are affected by this issue want to import utils from axios/lib/..., which is curretly forbidden by package.json#exports.

    Currently package.json#exports only allow us to import 'axios', but we are not just importing axios, we are importing axios/lib/..., and change exports of axios won't help.

    The other issues closed by this #5030 are indeed importing from axios, they are fixed by #5030, but not this one.

    This is correct, I made a mistake in when describing #5030, as this issue was mentioned in #5008 (comment).

  8. tusbar commented on Oct 13, 2022

    @tusbar
    Contributor
  9. kranzhoff commented on Nov 9, 2022

    @kranzhoff

    I think my issue is related to this:
    Trying to set the axios.defaults.adapter in my TS project's test files like this:

    axios.defaults.adapter = require('axios/lib/adapters/http')

    fails with axios 1.x due to the httpAdapter not being exported.
    This used to work just fine with axios 0.27.2.

  10. jasonsaayman commented on Nov 10, 2022

    @jasonsaayman
    Member

    Hi 👋

    Please try the latest pre-release by running the following:

    Please provide feedback in either the pinned issue or the discussion thread 🧵

  11. 9 remaining items

  12. DigitalBrainJS commented on Nov 21, 2022

    @DigitalBrainJS
    Collaborator

    @guoyunhe Manual adapter switching without the need for direct import will be provided by #5277 PR.

  13. bigmeow commented on Nov 22, 2022

    @bigmeow

    I have a solution and it wasted me a few days:
    make it not work that "exports" field in the package.json file, see the webpack doc.

    webpack.config.js

    module.exports = {
      //...
      resolve: {
        exportsFields: ['exports2'],
      },
    };

    or webpackChain

    chain.merge({
        resolve: {
          exportsFields: ["exports2"],
        }
    });
    

    It really works

    @trim21 @kranzhoff @guoyunhe

  14. arthurfiorette commented on Dec 6, 2022

    @arthurfiorette
    Contributor

    @DigitalBrainJS, Actually, this just extends the current problem into smaller ones. Everytime someone will be needing an internal function from axios that is not exported. As you can see in #5324, #5264 and so on...

    Why can't all functions be exported as normal so anyone can use it while integrating with axios? The bytesize wouldn't be that big? or would it?

  15. jasonsaayman commented on Dec 6, 2022

    @jasonsaayman
    Member

    @arthurfiorette we don't want to export everything else people will rely on code that can change without being documented as we can change underlying implementations without changing the public API. The only way I can see this working is we either export it and then whoever uses it does so explicitly at their own risk or we export only what is needed. For now I would like to do it one by one but if there are many uses cases and people really want us to do so we can look into everything being exported.

  16. damirbogdanov commented on Dec 22, 2022

    @damirbogdanov

    the following fixed the issue for me

    webpack.config.js

    module.exports = {
      resolve: {
        alias: {
          "axios/lib": path.resolve(__dirname, "../../node_modules/axios/lib"),
        },
      },
    };
  17. Liubasara commented on Apr 13, 2023

    @Liubasara

    any news? same problem here.

  18. arthurfiorette commented on Apr 22, 2023

    @arthurfiorette
    Contributor

    Hey @jasonsaayman. I know that importing code inside lib folder that are not part of the public API is strongly discouraged and it may break even in patch versions.

    The main reason we import things inside the lib/related folders is because we are trying to do something that axios does not support yet or reusing axios's logic in another part of our codebase.

    The code is in OUR node_modules and just disallowing us from accessing it WILL not prevent us from using it. It is going to just create another bigger problem as, to solve our problem, we will copy/paste axios source code, which is much worse than our code breaking when upgrading axios, because it creates duplicity anyway and even future critical problems axios may fix won't reflect on our copy-pasted code.

    And even if we achieve to import axios internal content without copy-pasting functions, I am sure that the code written to access it is more error-prone than just hypothetic incompatibilities axios may bring with a patch/minor version.

    I am strongly in favor of exporting everything axios has, even if it is going to be under a internal property or similar names.

    I know that creating a issue to export X functionality will leave axios with much more control of what APIs you should care about or not, but in this issue/code/release meantime, the end-user will already have copied the function into his source code and probably will never look at it again, which leverages us to the same prior problem of axios fixing critical stuff and it not reflecting everywhere.

    Happy to discuss more, also if you want, I can create another issue.

  19. jasonsaayman commented on Apr 25, 2023

    @jasonsaayman
    Member

    Ok fair enough, I have been gone for a bit of a hiatus, but seems as good as any time to start getting stuck in again. I will look into this

  20. trim21 commented on Apr 25, 2023

    @trim21
    ContributorAuthor
  21. DigitalBrainJS commented on Apr 25, 2023

    @DigitalBrainJS
    Collaborator

    Perhaps we can export the lib folder with some prefix in the path to highlight this as an unsafe operation. I would like to avoid potential situations when we change the internal structure of Axios in a minor version, causing a bunch of issue reports. The next major version will have intensive changes to internal modules, as the core needs a deep rework in order to add middleware/hooks functionality and a plugin loader.

  22. marcin-wlodarczyk commented on Jul 12, 2023

    @marcin-wlodarczyk

    @DigitalBrainJS I'm trying to implement a custom Axios adapter.
    Looks like the path ./unsafe/core/settle.js is not recognized by TypeScript.

  23. eizodann commented on Aug 10, 2023

    @eizodann

    @DigitalBrainJS I'm trying to implement a custom Axios adapter. Looks like the path ./unsafe/core/settle.js is not recognized by TypeScript.

    Same here

  24. trim21 commented on Aug 10, 2023

    @trim21
    ContributorAuthor

    @DigitalBrainJS I'm trying to implement a custom Axios adapter. Looks like the path ./unsafe/core/settle.js is not recognized by TypeScript.

    axios internal files are plain js, so you need to write your own type defs for them.

    There is nothing axios can do unless they rewrite whole project into TypeScript.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions