axios icon indicating copy to clipboard operation
axios copied to clipboard

[ERR_PACKAGE_PATH_NOT_EXPORTED] when trying to import isAbsoluteURL

Open daniil4udo opened this issue 3 years ago • 4 comments

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]

daniil4udo avatar Oct 09 '22 07:10 daniil4udo

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]

josephh avatar Oct 16 '22 15:10 josephh

@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]

bt95 avatar Oct 17 '22 12:10 bt95

@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

daniil4udo avatar Oct 17 '22 12:10 daniil4udo

@daniil4udo Perfect! Thanks for info!

bt95 avatar Oct 17 '22 12:10 bt95

@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 avatar Oct 25 '22 19:10 msalimbene

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

msalimbene avatar Oct 25 '22 19:10 msalimbene

@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

daniil4udo avatar Oct 26 '22 09:10 daniil4udo

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

KaKi87 avatar Oct 26 '22 16:10 KaKi87

Related issues:

  • #5189
  • #5183

daniil4udo avatar Nov 03 '22 09:11 daniil4udo

Related to https://github.com/axios/axios/issues/5262

Doc999tor avatar Nov 13 '22 22:11 Doc999tor

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

jasonsaayman avatar Nov 14 '22 08:11 jasonsaayman

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

KaKi87 avatar Nov 14 '22 09:11 KaKi87

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?

twistedpair avatar Jan 31 '23 21:01 twistedpair

I have the same problem...

jhvissotto avatar Feb 14 '23 23:02 jhvissotto

Same issue. Any update?

toan-le-codeleap avatar Feb 26 '23 00:02 toan-le-codeleap

same issue here

snwfdhmp avatar Apr 12 '23 23:04 snwfdhmp

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: https://github.com/Gerhut/axios-debug-log/commit/df0f23a8c2258c4764227ea93dcd7951a651d90a#diff-e727e4bdf3657fd1d798edcd6b099d6e092f8573cba266154583a746bba0f346R10. Hopefully this is helpful to someone else!

rrogowski avatar May 18 '23 15:05 rrogowski

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.

lsickert avatar May 22 '23 13:05 lsickert

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.

DiZiNnEs avatar Dec 15 '23 09:12 DiZiNnEs

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

lahrachmohamed avatar Dec 27 '23 17:12 lahrachmohamed

Any updates on this issue?

Mifrill avatar Jan 02 '24 21:01 Mifrill

WTAF

F1r3Hydr4nt avatar Jan 17 '24 04:01 F1r3Hydr4nt

Try this, it work for me

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

suhaotian avatar Jan 26 '24 14:01 suhaotian

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';

DigitalBrainJS avatar Jan 26 '24 15:01 DigitalBrainJS

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 ?

KaKi87 avatar Jan 26 '24 16:01 KaKi87

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;

DigitalBrainJS avatar Jan 26 '24 17:01 DigitalBrainJS

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++

Mifrill avatar Jan 26 '24 19:01 Mifrill

It's worked @Mifrill

image

Demo code (js version): https://codesandbox.io/p/sandbox/new-wildflower-m3znw4?file=%2Fsrc%2FApp.js%3A10%2C11

TS version: https://codesandbox.io/p/sandbox/lucid-cloud-forked-4fhnzx?file=%2Fsrc%2FApp.tsx%3A12%2C5

suhaotian avatar Jan 27 '24 07:01 suhaotian

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

Mifrill avatar Jan 31 '24 12:01 Mifrill

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/[email protected]/lib/helpers/buildURL.js')

Thanks

KaKi87 avatar Feb 11 '24 04:02 KaKi87