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

[ERR_PACKAGE_PATH_NOT_EXPORTED] when trying to import anything from lib/helpers #5072

Closed
daniil4udo opened this issue Oct 9, 2022 · 33 comments

Comments

@daniil4udo
Copy link

daniil4udo commented Oct 9, 2022

Trying to import helper into my project:

import isAbsoluteURL from 'axios/lib/helpers/isAbsoluteURL'

Getting:

Module not found: Error: Package path ./lib/helpers/isAbsoluteURL is not exported from package /Users/.../node_modules/axios (see exports field in /Users/.../node_modules/axios/package.json)

Expected behavior

Was able to import the helper (used to work in 0.27)

Environment

  • Axios Version [1.1.2]
  • Node.js Version [16]
@josephh
Copy link

josephh commented Oct 16, 2022

Seeing similar with node.js ES6 modules, e.g.

import axios from 'axios'
import config from 'axios-debug-log'
...

Behaviour

At runtime,

...
"trace":["Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/helpers/isAbsoluteURL' is not defined by \"exports\" in /var/task/node_modules/axios/package.json","    at new NodeError (node:internal/errors:387:5)","    at throwExportsNotFound (node:internal/modules/esm/resolve:464:9)","    at packageExportsResolve (node:internal/modules/esm/resolve:748:3)","    at resolveExports (node:internal/modules/cjs/loader:493:36)","    at Function.Module._findPath (node:internal/modules/cjs/loader:533:31)","    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:942:27)","    at Function.Module._load (node:internal/modules/cjs/loader:804:27)","    at Module.require (node:internal/modules/cjs/loader:1028:19)","    at require (node:internal/modules/cjs/helpers:102:18)","    at Object.<anonymous> (/var/task/node_modules/axios-debug-log/index.js:4:21)"]}
...

Environment

Axios Version [1.1.2]
Node.js Version [16]

@bt95
Copy link

bt95 commented Oct 17, 2022

@daniil4udo @josephh Does 1.1.3 work for you? I still get the same error with 1.1.3 as with 1.0.0 and 1.1.2.

Trying to import helper into my project:

import isAbsoluteURL from 'axios/lib/helpers/isAbsoluteURL'

Getting:

Module not found: Error: Package path ./lib/helpers/isAbsoluteURL is not exported from package /Users/.../node_modules/axios (see exports field in /Users/.../node_modules/axios/package.json)

Expected behavior

Was able to import the helper (used to work in 0.27.1)

Environment

Axios Version [1.1.3]
Node.js Version [18.8]

@daniil4udo
Copy link
Author

daniil4udo commented Oct 17, 2022

@bt95 no, lib folder (or at least helpers) has to be added to the exports in the package.json. I believe I saw PR #5136 for this issue, hopefully will be merged and released soon

@bt95
Copy link

bt95 commented Oct 17, 2022

@daniil4udo Perfect! Thanks for info!

@msalimbene
Copy link

@daniil4udo experiencing the same problem with version 1.1.3. Used to work in 0.27.1. Have you been able to solve this?

@msalimbene
Copy link

