basarat / basarat/typescript-collections
TypeError: ... is not a constructor when bundling with webpack 5
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 1.2k
- Forks
- 153
- PR merge metrics
- No merged PRs in 30d
Description
I am not sure if this is an error with `typescript-collections` but since I upgraded to webpack 5 this package is the only one that has issues and I wonder what's the problem here. It worked back then with webpack 4.
Consider the following code, which I also pushed to https://github.com/blu3r4y/typescript-collections-webpack so that you can directly check it out and investigate the issue.
**`src/main.ts`**
```typescript
import * as Collections from 'typescript-collections';
// the next line will produce an error
let foo: Collections.Set = new Collections.Set();
foo.add("element");
console.log(foo);
```
**`tsconfig.json`**
```json
{
"compilerOptions": {
"target": "es6",
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
}
}
```
**`webpack.config.js`**
```javascript
module.exports = {
target: "node",
entry: "./src/main.ts",
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/,
},
],
},
};
```
As long as I have `target: "node"` in webpack, which I need for other reasons, I get the following error. I assume that this has something to do with the way this package is importing modules (see https://stackoverflow.com/questions/40294870/module-exports-vs-export-default-in-node-js-and-es6). However, I have to admit that I am rather clueless about these internals and I still don't understand if my `tsconfig.json` or `webpack.config.js` is wrong, or this package requires a different importing strategy?
Could you help me resolve getting to run `typescript-collections` with the above configuration, while still using `target: "node"`?
```
TypeError: n(...).Set is not a constructor
at C:\Users\mario\Desktop\typscript-collections-webpack\dist\main.js:1:30316
at C:\Users\mario\Desktop\typscript-collections-webpack\dist\main.js:1:30365
at Object. (C:\Users\mario\Desktop\typscript-collections-webpack\dist\main.js:1:30369)
at Module._compile (internal/modules/cjs/loader.js:1200:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
```
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the failure using src/main.ts, tsconfig.json, and webpack.config.js from the report or the linked reproduction repository. Start by bundling with webpack 5 while keeping target: "node", then run dist/main.js and inspect the generated module handling around the reported constructor error. Done means the example runs successfully with that target and configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript, webpack
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100