Project must list all files or use an 'include' pattern
Asked Answered
M

9

51

I see this warning thrown in VSCode:

VSCode Warning

This is the line that throws the ts warning:

import packageJson from "../package.json";

Weirdly, building and linting the project works fine:

$ tsc --project .
✨  Done in 1.16s.
$ tslint --config ../../tslint.json --project .
✨  Done in 1.59s.

Is this a warning caused by the VSCode parser, or is there something wrong in my tsconfig.json file?

// tsconfig.json
{
  "exclude": [
    "node_modules"
  ],
  "extends": "../../tsconfig.json",
  "files": [
    "package.json"
  ],
  "include": [
    "src/**/*"
  ],
  "compilerOptions": {
    /* Basic Options */
    "outDir": "dist",
    /* Module Resolution Options */
    "baseUrl": ".",
  }
}
Martguerita answered 2/2, 2020 at 18:7 Comment(0)
L
100

In my case I was builiding a monorepo and referencing one of the packages into another package.

All I had to do was remove composite: true from tsconfig.json and it worked.

Lelahleland answered 2/2, 2022 at 23:9 Comment(4)
It works! Why this is not accepted answer? or maybe it raise another problem for others?Indopacific
This worked for me, but what functionality are we losing by removing composite? I'm referring to my package tsconfig.json from a base tsconfig.json using references.Lugworm
why does it work?!Paramatta
You shouldn't do this. Setting composite to false disables enforcement of includes patterns (see docs). It's not clear to me why JSON files aren't included by default when writing wildcard globs, but specifically including *.json files seems to work fine (TS 5.2.2).Yardman
M
40

As dumb as it sounds, restarting VSCode fixed this for me.

Milker answered 31/10, 2023 at 16:27 Comment(8)
unbeliveable, but same for me :DResiniferous
worked in my Jetbrains IDE (PyCharm) as wellSprit
same for me :)))Legitimatize
Same here. You don't have to re-start though. You can re-load the window: hit F1, serach for "reload developer window", select the entry and hit Enter. This solves a lot of issues with false errors in VSCode.Depressor
worked for me to restart aswellVeratridine
Have you tried turning it off and on again?Spires
Even better than the suggestion from @Fred, from the command palette, run Typescript: Restart TS Server to quickly restart the TS language server only.Lafontaine
@JoeSadoski awesome, thanks for sharing! This avoids all the popups from re-loaded plugins :-DDepressor
G
15

Samuel's suggestion works great for me.

I got the same error for another reason, I use .json files as static mocks for tests - importing them as modules. In my case the files are nested inside src, and adding an explicit pattern to include .json files did the trick for me.

Here's the relevant part in my tsconfig.json:

{
  "include": [
    // ...other includes
    "src/**/*.json"
  ]
}

Referring to the question asked - this should work:

{
  "include": [
    "src/**/*",
    "package.json" // <---
  ]
}
Guthrey answered 6/9, 2022 at 0:25 Comment(4)
This answer isn't very useful since you don't mention which file the above code is meant to go into.Extern
@Extern Obviously tsconfigMarkus
@Markus Nothing ever is obviousDepressor
didn't work unfortunatelyGrivation
Y
8

Open your tsconfig.node.json, and add your files path in the includes just like "include": ["vite.config.ts", "yourFilePath"],

Yoshieyoshiko answered 20/2, 2022 at 16:19 Comment(1)
Adding "src/folderName/*.json" on tsconfig.include worked for meWaugh
A
6

Add package.json to your includes. It out of src dir.

{ 
    "include": [
      "src/**/*",
      "package.json"
    ]
}
Azeotrope answered 3/8, 2020 at 15:37 Comment(2)
but I got: error TS6059: File 'package.json' is not under 'rootDir' 'src'. 'rootDir' is expected to contain all source files.Sideshow
Is your rootDir set to src? If so it won't work, remove rootDir or set to "."Quent
C
6

This can happen if your server is running when you add new files. If this is the case, simply stop it and start it again so that is can rebuild and include the new files.

Climacteric answered 5/8, 2022 at 7:58 Comment(0)
M
2

Try adding:

"resolveJsonModule": true,
"esModuleInterop": true 

to your compiler options

Manolo answered 2/2, 2020 at 18:14 Comment(2)
I already had these in the topmost tsconfig.json file, but to make sure I also dropped it in the nested one (the file from the OP); it did NOT workMartguerita
If this doesn’t work, you’re most likely running into this bug: github.com/microsoft/TypeScript/pull/…Antiseptic
C
2

On WebStorm

After including the file in tsconfig.app.json, I also had to do "File" > "Invalidate Caches and Restart" before the error went away.

EDIT: Sometimes it's enough to just restart the Vue Language Server (at the bottom right)

Cheetah answered 25/8, 2023 at 18:21 Comment(1)
Thanks! Not sure I would have ever figured this one out on my own.Tetryl
M
2

There's an incredibly long thread about this issue in the TypeScript repository.

Although not a permanent fix, the easiest solution is restarting your TypeScript server.

Ctrl + Shift + P > TypeScript: Restart TS Server With this, you don't have to restart vscode or reinstall node_modules.

Mindoro answered 5/1 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.