I can’t seem to figure out what’s wrong with the package that I created which I’m now trying to import to my other project.
The main file of the package contains these exports (names changed):
export { Class1 } from './util/translations/class1';
export { Class2 } from './util/translations/class2';
Each file exports a class such as:
export class Class1 {
private client: Client;
constructor(config: Config) {
this.client = new Client(Config);
}
async translateText(text: string, targetLanguage: Language): Promise<string> {
return client.translate(text, targetLanguage)
}
}
My package.json has these settings:
"main": "dist/Localisation.js",
"types": "dist/Localisation.d.ts",
"files": [
"dist/**/*"
],
My tsconfig.json:
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src",
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"types": ["node", "jest"],
"typeRoots": ["./node_modules/@types", "./types"],
"baseUrl": "."
},
"include": ["src/**/*.ts"],
"exclude": [
"node_modules",
]
I run tsc
to compile the project and get the dist
folder. The main file in dist looks like this:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Language = exports.Class1 = exports.Class2 = void 0;
var class1_1 = require("./util/translations/class1");
Object.defineProperty(exports, "Class1", { enumerable: true, get: function () { return class1_1.Class1; } });
var class2_1 = require("./util/translations/class2");
Object.defineProperty(exports, "Class2", { enumerable: true, get: function () { return class2_1.Class2; } });
var languages_1 = require("./util/languages");
Object.defineProperty(exports, "Language", { enumerable: true, get: function () { return languages_1.Language; } });
//# sourceMappingURL=Localisation.js.map
I then run npm publish
to publish my package with a bumped version. I pull that version in my other project and try to import Class1
and Class2
.
import { Class1, Class2 } from "my-package";
But when I try to construct an instance of Class1, I’m getting Class1 is not a constructor
. When I console log the imported Class1
, I’m getting undefined
as if it doesn’t exist at all on the package or as if the export didn’t work.
I’ve tried so many things, including:
- changing exports in the package to
export default
- moving all my classes to the main file of the project instead of exporting them in the main file
- using
tsup
instead oftsc
A very strange thing about is that I’m also exporting an enum in the same way in that same main file (from another folder within the package) and that enum is actually available and not null, the only thing that gets exported correctly.
I’m honestly not sure what I can do or try in this case.