Skip to content

require not supported after upgrading to axios 1.1.2 #5091

Closed

Description

@atnlantics

we are getting below error after upgrading axios to the 1.1.2 version

2022-10-11T04:56:57.575682812Z stderr F Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /app/node_modules/axios/package.json.

  |   | 2022-10-11T04:56:57.575671912Z stderr F require() of /app/node_modules/axios/index.js from /app/server.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
  |   | 2022-10-11T04:56:57.575663912Z stderr F require() of ES modules is not supported.
  |   | 2022-10-11T04:56:57.575654512Z stderr F Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /app/node_modules/axios/index.js
  |   | 2022-10-11T04:56:57.575647612Z stderr F
  |   | 2022-10-11T04:56:57.575640211Z stderr F ^
  |   | 2022-10-11T04:56:57.575628111Z stderr F throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
  |   | 2022-10-11T04:56:57.575556611Z stderr F internal/modules/cjs/loader.js:1172

  • Axios Version 1.1.2

  • Node.js Version 12

Activity

  1. everlectric-technology commented on Oct 16, 2022

    @everlectric-technology

    We have the same issue.

  2. jasonsaayman commented on Oct 17, 2022

    @jasonsaayman
    Member

    Can you please send a snippet of the import in app/server.js

  3. amercier commented on Oct 18, 2022

    @amercier

    Hello 👋 a similar issue happens to me on a simple Nuxt 2 project.

    Must use import to load ES Module: /sandbox/node_modules/axios/index.js require() of ES modules is not supported. require() of /sandbox/node_modules/axios/index.js from /sandbox/node_modules/vue-server-renderer/build.dev.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /sandbox/node_modules/axios/package.json.

    Could be related: #5150

  4. sts-ryan-holton commented on Oct 22, 2022

    @sts-ryan-holton

    @amercier Did you find a temporary workaround to this problem? I'm getting the same issue today when trying to run npm run generate in my Nuxt 2 project.

  5. yanheng888 commented on Oct 23, 2022

    @yanheng888

    I solve this problem by using the command: npm i [email protected]

    I think this problem is because the latest axios does not support require()

  6. amercier commented on Oct 25, 2022

    @amercier

    Did you find a temporary workaround to this problem? I'm getting the same issue today when trying to run npm run generate in my Nuxt 2 project.

    @sts-ryan-holton: No, no workaround for version 1.x so far I'm afraid.

    I solve this problem by using the command: npm i [email protected]
    I think this problem is because the latest axios does not support require()

    @yanheng888: Yes, also I can confirm 0.27.2 works great as well, I'd recommend using the latest 0.x until this is fixed.

  7. AlexBosio95 commented on Oct 29, 2022

    @AlexBosio95

    I solve this problem by using the command: npm i [email protected]

    I think this problem is because the latest axios does not support require()

    Thanks !

  8. lucas55i commented on Dec 23, 2022

    @lucas55i

    @amercier Thank you very much, it helped me a lot!

  9. sabaesm1377 commented on Jan 1, 2023

    @sabaesm1377

    thanks.it was helpful for me to,but i want know the reason?

    and how it can be fixed?

  10. itanglo commented on Jan 18, 2023

    @itanglo

    Thanks @DigitalBrainJS @jasonsaayman

    It has fixed using require.

    The problem I'm having is that my project uses the github.com/standard-things/esm package.

    When I try and run the module using node -r esm index.js I get this error:

    Error [ERR_REQUIRE_ESM]: require() of ES Module .../test-axios/node_modules/axios/index.js not supported.
    Instead change the require of index.js in null to a dynamic import() which is available in all CommonJS modules.
    

    Using:

    The index.js file only contains:

    import axios from 'axios';
    console.log(axios);
    

    I have asked a question here too: https://stackoverflow.com/questions/75152442/error-err-require-esm-require-of-es-module-when-using-esm-package-with-ax

  11. alintongutierrez2007 commented on Feb 2, 2023

    @alintongutierrez2007

    i fixed this error, only change axios version in Package.json in my proyect: i had : 1.3.1 y change to 0.24.0, and run "npm i " again and work it

  12. xeoshow commented on Feb 9, 2023

    @xeoshow

    Thanks @DigitalBrainJS @jasonsaayman

    It has fixed using require.

    The problem I'm having is that my project uses the github.com/standard-things/esm package.

    When I try and run the module using node -r esm index.js I get this error:

    Error [ERR_REQUIRE_ESM]: require() of ES Module .../test-axios/node_modules/axios/index.js not supported.
    Instead change the require of index.js in null to a dynamic import() which is available in all CommonJS modules.
    

    Using:

    The index.js file only contains:

    import axios from 'axios';
    console.log(axios);
    

    I have asked a question here too: https://stackoverflow.com/questions/75152442/error-err-require-esm-require-of-es-module-when-using-esm-package-with-ax

    Hello, I met the same problem. Still a bit confused to me: How to resolve this problem? By "It has fixed using require", You mean just replacing the import by require in the source code when using axios?

    Thanks a lot.

    Best regards, Jason

  13. mohdWajid19 commented on Mar 22, 2024

    @mohdWajid19

    Hi, I too faced the similar issue even in latest v1.6.8,

    Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/Github/test-axios/node_modules/axios/index.js not supported.
    Instead change the require of index.js in null to a dynamic import() which is available in all CommonJS modules.
    

    I was using esm so switching to esm-wallaby fixed my problems.

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