index.d.ts file not published to NPM

Viewed 2494

I created a package in Typescript and published it on NPM. I have set declaration: true in tsconfig.json of this package project. When I build this project, the TS compiler generates the .d.ts files as expected, however, these files are not published on NPM and as a result, when I install this package in some other project, I get the error, Cannot find module 'projectname-common' or its corresponding type declarations.

Here's my package.json file:

{
  "name": "@Organisation/lib",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "clean": "rm -rf ./dist",
    "build": "npm run clean && npx tsc",
    "pub": "git add . && git commit -m \"Changes\" && git push && npm version patch && npm run build && npm publish --access public"
  },
  "keywords": [],
  "author": "Author",
  "license": "ISC",
  "devDependencies": {
    //removed
  },
  "dependencies": {
    //removed
  }
}

Here's my tsconfig.json file:

{
    "compilerOptions": {
      "target": "es6",                                /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
      "module": "commonjs",                           /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
      "declaration": true,                         /* Generates corresponding '.d.ts' file. */
      "sourceMap": true,                           /* Generates corresponding '.map' file. */
      "outDir": "dist",                              /* Redirect output structure to the directory. */
      "strict": true,                                 /* Enable all strict type-checking options. */
      "noImplicitAny": true,                       /* Raise error on expressions and declarations with an implied 'any' type. */
      "moduleResolution": "node",                  /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
      "baseUrl": "./",                             /* Base directory to resolve non-absolute module names. */
      "paths": {
        "*": ["*"]
      },                                 /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
      "allowSyntheticDefaultImports": true,        /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
      "esModuleInterop": true,                        /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
      "skipLibCheck": true,                           /* Skip type checking of declaration files. */
      "forceConsistentCasingInFileNames": true        /* Disallow inconsistently-cased references to the same file. */
    },
    "include": [
      "src/**/*"
    ]
  }

Here's my .gitignore file:

lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
*.swp

pids
logs
results
tmp

# Build
public/css/main.css

# Coverage reports
coverage

# API keys and secrets
.env

# Dependency directory
node_modules
bower_components

# Editors
.vscode
.idea
*.iml

# OS metadata
.DS_Store
Thumbs.db

# Ignore built ts files
dist/**/*

# ignore yarn.lock
yarn.lock

**.log.**

Note: even though dist is included in .gitignore, the file dist/index.js is published on NPM but none of the .d.ts files are.

The directory structure is like this:

my-package/
├─ node_modules/
├─ dist/
│  ├─ models/
│  ├─ index.d.ts
│  ├─ index.js
│  ├─ index.js.map
├─ src/
│  ├─ models
│  ├─ index.ts
├─ .gitignore
├─ package.json
├─ tsconfig.json
1 Answers

You need to use files key, that way only things in the files array will be published to npm, and you don't need .npmignore

example:

  "main": "dist/cjs/index.js",
  "module": "dist/esm/untilted.esm.js",
  "unpkg": "dist/unpkg/untilted.js",
  "types": "dist/types/",
  "files": [
    "dist",
    "src"
  ],

In this example, dist directory holds all compiled files, and src holds the source code (needed for source maps). And that is all that is going to be published to npm plus some files that are always included (package.json, README, LICENCE, etc...)

Related