Search code examples
javascripttypescriptlibrarieshelpertsconfig

How can I correctly configure my tsconfig.json so only the files necessary are required when using npm run build?


Overview

I have a helper function library that I want to import into my projects.

Question

How can I correctly configure my tsconfig.json so only the files necessary are required when using npm run build? I don't want any extra files in to be included.

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "CommonJS",
    "declaration": true,
    "checkJs": true,
    "sourceMap": true,
    "outDir": "./dist",
    "removeComments": true,
    "strict": true,
    "noImplicitAny": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "skipLibCheck": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
  },
  "include": ["src/**/*"],
  "compileOnSave": false,
  "buildOnSave": false
}

Included in package.json

  "main": "./dist/index.js",
  "types": "./dist/index.d.ts",

What's showing in node_modules when imported into a project

  1. Should src be showing??
  2. How can I remove jest.config.js?

enter image description here


Solution

  • The tsconfig.json file doesn't have anything to do with what ends up in the final npm package. Per default (more or less) everything that is contained in your project directory will also be added into your package (except the node_modules folder).

    There are two ways to control the contents of your package

    1. Create a file named .npmignore and add every file/folder you want to be excluded from your package. This works similar to the .gitignore file. See the docs for details.

    2. You can add a files array to your package.json and add all files/folders you want to be included in your package. See docs for details.

    What files need to be in your package, depends on the functionality your package provides. The test configuration probably isn't required for the final package, neither is the source ...