Skip to content

Can't run npm on node 18.12.1 on Linux #45329

Description

@vvvbbbcz

Version

18.12.1 (the archive downloaded directly from site)

Platform

Linux 5.15.0-52-generic #58-Ubuntu SMP Thu Oct 13 08:03:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

npm

What steps will reproduce the bug?

Only run npm.

How often does it reproduce? Is there a required condition?

Every time.

What is the expected behavior?

node:internal/modules/cjs/loader:998
  throw err;
  ^

Error: Cannot find module '../lib/cli.js'
Require stack:
- /home/misaka/Libs/node-v18.12.1-linux-x64/bin/npm
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/home/misaka/Libs/node-v18.12.1-linux-x64/bin/npm:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/home/misaka/Libs/node-v18.12.1-linux-x64/bin/npm' ]
}

Node.js v18.12.1

What do you see instead?

node:internal/modules/cjs/loader:998
  throw err;
  ^

Error: Cannot find module '../lib/cli.js'
Require stack:
- /home/misaka/Libs/node-v18.12.1-linux-x64/bin/npm
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/home/misaka/Libs/node-v18.12.1-linux-x64/bin/npm:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/home/misaka/Libs/node-v18.12.1-linux-x64/bin/npm' ]
}

Node.js v18.12.1

Additional information

When I edit node-v18.12.1-linux-x64/bin/npm from:

#!/usr/bin/env node
require('../lib/cli.js')(process)

To:

#!/usr/bin/env node
require('../lib/node_modules/npm/lib/cli.js')(process)

The problem will be solved. But I don't how to make contribution, because I can't understand your source code.

