Skip to content
webtype.orgwebtype.orgTS5055

    ↑↓ move · ⏎ open · esc close

    TS5055

    Emitting over your own source

    Cannot write file 'src/app.js' because it would overwrite input file.

    The compiler’s own words. Not translated — this is the string you pasted into a search box.

    With `allowJs` on, a `.js` file is both an input and the name of an output. If the output goes where the input lives, the compiler would overwrite the file it just read.

    Reproduction

    // tsconfig.json
    {
      "compilerOptions": {
        "allowJs": true,
        "outDir": "src"
      }
    }
    
    // src/app.js — read as an input, and the exact path the emit would land on
    export const version = 1

    The build asserts this emits exactly this code.

    Why the compiler says this

    Nothing else in this catalogue is raised about a file; this one is raised about the build. `allowJs` puts `src/app.js` in the program as a source, and compiling it produces JavaScript that the emitter wants to call `app.js` in `outDir` — the same path. The compiler will not destroy an input to satisfy a configuration, so it refuses before writing anything and the whole emit stops. That is also why nothing appears in the output directory: this is not a warning about one file, it is a build that did not happen.

    Fixes

    1. 01
      // tsconfig.json
      {
        "compilerOptions": {
          "allowJs": true,
          "outDir": "dist"
        }
      }
      
      // src/app.js stays an input; the emit lands in dist/
      export const version = 1

      Give the output a directory of its own. Inputs and outputs sharing a folder is the whole problem, and every other arrangement follows from separating them.

    2. 02
      // tsconfig.json
      {
        "compilerOptions": {
          "allowJs": true,
          "checkJs": true,
          "noEmit": true
        }
      }
      
      // src/app.js is checked and left exactly as it is
      export const version = 1

      Or stop emitting. If a bundler already builds this project, TypeScript is there to check it — `noEmit` says so, and the collision cannot arise.

    Takeaway

    Decide whether TypeScript builds this project or only checks it. Most of this error is a project that answered both at once.

    Errors