I've created and published a Typescript package, an SDK for my API. I've not done this before, and used a lot of third party tools to do the heavy lifting. The end result, when installed from NPM, doesn't work as expected. If I try to import a class from @namespace/module
I get an error Cannot find module '@namespace/module' or its corresponding type declarations.
If I import it as @namespace/module/dist/types/src/index
that error goes away, but will instead get Module not found: Error: Can't resolve '@namespace/module/dist/types/src/index'
when I try to compile.
I suspect I have something in the wrong location, or am missing a reference, but I cannot discover what that might be, as the third party tools picked those locations. Specifically, tomchen/example-typescript-package
, but I also have a module that's generating dynamic TypeScript based on my API.
Node 18.18
Googling shows result for people having trouble installing correctly built modules, I'm assuming the error is in the module itself. As I'm not really sure what exactly is wrong, I've not had success finding any solution. My lack of experience creating modules is limiting me here.
package.json:
{
"name": "@namespace/module",
"version": "1.0.0",
"description": "",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"umd:main": "dist/umd/index.js",
"types": "dist/types/index.d.js",
"scripts": {
"postinstall": "husky install",
"prepublishOnly": "pinst --disable",
"postpublish": "pinst --enable",
"gen-odata": "odata2ts",
"build": "npm run gen-odata && npm run build:cjs && npm run build:esm && npm run build:umd && npm run build:types",
"build:cjs": "node tools/cleanup cjs && tsc -p config/tsconfig.cjs.json",
"build:esm": "node tools/cleanup esm && tsc -p config/tsconfig.esm.json",
"build:umd": "node tools/cleanup umd && webpack --config config/webpack.config.js",
"build:types": "node tools/cleanup types && tsc -p config/tsconfig.types.json",
"clean": "node tools/cleanup",
"package": "npm run build && npm pack",
"test": "jest --no-cache --runInBand",
"test:cov": "jest --coverage --no-cache --runInBand",
"addscope": "node tools/packagejson name @namespace/module"
},
"keywords": [],
"publishConfig": {
"access": "public"
},
"files": [
"dist"
],
"author": "",
"license": "MIT",
"homepage": "",
"repository": {
"type": "git",
"url": "git@github.com:namespace/module.git"
},
"bugs": {
"url": ""
},
"devDependencies": {
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"@odata2ts/odata2ts": "^0.31.1",
"@types/jest": "^29.5.5",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^7.0.2",
"jest": "^29.7.0",
"pinst": "^2.1.6",
"prettier": "^2.4.0",
"ts-jest": "^29.1.1",
"ts-loader": "^9.2.5",
"typescript": "^4.4.3",
"webpack": "^5.89.0",
"webpack-cli": "^4.8.0"
},
"dependencies": {
"@odata2ts/http-client-fetch": "^0.6.2",
"@odata2ts/odata-service": "^0.17.1"
}
}
dist folder structure:
dist
cjs
odata2ts.config.js
src
index.js
build
namespace
QNamespace.js
NamespaceModel.js
NamespaceService.js
esm
odata2ts.config.js
src
index.js
build
namespace
QNamespace.js
NamespaceModel.js
NamespaceService.js
types
odata2ts.config.d.ts
src
index.d.ts
build
namespace
QNamespace.d.ts
NamespaceModel.d.ts
NamespaceService.d.ts
umd
index.js
Your package.json defines the below as the path to your entry points
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"umd:main": "dist/umd/index.js",
"types": "dist/types/index.d.js",
But, if you check your dist folder structure, the above paths are wrong. Below is the corrected one
"main": "dist/cjs/src/index.js",
"module": "dist/esm/src/index.js",
"umd:main": "dist/umd/index.js",
"types": "dist/types/src/index.d.ts",