Activity

  1. MrJithil commented on Nov 6, 2022

    @MrJithil
    Member

    Can we try rm -rf node_modules/ && npm I and please paste the results for better debugging.

    Also, is it possible to make a test code snippet?

  2. MegaMraz commented on Nov 6, 2022

    @MegaMraz

    Got this too. Such issues do not occur on nvm or nodesource, only with archive downloaded directly from site.
    Devs must be joking. I simply download archive, untar it in /opt/, set PATH to /opt/node/bin.

    node --version is working

    npm only works with change above

    npx also needs a patch:

    - const cli = require('../lib/cli.js')
    + const cli = require('../lib/node_modules/npm/lib/cli.js')
    ...
    - process.argv[1] = require.resolve('./npm-cli.js')
    + process.argv[1] = require.resolve('../lib/node_modules/npm/bin/npm-cli.js')
    ...
    - const { definitions, shorthands } = require('../lib/utils/config/index.js')
    + const { definitions, shorthands } = require('../lib/node_modules/npm/lib/utils/config/index.js')
  3. MegaMraz commented on Nov 7, 2022

    @MegaMraz

    Sorry, I am dumb fox, looks like false alert

    When I leave node binary archive in /home and rename it to node19 it works just fine. This is what I have in .bashrc:

    export NODEJS_HOME=/home/megamraz/Apps/node19/bin
    export PATH=$NODEJS_HOME:$PATH
    

    having it in /opt with root owner and 755 produces issues above. How should I set permissions correctly?

  4. IFirstYou commented on Nov 8, 2022

    @IFirstYou

    Version

    18.12.1

    Platform

    Linux 5.15.0-52-generic #58-Ubuntu SMP Thu Oct 13 08:03:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

    Subsystem

    npm

    What steps will reproduce the bug?

    Only run npm.

    How often does it reproduce? Is there a required condition?

    Every time.

    What is the expected behavior?

    node:internal/modules/cjs/loader:998
      throw err;
      ^
    
    Error: Cannot find module '../lib/cli.js'
    Require stack:
    - /home/misaka/Libs/node-v18.12.1-linux-x64/bin/npm
        at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
        at Module._load (node:internal/modules/cjs/loader:841:27)
        at Module.require (node:internal/modules/cjs/loader:1061:19)
        at require (node:internal/modules/cjs/helpers:103:18)
        at Object.<anonymous> (/home/misaka/Libs/node-v18.12.1-linux-x64/bin/npm:2:1)
        at Module._compile (node:internal/modules/cjs/loader:1159:14)
        at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
        at Module.load (node:internal/modules/cjs/loader:1037:32)
        at Module._load (node:internal/modules/cjs/loader:878:12)
        at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
      code: 'MODULE_NOT_FOUND',
      requireStack: [ '/home/misaka/Libs/node-v18.12.1-linux-x64/bin/npm' ]
    }
    
    Node.js v18.12.1

    What do you see instead?

    node:internal/modules/cjs/loader:998
      throw err;
      ^
    
    Error: Cannot find module '../lib/cli.js'
    Require stack:
    - /home/misaka/Libs/node-v18.12.1-linux-x64/bin/npm
        at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
        at Module._load (node:internal/modules/cjs/loader:841:27)
        at Module.require (node:internal/modules/cjs/loader:1061:19)
        at require (node:internal/modules/cjs/helpers:103:18)
        at Object.<anonymous> (/home/misaka/Libs/node-v18.12.1-linux-x64/bin/npm:2:1)
        at Module._compile (node:internal/modules/cjs/loader:1159:14)
        at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
        at Module.load (node:internal/modules/cjs/loader:1037:32)
        at Module._load (node:internal/modules/cjs/loader:878:12)
        at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
      code: 'MODULE_NOT_FOUND',
      requireStack: [ '/home/misaka/Libs/node-v18.12.1-linux-x64/bin/npm' ]
    }
    
    Node.js v18.12.1

    Additional information

    When I edit node-v18.12.1-linux-x64/bin/npm from:

    #!/usr/bin/env node
    require('../lib/cli.js')(process)

    To:

    #!/usr/bin/env node
    require('../lib/node_modules/npm/lib/cli.js')(process)

    The problem will be solved. But I don't how to make contribution, because I can't understand your source code.

    I also have this issue

  5. aaw20904 commented on Nov 14, 2022

    @aaw20904

    Hello! I`m trying to deploy nodejs app on AWS ubuntu EC2 micro instance. I have Node 18.12.1. When I start from my local folder in the remote terminal - it works well. But when I run as a demon - node throws an error where I use an 'import' statament. When I reconfiged a package.json file - the app works well

  6. BlackStar1991 commented on Nov 14, 2022

    @BlackStar1991

    I have the same problem on a PC on Windows . After update Node version. Can any fixed this problem?

  7. tuancaraballo commented on Nov 15, 2022

    @tuancaraballo

    I had the same issue running rm -rf node_modules && npm i fixed it for me.

  8. aaw20904 commented on Nov 15, 2022

    @aaw20904

    Yesterday I started to fix the problem.I found out - when Nodejs runs as a service (I used systemctl for running the one) then It doesnt work correct: [case A]: ES6 import/export isnt working - the interpreter throws an error.But require() works well))
    [case B]: When you use a private members in classes (for example - '#myPropery, #myMethod() ') - the nodejs throws an error << Error: Cannot find module 'testlib.js' >>.
    ! BUT ! When I run a script in a terminal (not as a service) - it works well with import/export and private members of a class.
    My target system - AWS EC2 micro Ubntu 20 LTS.
    I have NO idea how to fix it. Can we avoid using private members and import/export in the Nodejs when a target system is Linux?

  9. changed the title [-]Can't run npm on node 18.12.1 on Linux[/-] [+]Can't run `npm` on node 18.12.1 on Linux[/+] on Nov 19, 2022
  10. antoniomormile commented on Nov 30, 2022

    @antoniomormile

    I got a similar error when I imported bcrypt package.

    Error: Cannot find module 'bcrypt' nestjs-be-nest-1 | Require stack: nestjs-be-nest-1 | - /usr/src/app/dist/src/auth/controllers/register.controller.js nestjs-be-nest-1 | - /usr/src/app/dist/src/auth/auth.module.js nestjs-be-nest-1 | - /usr/src/app/dist/src/app.module.js nestjs-be-nest-1 | - /usr/src/app/dist/src/main.js nestjs-be-nest-1 | at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1039:15) nestjs-be-nest-1 | at Function.Module._load (node:internal/modules/cjs/loader:885:27) nestjs-be-nest-1 | at Module.require (node:internal/modules/cjs/loader:1105:19) nestjs-be-nest-1 | at require (node:internal/modules/cjs/helpers:103:18) nestjs-be-nest-1 | at Object.<anonymous> (/usr/src/app/src/auth/controllers/register.controller.ts:12:1) nestjs-be-nest-1 | at Module._compile (node:internal/modules/cjs/loader:1218:14) nestjs-be-nest-1 | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1272:10) nestjs-be-nest-1 | at Module.load (node:internal/modules/cjs/loader:1081:32) nestjs-be-nest-1 | at Function.Module._load (node:internal/modules/cjs/loader:922:12) nestjs-be-nest-1 | at Module.require (node:internal/modules/cjs/loader:1105:19)

  11. drexler commented on Dec 2, 2022

    @drexler

    I have the same problem attempting to build on Mac OS 13.01

  12. dj0024javia commented on Dec 9, 2022

    @dj0024javia

    Yup, This is happening with es build as well.
    image

    Nodejs 18.12.0

    This was not happening in older node version as i tried to rebuild the same repo that i built 6 months back and it was working fine then.

  13. devk232 commented on Dec 18, 2022

    @devk232

    I got the same error on the windows:

    node:internal/modules/cjs/loader:998
      throw err;
      ^
    
    Error: Cannot find module '../lib/cli.js'
    Require stack:
    - C:\Users\devsa\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js
        at Module._load (node:internal/modules/cjs/loader:841:27)
        at Module.require (node:internal/modules/cjs/loader:1061:19)
        at require (node:internal/modules/cjs/helpers:103:18)
        at Object.<anonymous> (C:\Users\devsa\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js:2:1)
        at Module._compile (node:internal/modules/cjs/loader:1159:14)
        at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
        at Module.load (node:internal/modules/cjs/loader:1037:32)
        at Module._load (node:internal/modules/cjs/loader:878:12)
        at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
      code: 'MODULE_NOT_FOUND',
      requireStack: [
        'C:\\Users\\devsa\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js'
      ]
    }
    
    Node.js v18.12.1
    
  14. dj0024javia commented on Dec 22, 2022

    @dj0024javia

    If anyone is trying to build using esbuild, i got error using that. Switched to aws-cdk Lambda stack which has esbuild in built, solved my issue. I guess it has something to do with the configration in the esbuild.config.js file

  15. bnoordhuis commented on Dec 23, 2022

    @bnoordhuis
    Member

    I'll close this because this ultimately looks like an npm issue and those should be reported to npm, not here. Node bundles npm but doesn't maintain it.

  16. added
    npmIssues and PRs related to the npm client dependency or the npm registry.
    wrong repoIssues that should be opened in another repository.
    on Dec 23, 2022
  17. N00bSkywalker commented on Apr 19, 2023

    @N00bSkywalker

    For anyone getting here and seeing the same error, I was also able to reproduce this, albeit accidentally. I'm using a Python script to download & extract node-v18.16.0-linux-x64.tar.xz to a temp directory ... and the contents of that temp directory are then copied to a final location via shutil.copytree(). Problem ended up being that the default for shutil.copytree() is to not preserve symbolic links ... which is why npm & npx failed to run (they're supposed to be links!).

    When downloading via wget & extracting via tar, with no interim directory or copying involved, symlinks were preserved, and npm & npx ran as expected.

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

    npmIssues and PRs related to the npm client dependency or the npm registry.wrong repoIssues that should be opened in another repository.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions