In my TypeScript library, I have created models and API calls for multiple web applications that share the same backend.
My goal is to export these models and functions in modules so that consumer apps can import them like this:
import { SomeType, SomeFunction } from 'myLib/myModule';
Although it currently works well, I am encountering the following TSError: TS2307: Cannot find module 'myLib/myModule' or its corresponding type declarations.
The library is built using Vite and is used in consumer apps as a .tgz file.
This is the file architecture of the library. The index.ts files in api/ and computation/ directories are exporting all the models and services contained in the subdirectories:
https://i.sstatic.net/RpCTG.png
The index.ts file located in src/ does the following:
export * from './api';
export * from './computation';
The init.ts file simply exports a function to initialize the library.
I also have the following configuration files:
{
"compilerOptions": {
"module": "es2015",
"target": "es2015",
"declaration": true,
"outDir": "./dist",
"rootDir": "./src",
"moduleResolution": "node",
"esModuleInterop": true,
},
"include": [
"src/**/*"
]
}
package.json:
{
"name": "coanda-sdk-ts",
"version": "1.0.0",
"files": ["dist"],
"main": "./dist/coanda-sdk-ts.cjs.js",
"module": "./dist/coanda-sdk-ts.es.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/coanda-sdk-ts.es.js",
"require": "./dist/coanda-sdk-ts.cjs.js",
"types": "./dist/index.d.ts"
},
"./api": {
"import": "./dist/api/index.js",
"require": "./dist/api/index.js",
"types": "./dist/api/index.d.ts"
},
"./computation": {
"import": "./dist/computation/index.js",
"require": "./dist/computation/index.js",
"types": "./dist/computation/index.d.ts"
},
"./init": {
"import": "./dist/init.js",
"require": "./dist/init.js",
"types": "./dist/init.d.ts"
}
},
"scripts": {
"build": "vite build && tsc --project tsconfig.json"
},
"dependencies": {
"axios": "^1.4.0"
},
"devDependencies": {
"@types/node": "^20.3.2",
"vite": "^4.3.9"
}
}
vite.config.js:
const path = require('path');
const { defineConfig } = require('vite');
module.exports = defineConfig({
resolve: {},
build: {
lib: {
entry: {
api: path.resolve(__dirname, 'src/api/index.ts'),
computation: path.resolve(__dirname, 'src/computation/index.ts'),
init: path.resolve(__dirname, 'src/init.ts')
},
name: 'coanda-sdk-ts',
fileName: (format) => `coanda-sdk-ts.${format}.js`,
},
rollupOptions: {},
},
});
The dist/ folder after building the library looks like this: https://i.sstatic.net/ggmzg.png
These files seem to be related to my issue, which seems unusual:
src/coanda-sdk-ts.cjs.js
src/coanda-sdk-ts.cjs2.js
src/coanda-sdk-ts.cjs3.js
src/coanda-sdk-ts.es.js
src/coanda-sdk-ts.es2.js
src/coanda-sdk-ts.es3.js
I have attempted various solutions to eliminate the TSError, but it appears that my configurations may no longer be applicable. Unfortunately, I am unable to identify the problem. The consumer app is functioning correctly and utilizing the library's functions by importing modules as follows:
import { SomeAPIFunction } from 'coanda-sdk-ts/api';
If possible, I would prefer not to make any changes to this setup. Can someone please offer assistance?