'TypeScript only compiles index file after publish on NPM

I made a custom NPM package, it links perfectly, but when I publish and install it to other project it said error

 error: Cannot find module './lib'

My structure:

src/
|--index.ts
|--lib.ts
tsconfig.json
package.json
...

My output:

dist/
   - index.js  <--- lib.js is missing
package.json

tsconfig.json:

{
    "compilerOptions": {
        "lib": [
            "es2019",
            "es2020.promise",
            "es2020.bigint",
            "es2020.string",
            "DOM"
        ],
        "module": "commonjs",
        "target": "es2019",
        "noImplicitAny": false,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "allowJs": true,
        "outDir": "dist",
        "rootDir": "src",
        "noEmitOnError": false,
        "resolveJsonModule": true,
        "sourceMap": true,
        "strictNullChecks": true,
        "isolatedModules": true,
    },
    "include": [
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules/",
    ]
}

My package.json:

{
  "name": "mypackage",
  "version": "1.0.2",
  "main": "dist/index.js",
  "types": "src/index.ts",
  "license": "MIT",
  "dependencies": {
    "axios": "^0.26.1",
    "crypto": "^1.0.1",
    "dotenv": "^16.0.0",
    "form-data": "^4.0.0"
  },
  "scripts": {
    "dev": "tsc --watch",
    "build": "tsc --build"
  },
  "devDependencies": {
    "@types/node": "^17.0.25",
    "typescript": "^4.6.3"
  }
}

It's okay when I compile it locally, only happens when I publish it to NPM and install it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source