microsoft / tsyringe

Lightweight dependency injection container for JavaScript/TypeScript
MIT License
5.16k stars 173 forks source link

Named exports for Node.js #151

Open witrin opened 3 years ago

witrin commented 3 years ago

Problem

I'm using tsyringe in Node.js >= 12 with the ESM module loader enabled. I would like to use named exports of tsyringe but I'm not able to do so:

import { container } from 'tsyringe';
         ^^^^^^^^^
SyntaxError: The requested module 'tsyringe' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
For example:
import pkg from 'tsyringe';
const { container } = pkg;

My tsconfig.json looks like:

{
    "compilerOptions": {
        "outDir": "lib",
        "incremental": true,
        "target": "ES2019",
        "module": "ESNext",
        "strict": true,
        "noImplicitAny": false,
        "alwaysStrict": true,
        "moduleResolution": "Node",
        "baseUrl": "src",
        "types": [ "node", "global", "mocha" ],
        "esModuleInterop": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "allowSyntheticDefaultImports": true,
        "declaration": true,
        "sourceMap": true,
        "importHelpers": true
    },
    "include": [
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules"
    ]
}

Description

Having a release published as ES 6 module ("type": "module") would help.

tobiasdiez commented 1 year ago

Alternatively, add a correct externals option in package.json