question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

autoInjectable not working in Chrome Extension using Webpack

See original GitHub issue

Describe the bug TSyringe seems not to be working when used in a Chrome Extension using Webpack and ts-loader.

To Reproduce

// Database.ts
class Database {
    constructor() {}
}

export default Database;
// ConfigService.ts
import "reflect-metadata";
import { autoInjectable } from "tsyringe";
import Database from "../Database";

@autoInjectable()
class ConfigService {
    constructor(private db?: Database) {}
}

export default ConfigService;
// Background.ts
import "reflect-metadata";
import { container } from "tsyringe";
import Database from "../db/Database";
import ConfigService from "../db/services/ConfigService";

container.register<Database>(Database, { useClass: Database });

const service = new ConfigService();

Expected behavior Expected new ConfigService() to return a ConfigService instance with dependencies resolved automatically.

Actual behavior new ConfigService() throws an uncaught TypeError Class constructor ConfigService cannot be invoked without ‘new’

Version: 4.4.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11

github_iconTop GitHub Comments

3reactions
ffortiercommented, May 4, 2021

It looks like webpack is prefering the "module": "./dist/esm5/index.js" config in the tsyringe/package.json over the better option "es2015": "./dist/esm2015/index.js". The esm5 does not use the class keyword and that’s why you’re getting this error. To fix it, I added the following config in my webpack.config.js file:

{
    resolve: {
        extensions: ['.ts', '.js'],
        alias: {
            tsyringe: require.resolve('tsyringe/dist/esm2015/index.js'),
        },
    },
}
0reactions
ckifercommented, Nov 2, 2022

Not to resurrect this, but is there any way that this could be solved by the repository rather than by each user? Same issue today when running webpack but packing files for a Node runtime and using esbuild-loader. Why does it resolve to esm5 when our targets are higher?

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebPack + WebExtensions/Chrome Extension - Stack Overflow
The best way to work around this is using the extension API's message system, with the sendMessage function and onMessage event - or...
Read more >
Does Chrome Extensions Allow Webpack Bundle in ...
Hi, I'm writing an extension using React Typescript and webpack, ... 1) WebStore requires you to use minification but not obfuscation.
Read more >
chrome-extension-boilerplate-react - npm
A chrome extension boilerplate built with React 17, Webpack 5, and Webpack Dev Server 4. Latest version: 4.4.0, last published: a month ago....
Read more >
How to Build a Chrome Extension With React, TypeScript, and ...
And then I thought, “Why isn't there something like Create React App for Chrome extensions - run one command and get everything up...
Read more >
Creating a Chrome Extension with React and TypeScript
Similar to web pages, Chrome extensions are created with HTML, JavaScript and ... project using Webpack, which create-react-app is based on.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found