I've just published my first library package to NPM, and I'm trying to use it in an application project.
It's written in typescript and the project builds okay, and has been published to NPM. But then trying to use it fails as apparently it's not a valid module.
The code in the index.ts file that sets up the module export (afaik) is:
const initByClass = (widgetBindings: WidgetClassBinding[], h: any, render: any) => {
for (const widgetBinding of widgetBindings) {
setupWidget(widgetBinding, h, render);
}
};
module.exports = {
initByClass
};
And the tsconfig.json file in the library is:
{
"compilerOptions": {
"esModuleInterop": true,
"target": "ES6",
"module": "ES6",
"moduleResolution": "node",
"declaration": true,
"outDir": "./lib",
"strict": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]
}
In my application code I have added the package to my package.json (and run npm update) and am trying to import it in the app entry file with:
import { initByClass } from "widgety";
but it gives me an error:
TS2306: File '/var/app/app/node_modules/widgety/src/index.ts' is not a module.
What do I need to change, to make the code be importable into another typescript project?
In case they are of use, all of project files: https://github.com/Danack/widgety/ And the NPM package entry: https://www.npmjs.com/package/widgety
requireinstead ofimportIf you want to use ES6, useexport initByClass;orexport const initByClass = ...Better? Meh, depends on your audience. If this is to be used in Node, I'd use CommonJS. Otherwise I'd use ES6.