On a second look isn't exactly the same err, should I open a new ticket:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/defaults' is not defined by "exports" in /Users/salimbene/dev/hlp-bc-contracts-api/node_modules/axios/package.json
[0] at new NodeError (node:internal/errors:372:5)
[0] at throwExportsNotFound (node:internal/modules/esm/resolve:472:9)
[0] at packageExportsResolve (node:internal/modules/esm/resolve:753:3)
[0] at resolveExports (node:internal/modules/cjs/loader:482:36)
[0] at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
[0] at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
[0] at Function.Module._load (node:internal/modules/cjs/loader:778:27)
[0] at Module.require (node:internal/modules/cjs/loader:1005:19)
[0] at require (node:internal/modules/cjs/helpers:102:18)
[0] at Object. (/Users/salimbene/dev/hlp-bc-contracts-api/node_modules/axios-cookiejar-support/lib/index.js:8:40) {
[0] code: 'ERR_PACKAGE_PATH_NOT_EXPORTED

@daniil4udo
Copy link
Author

@msalimbene the origin of the problem is the same. the difference is that you're trying to import all helpers, where I need just isAbsoluteUrl.

If all helpers will be added to the exports in the package.json, it solves both our problems

@KaKi87
Copy link

KaKi87 commented Oct 26, 2022

Hello,

I'm experiencing the same issue as I wrote an Axios adapter for Tauri inspired by Axios' official adapters, involving several imports under the lib directory, working on v0.x but not v1.x :

import buildURL from 'axios/lib/helpers/buildURL';
import buildFullPath from 'axios/lib/core/buildFullPath';
import {
    isFormData,
    isArrayBuffer,
    isString,
    isBlob
} from 'axios/lib/utils';
import AxiosError from 'axios/lib/core/AxiosError';
import { transitional } from 'axios/lib/defaults';
import settle from 'axios/lib/core/settle';

Thanks

@daniil4udo daniil4udo changed the title [ERR_PACKAGE_PATH_NOT_EXPORTED] when trying to import isAbsoluteURL [ERR_PACKAGE_PATH_NOT_EXPORTED] when trying to import anything from lib/helpers Nov 3, 2022
@Doc999tor
Copy link

Related to #5262

@jasonsaayman
Copy link
Member

jasonsaayman commented Nov 14, 2022

I will look at what we can export but I must warn everyone we would break anything not currently exported without any warning

@KaKi87
Copy link

KaKi87 commented Nov 14, 2022

So there will be no documentation nor support ever regarding community adapters ?

If not, then how can we learn to make good stuff ?
(Cf. #5204)

Thanks

@twistedpair
Copy link

Any update here?
I've upgraded to the latest, and we're seeing the same thing breaking all our builds.

From reading this thread, sounds like there are many issues in the same area. Any fix or work around in site?

@jhvissotto
Copy link

I have the same problem...

@toan-le-codeleap
Copy link

Same issue. Any update?

@snwfdhmp
Copy link

same issue here

@rrogowski
Copy link

I ran into the same issue. We were using the helper libraries to build a full URL. I noticed that the axios-debug-log library encountered a similar issue nearly identical to ours, and this was the fix they implemented: Gerhut/axios-debug-log@df0f23a#diff-e727e4bdf3657fd1d798edcd6b099d6e092f8573cba266154583a746bba0f346R10. Hopefully this is helpful to someone else!

@lsickert
Copy link

Just want to add my two cents to this. After struggling to find the reason for this error within my own code, I finally noticed that one of my other dependencies was using an old version of axios-cookiejar-support that was throwing this error. Updating that package fixed it for me.

@DiZiNnEs
Copy link

I updated the axios-cookiejar-support package but it didn't help me

Just want to add my two cents to this. After struggling to find the reason for this error within my own code, I finally noticed that one of my other dependencies was using an old version of axios-cookiejar-support that was throwing this error. Updating that package fixed it for me.

@lahrachmohamed
Copy link

maybe the solution is to install a previous version of the package you currently use

@Mifrill
Copy link

Mifrill commented Jan 2, 2024

Any updates on this issue?

@F1r3Hydr4nt
Copy link

WTAF

@suhaotian
Copy link

Try this, it work for me

// @ts-ignore
import isAbsoluteURL from 'axios/lib/helpers/isAbsoluteURL.js'

@DigitalBrainJS
Copy link
Collaborator

The lib folder is exported as unsafe to highlight this is at your risk, since you will import private modules where the semver stability of the interfaces is not guaranteed.

import isAbsoluteURL from 'axios/unsafe/helpers/isAbsoluteURL.js';

@KaKi87
Copy link

KaKi87 commented Jan 26, 2024

The lib folder is exported as unsafe

Starting from which version ?

this is at your risk

Well, what's a non-risky way to create adapters that the maintainers won't add inside Axios ?

@DigitalBrainJS
Copy link
Collaborator

Starting from which version ?

v1.4.0 (#5677)

Well, what's a non-risky way to create adapters that the maintainers won't add inside Axios ?

There are no good enough solutions for now. This is one of the reasons why we need to thoroughly rework the core before adding any new major things because the larger the code base, the more we will have to rewrite and refactor later. In the new codebase, these helpers will be static methods of the AxiosURL (extends URL) class, which are supposed to be public export.

import {forEach, toCamelCase, merge} from "#utils";
import {paramsToString, deserializeParams} from "#helpers";
import platform from '#platform';
import {URLEncodedFormSerializer} from "./Serializers.js";

const defaultOriginURL = new URL(platform.origin);
const defaultOrigin = defaultOriginURL.toString();

class AxiosURL extends URL{
  constructor(url, base) {
    super(url, base || defaultOrigin);
  }

  /**
   * Determines whether the specified URL is absolute
   *
   * @param {string} url The URL to test
   *
   * @returns {boolean} True if the specified URL is absolute, otherwise false
   */
  static isAbsoluteURL(url) {
    // A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
    // RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
    // by any combination of letters, digits, plus, period, or hyphen.
    // scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
    // https://datatracker.ietf.org/doc/html/rfc3986#section-4.3
    return /^(([a-z][-a-z\d+.]*:\/?\/?)|(\/\/))/i.test(url);
  }

  isSameOriginURL(origin) {
    origin = origin ? new URL(origin) : defaultOriginURL;

    return (
      this.protocol === origin.protocol &&
      this.host === origin.host &&
      (platform.isMSIE || this.port === origin.port)
    );
  }

  /**
   * Build a URL by appending params to the end
   *
   * @param {string} url The base of the url (e.g., http://www.google.com)
   * @param {?object} [params] The params to be appended
   * @param {?object} [options]
   *
   * @returns {AxiosURL} The formatted url
   */
  static buildURL(url, params, options) {
    const urlObj = new this(url);
    const {search} = urlObj;
    const pairs = [];

    const existedParams = search && deserializeParams(search);

    if (existedParams) {
      params = merge.call({mergeArrays: true}, existedParams, params);
    }

    console.log(existedParams);
    console.log(params);

    params && new URLEncodedFormSerializer(options).serialize(params, {
      append(key, value) {
        pairs.push([key, value]);
      }
    });

    params && (urlObj.search = paramsToString(pairs, options && options.encoder || null));

    return urlObj;
  }

  /**
   * Creates a new URL by combining the specified URLs
   *
   * @param {string} baseURL The base URL
   * @param {string} relativeURL The relative URL
   *
   * @returns {AxiosURL} The combined URL
   */
  static combineURLs(baseURL, relativeURL) {
    return new this(
      String(relativeURL).replace(/^\/+/, ''),
      baseURL ? String(baseURL).replace(/\/?\/$/, '') + '/' : undefined
    );
  }
}

const {prototype} = AxiosURL;

/*defineConstants(prototype, {
  ORIGIN: platform.origin
});*/


forEach('href origin protocol username password host hostname port pathname search hash', (key) => {
  key !== 'constructor' && Object.defineProperty(prototype, toCamelCase('set ' + key), {
    value(newValue) {
      this[key] = newValue;
      return this;
    },
    configurable: true
  })
});

export default AxiosURL;

@Mifrill
Copy link

Mifrill commented Jan 26, 2024

I'm on version 1.6.4 and the import literally not working regardless of the unsafe path:

  • import isAbsoluteURL from 'axios/lib/helpers/isAbsoluteURL'
  • import isAbsoluteURL from 'axios/lib/helpers/isAbsoluteURL.js'
  • import isAbsoluteURL from 'axios/unsafe/helpers/isAbsoluteURL'
  • import isAbsoluteURL from 'axios/unsafe/helpers/isAbsoluteURL.js'

non of those not working after upgrade axios from 0.25.0 to 1.6.0++

@suhaotian
Copy link

suhaotian commented Jan 27, 2024

@Mifrill
Copy link

Mifrill commented Jan 31, 2024

Hey @suhaotian thanks for update. It seems like my issue is about Jest config, it's described here: #5026. Although I'm not using react, the issue is persists and suggestions with moduleNameMapper or transformIgnorePatterns does not solves that unfortunately.

@KaKi87
Copy link

KaKi87 commented Feb 11, 2024

In browser though, while the following works :

await import('https://esm.sh/axios/unsafe/helpers/isAbsoluteURL.js')

This one doesn't :

await import('https://esm.sh/axios/unsafe/helpers/buildURL.js')

Except when using v0.x :

await import('https://esm.sh/axios@0.27.2/lib/helpers/buildURL.js')

Thanks

@suhaotian
Copy link

Maybe migrate to xior:

import {
  isAbsoluteURL,
} from 'xior';

and support utils:

import lru from 'tiny-lru';
import {
  encodeParams,
  merge as deepMerge,
  delay as sleep,
  buildSortedURL,
  isAbsoluteURL,
} from 'xior';

@KaKi87
Copy link

KaKi87 commented Mar 4, 2024

No adapters, which is what this issue is about.

@jamesryan-dev
Copy link

+1 experiencing this issue

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 a pull request may close this issue.