isaacs / node-lru-cache

A fast cache that automatically deletes the least recently used items
http://isaacs.github.io/node-lru-cache/
ISC License
5.35k stars 353 forks source link

TypeError: lru_cache_1.LRUCache is not a constructor #294

Closed TroyCoombs closed 1 year ago

TroyCoombs commented 1 year ago

Get this error TypeError: lru_cache_1.LRUCache is not a constructor when calling the constructor.

import { LRUCache } from 'lru-cache';
const lru_options = { max: 1000 };
const cache = new LRUCache(lru_options);

Running version 9.1.1

isaacs commented 1 year ago

Can you run npm ls lru-cache to verify?

isaacs commented 1 year ago

And if it is 9.1.1, run npm why lru-cache to verify that nothing else is getting the wrong version.

TroyCoombs commented 1 year ago

npm ls lru-cache gives this,

├─┬ @google-cloud/datastore@6.6.2
│ └─┬ google-gax@2.30.1
│   └─┬ google-auth-library@7.14.0
│     └── lru-cache@6.0.0
├─┬ @google-cloud/pubsub@3.0.1
│ └─┬ google-auth-library@8.0.2
│   └── lru-cache@6.0.0
├─┬ @google-cloud/storage@5.19.1
│ └─┬ google-auth-library@7.14.1
│   └── lru-cache@6.0.0
├─┬ @typescript-eslint/eslint-plugin@5.16.0
│ └─┬ semver@7.3.8
│   └── lru-cache@6.0.0
├─┬ google-auth-library@6.1.6
│ └── lru-cache@6.0.0
└── lru-cache@9.1.1
isaacs commented 1 year ago

If you open up node_modules/lru-cache/dist/cjs/index.js in an editor, do you see class LRUCache { on line 139, and exports.LRUCache = LRUCache; on line 1394?

If you open up node_modules/lru-cache/dist/mjs/index.js in an editor, do you see export class LRUCache { on line 136?

isaacs commented 1 year ago

Also, if you add console.log(require.resolve('lru-cache')) to your program, what does it output?

TroyCoombs commented 1 year ago

Regarding this,

If you open up node_modules/lru-cache/dist/cjs/index.js in an editor, do you see class LRUCache { on line 139, and exports.LRUCache = LRUCache; on line 1394?

If you open up node_modules/lru-cache/dist/mjs/index.js in an editor, do you see export class LRUCache { on line 136?

the answer us yes to all of it.

And regarding this,

Also, if you add console.log(require.resolve('lru-cache')) to your program, what does it output?

I get /core/server/node_modules/lru-cache/index.js

isaacs commented 1 year ago

Ok. Then the lru-cache that your program is loading is definitely not v9.1.1.

I'm not sure how you're building the deps for your server, but what it's getting is an old version.

seranus commented 1 year ago

I'm got hit with the same issue when migrating from webpack to vite with interop set to true

While debugging raw JS it seems that it's loading the proper version and it's properly initialized when for my own code.

Error seems to happen with SemVer dependency not loading an older version for it's range function range = Range2; const LRU = LRUCache; const cache2 = new LRU({ max: 1e3 });

compared to my code this.cache = new LRUCache.LRUCache(cacheOptions);

isaacs commented 1 year ago

If your package manager or rollup builder isn't providing libraries with dependencies in their stated version ranges, there's not much I can do about that as the library author. Sounds like a vite bug.

dceddia commented 1 year ago

I got a similar error, "LRUCache is not a constructor". The weird thing in my case was that it was working fine on my Mac, but not my Windows dev machine (mine's a Tauri app running everything in a webview so I'm building on both). I'm using Vite as well.

The comment that it might be picking the wrong version gave me the feeling that maybe it's some weird module resolution issue, so I blew everything away with rm -rf node_modules and re-ran yarn and the error went away! Pretty unsatisfying fix, but worth a shot anyway.

AlonaAkimova commented 6 months ago

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.

Solution is to downgrade to 7.3.2 or fix in node-semver :)

In order to downgrade add below to your package.json:

NPM:

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

Yarn:

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

Hope it helps.

isaacs commented 6 months ago

Just reiterating again:

This is not a lru-cache error. This is a package manager error. Please stop reporting it here. I cannot fix yarn.