skip to Main Content

I have issue when run tsc

error TS5055: Cannot write file 'index.d.ts' because it would overwrite input file.

my tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "declaration": true,
    "newLine": "LF",
    "preserveConstEnums": true,
    "pretty": true,
    "experimentalDecorators": true
  },

  "exclude": [
    "node_modules",
    "typings"
  ]
}

This issue solved when:

  1. exclude index.ts in tsconfig
  2. run tsc index.ts
  3. Turn declaration off in tsconfig
  4. rename index to another name!

I have same issue when change typescript main file in package.json
for example: rename index.ts to foo.ts

change package.json to

"typescript": {
  "main": "foo.ts"
}

tsc error:

error TS5055: Cannot write file 'index.d.ts' because it would overwrite input file.

content of file no mater, any code content has same issue!

What can i do for fix it ?

Source code: https://github.com/AliMD/Node.js-Telegram-Bot-API/tree/v0.0.2-0
Thank you in advance.

5

Answers


  1. Chosen as BEST ANSWER

    In your example folder you wrote import * as test from '../'
    see https://github.com/AliMD/Node.js-Telegram-Bot-API/blob/v0.0.2/examples/test-server.ts#L1

    It should load your index.ts, index.d.ts, or package.json "typings" field.
    And that's the issue. Your package.json does say that index.d.ts is the definition file for this package, see https://github.com/AliMD/Node.js-Telegram-Bot-API/blob/v0.0.2/package.json#L9
    so import * as test from '../' will load package.json, load typings field and then load index.d.ts and it causes the problem.

    Two solutions

    • Import index file instead of root folder (recommended approach)
      e.g. import * as test from '../index'; or,

    • Move output to a different folder
      e.g. libindex.d.ts.

    In both solutions you should load the target file and not the folder. Since you're not loading the root folder anymore the problem will be solved.


  2. I had this very same problem, and it was super simple to fix. I deleted node_modules, then deleting the typings folder.

    in package.json I have these in

    scripts: {
        ...
        "tsc": "tsc",
        "typings": "typings"
        ...
    }
    

    then ran the commands:

    npm install
    npm run tsc
    npm run typings install
    
    Login or Signup to reply.
  3. Perahaps a better approach is to exclude the target directory from your build. The key is including the same directory in both the outDir and exclude keys:

    {
        "compilerOptions": {
            "module": "commonjs",
            "target": "es6",
            "noImplicitAny": false,
            "sourceMap": true,
            "experimentalDecorators": true,
            "outDir": "target",
            "declaration": true
        },
        "exclude": [
            "node_modules",
            "target"
        ]
    }
    
    Login or Signup to reply.
  4. Many thanks to everyone else for their answers, for me however the problem was a lot simpler (and stupider). I had let WebStorm select the best import statement for me and overlooked that it included a file from the “dist” directory (this is what I have my output set to.

    Once that cause was found the error made much more sense as it was indeed trying to write to the output file that was being used as it’s input.

    Login or Signup to reply.
  5. In my case I was missing "include": ["./src/**/*.ts"] in tsconfig.json even though I had set the following:

        "compilerOptions": {
            "outDir": "./build",
            "rootDir": "./src"
        },
    

    Typescript was probably confused as to which files it needed to include and probably thought my file that was built in ./build/index.d.ts was an input file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search