Skip to content

[BUG] all commands fail: TypeError: LRU is not a constructor #6785

Closed

Description

@iambumblehead

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

npm --version and other commands print a runtime error

$ npm --version
/usr/lib/node_modules/npm/lib/es6/validate-engines.js:31
    throw err
    ^

TypeError: LRU is not a constructor
    at Object.<anonymous> (/usr/lib/node_modules/npm/node_modules/semver/classes/range.js:188:15)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at Object.<anonymous> (/usr/lib/node_modules/npm/node_modules/semver/functions/satisfies.js:1:15)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)

Expected Behavior

npm commands should not fail with a runtime error

$ npm --version
10.0.0-1

Steps To Reproduce

  1. install npm
  2. use any npm command, such as "npm --version", "npm install" or "npm test".
  3. see runtime error

Environment

  • npm: 10.0.0-1
  • Node.js: v20.6.0
  • OS Name: Arch Linux
  • System Model Name: xps13
  • npm config: (generated from the previous, working version of npm, version 9.8.1)
; "user" config from /home/bumble/.npmrc

//registry.npmjs.org/:_authToken = (protected) 

; node bin location = /usr/bin/node
; node version = v20.6.0
; npm local prefix = /home/bumble/software/esmock
; npm version = 9.8.1
; cwd = /home/bumble/software/esmock
; HOME = /home/bumble
; Run `npm config ls -l` to show all defaults.

