ide / await-lock

Mutex locks for async functions
MIT License
89 stars 12 forks source link

TypeError: AwaitLock is not a constructor #5

Closed dvaruas closed 5 years ago

dvaruas commented 5 years ago

With the new version 1.2.0 this piece of code throws the error TypeError: AwaitLock is not a constructor

const AwaitLock = require('await-lock');
const lock = new AwaitLock();

This works with 1.1.3 version.

ide commented 5 years ago

Published a backwards compatibility fix in 1.2.1. I had to manually modify the build output so there is no commit that corresponds to this version of the package. Moving forward, this will not be supported in 2.0.0.

jsoldi commented 2 years ago

Having this problem to. Is it not possible to create an instance of AwaitLock? What's the alternative?

ide commented 2 years ago

The issue is with the way the module is loaded, not the constructor. Use ESM, either natively or with tsc, Babel, SWC, etc.

Spenhouet commented 1 year ago

I'm running into the same issue. Not sure what you mean. Everything else loads fine. Not this module. How to resolve this?

import AwaitLock from 'await-lock';

....

this.mutex = new AwaitLock();

this.mutex = new AwaitLock(); ^ TypeError: AwaitLock is not a constructor at new Model (file:///home/m/scripts/model.ts:78:16) at file:///home/m/scripts/ecosystem.ts:109:14 at ModuleJob.run (node:internal/modules/esm/module_job:198:25) at async Promise.all (index 0) at async ESMLoader.import (node:internal/modules/esm/loader:385:24) at async loadESM (node:internal/process/esm_loader:88:5) at async handleMainPromise (node:internal/modules/run_main:61:12)

tsconfig.json:

"compilerOptions": {
    "module": "es2022",
    "moduleResolution": "node",
    "lib": ["es2020"],
    "target": "es2020",
    "esModuleInterop": true,
Goostavo commented 1 year ago

It's a export default module error. It can be fixed easily with adding .default

Working example:

const AwaitLock = require('await-lock').default;
let lock = new AwaitLock();