tsyringe
tsyringe copied to clipboard
Named exports for Node.js
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.
Alternatively, add a correct externals option in package.json