Activity

  1. lukekarrys commented on Sep 11, 2023

    @lukekarrys
    Contributor

    Can you try npm@10.1.0 which is the latest versions? Also how did you download this version of npm? 10.0.0-1 isn't a valid version of npm in the registry:

    npm view npm --json versions
    [
      ...
      "10.0.0-pre.0",
      "10.0.0-pre.1",
      "10.0.0",
      "10.1.0"
    ]
  2. added and removed
    Needs Triageneeds review for next steps
    on Sep 11, 2023
  3. iambumblehead commented on Sep 11, 2023

    @iambumblehead
    Author

    @lukekarrys thank you and my apology for opening this issue here. This should have been opened at the arch package location instead https://gitlab.archlinux.org/archlinux/packaging/packages/npm/-/blob/main/PKGBUILD?ref_type=heads

    respectfully, I will close this issue

  4. blacklight commented on Sep 14, 2023

    @blacklight

    @iambumblehead I've been experiencing the same issue on all of my Arch installations as well after upgrading to npm 10.

    Downgrading to 9.8.1 is a temporary workaround.

    Have you already reported the issue to the Arch maintainer? I couldn't find anything here https://bugs.archlinux.org/?project=1&string=npm.

    I can open a ticket on the Arch side as well if you prefer.

  5. iambumblehead commented on Sep 14, 2023

    @iambumblehead
    Author

    @blacklight please open a ticket :) I do not have an account there and did not open a ticket

  6. blacklight commented on Sep 14, 2023

    @blacklight

    @iambumblehead ok FYI I've filed a bug report on Arch here https://bugs.archlinux.org/task/79693

  7. iambumblehead commented on Sep 14, 2023

    @iambumblehead
    Author

    thanks. toolybird's comment un-necessarily rude

  8. blacklight commented on Sep 14, 2023

    @blacklight

    @iambumblehead I've found the culprit and a solution - see comment on the Arch Linux thread.

    Long story short, the error most likely happens when you have (or had at some point) an old version of semver installed - either an old semver package or something installed via npm install -g.

    Uninstalling npm+semver, removing /usr/lib/node_modules/npm/node_modules/semver and reinstalling it should fix it.

  9. iambumblehead commented on Sep 15, 2023

    @iambumblehead
    Author

    @blacklight yes that works thank you!

    $ sudo pacman -R npm semver node-gyp
    $ sudo rm -rf /usr/lib/node_modules/npm/node_modules/semver
    $ sudo pacman -S npm semver
    $ npm --version
    10.1.0
  10. br4nnigan commented on Oct 8, 2023

    @br4nnigan

    @iambumblehead I've found the culprit and a solution - see comment on the Arch Linux thread.

    Long story short, the error most likely happens when you have (or had at some point) an old version of semver installed - either an old semver package or something installed via npm install -g.

    Uninstalling npm+semver, removing /usr/lib/node_modules/npm/node_modules/semver and reinstalling it should fix it.

    I'm curious, how tf did you find that out?

  11. antonis-tsolomitis commented on Feb 11, 2024

    @antonis-tsolomitis

    The above fix does not work now. After
    sudo pacman -Syu
    sudo pacman -R npm semver node-gyp
    rm -rf /usr/lib/node_modules/npm/node_modules/semver
    sudo pacman -S npm semver

    The command

    npm --version

    gives
    /usr/lib/node_modules/npm/lib/es6/validate-engines.js:31
    throw err
    ^

    TypeError: LRU is not a constructor
    at Object. (/usr/lib/node_modules/npm/node_modules/npm-package-arg/node_modules/hosted-git-info/index.js:6:15)
    at Module._compile (node:internal/modules/cjs/loader:1375:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1434:10)
    at Module.load (node:internal/modules/cjs/loader:1206:32)
    at Module._load (node:internal/modules/cjs/loader:1022:12)
    at Module.require (node:internal/modules/cjs/loader:1234:19)
    at require (node:internal/modules/helpers:176:18)
    at Object. (/usr/lib/node_modules/npm/node_modules/npm-package-arg/lib/npa.js:8:19)
    at Module._compile (node:internal/modules/cjs/loader:1375:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1434:10)

    Node.js v21.5.0

  12. ljharb commented on Feb 11, 2024

    @ljharb
    Contributor

    Why would you install an npm package via pacman? use npm.

  13. blacklight commented on Feb 11, 2024

    @blacklight

    Why would you install an npm package via pacman? use npm.

    Installing packages via npm install -g is exactly what seems to cause the issue in these cases.

    When npm is upgraded via pacman it also upgrades anything under /usr/lib/node_modules, if those packages were installed via pacman too.

    If they were installed via npm instead, then those old dependencies are likely to stay there. If they are incompatible with the new version of npm, then you'll have this kind of errors.

    As a general advice, global packages should always be installed through the system package manager, if they are available. npm install -g is likely to break system dependencies just like sudo pip install.

    The above fix does not work now

    If the error doesn't come from gyp/semver now then it may come from another dependency installed via npm install -g.

    My way of pinpointing the culprit usually works, but it's a bit cumbersome - not sure if there's a smarter way of telling which dependencies weren't installed via pacman.

    1. Backup your /usr/lib/node_modules and /usr/local/lib/node_modules folders and remove them
    2. Reinstall node+npm
    3. Verify that the error no longer occurs
    4. Reinstall all the dependencies in your previous node_modules via pacman. Or try and move the modules folders back one by one to test which one causes the error
  14. antonis-tsolomitis commented on Feb 11, 2024

    @antonis-tsolomitis

    I backed up each of the above folder and removed it.
    pacman -R npm semver node-gyp

    then

    pacman -S npm

    accepted the semver and node-gyp dependencies and all installed.

    Now npm --version fails again, now with:

    /usr/lib/node_modules/npm/lib/es6/validate-engines.js:31
    throw err
    ^

    Error: Cannot find module 'nopt'
    Require stack:

    • /usr/lib/node_modules/npm/node_modules/@npmcli/config/lib/index.js
    • /usr/lib/node_modules/npm/lib/npm.js
    • /usr/lib/node_modules/npm/lib/cli-entry.js
    • /usr/lib/node_modules/npm/lib/cli.js
    • /usr/lib/node_modules/npm/bin/npm-cli.js
      at Module._resolveFilename (node:internal/modules/cjs/loader:1143:15)
      at Module._load (node:internal/modules/cjs/loader:984:27)
      at Module.require (node:internal/modules/cjs/loader:1234:19)
      at require (node:internal/modules/helpers:176:18)
      at Object. (/usr/lib/node_modules/npm/node_modules/@npmcli/config/lib/index.js:4:14)
      at Module._compile (node:internal/modules/cjs/loader:1375:14)
      at Module._extensions..js (node:internal/modules/cjs/loader:1434:10)
      at Module.load (node:internal/modules/cjs/loader:1206:32)
      at Module._load (node:internal/modules/cjs/loader:1022:12)
      at Module.require (node:internal/modules/cjs/loader:1234:19) {
      code: 'MODULE_NOT_FOUND',
      requireStack: [
      '/usr/lib/node_modules/npm/node_modules/@npmcli/config/lib/index.js',
      '/usr/lib/node_modules/npm/lib/npm.js',
      '/usr/lib/node_modules/npm/lib/cli-entry.js',
      '/usr/lib/node_modules/npm/lib/cli.js',
      '/usr/lib/node_modules/npm/bin/npm-cli.js'
      ]
      }

    Node.js v21.5.0

  15. antonis-tsolomitis commented on Feb 11, 2024

    @antonis-tsolomitis

    Ah, I installed nodejs-nopt and now it works! Thank you.

    The folders I had backed up are: corepack, root, vdx and node-gyp, nopt, npm, semver which where installed again by pacman. I guess these are needed... how do I install them with pacman (I mean corepack, root, vdx)?

  16. blacklight commented on Feb 11, 2024

    @blacklight

    how do I install them with pacman (I mean corepack, root, vdx)?

    It may be that they are no longer required by recent versions of node - I'm on the latest version and my node_modules doesn't seem to contain those folders any longer.

  17. antonis-tsolomitis commented on Feb 11, 2024

    @antonis-tsolomitis

    OK. Great!

  18. devsnd commented on Apr 7, 2024

    @devsnd

    I had a few other modules that did not work after having reinstalled npm and semver as recommended by @blacklight .

    I also needed to manually delete the node_modules that were part of the npm install, so to fix it on arch I did:

    $ sudo pacman -R npm semver node-gyp
    $ sudo rm -rf /usr/lib/node_modules/npm  # note that I removed the whole npm directory this time
    $ sudo pacman -S npm semver
    $ npm --version
    v21.7.2
    

    Note: This obviously nukes your globally installed node_modules, but I wanted a clean state to work off of

  19. sarthakgupta898 commented on Apr 24, 2024

    @sarthakgupta898

    What is the workaround for windows?

  20. AlonaAkimova commented on Apr 24, 2024

    @AlonaAkimova

    Hello,

    Error is in this line: https://github.com/npm/node-semver/blob/ec49cdcece9db0020d6829b246681ff65a393644/classes/range.js#L201

    It has been introduced in semver 7.3.3.

    The new version 7.6.1 is released and will solve the problem. Related MR here:
    npm/node-semver#706.

    Solution is to upgrade to 7.6.1 or downgrade to 7.3.2

    If you want to downgrade add below to your package.json:

    NPM:

    {
       "overrides": {
         "semver": "7.3.2"
       }
     }
    

    Yarn:

    "resolutions": {
        "semver": "7.3.2"
    }
    

    Hope it helps.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions