49

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": ".",
  }
}
0

9 Answers 9

98

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.

4
  • 2
    It works! Why this is not accepted answer? or maybe it raise another problem for others?
    – FatihAziz
    Commented Jul 21, 2022 at 8:25
  • 14
    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.
    – dimiguel
    Commented Sep 25, 2022 at 23:24
  • 7
    why does it work?!
    – roomcayz
    Commented Feb 1, 2023 at 8:26
  • 1
    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).
    – sheng
    Commented Oct 31, 2023 at 17:01
33

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

6
  • 6
    unbeliveable, but same for me :D Commented Dec 17, 2023 at 17:34
  • 1
    worked in my Jetbrains IDE (PyCharm) as well Commented Dec 19, 2023 at 5:10
  • 1
    same for me :))) Commented Feb 7 at 17:20
  • 2
    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.
    – Fred
    Commented Mar 9 at 14:59
  • 1
    worked for me to restart aswell
    – Soma Juice
    Commented Apr 9 at 13:53
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" // <---
  ]
}
3
  • This answer isn't very useful since you don't mention which file the above code is meant to go into.
    – user275801
    Commented Jun 17, 2023 at 8:24
  • 4
    @user275801 Obviously tsconfig Commented Jul 26, 2023 at 12:09
  • @idkwhatsgoingon Nothing ever is obvious
    – Fred
    Commented Mar 9 at 14:57
8

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

1
  • Adding "src/folderName/*.json" on tsconfig.include worked for me Commented Jun 18, 2023 at 14:49
6

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

{ 
    "include": [
      "src/**/*",
      "package.json"
    ]
}
2
  • 1
    but I got: error TS6059: File 'package.json' is not under 'rootDir' 'src'. 'rootDir' is expected to contain all source files.
    – 蔡宗容
    Commented Jan 5, 2022 at 7:07
  • Is your rootDir set to src? If so it won't work, remove rootDir or set to "." Commented Mar 10, 2023 at 19:53
5

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.

2

Try adding:

"resolveJsonModule": true,
"esModuleInterop": true 

to your compiler options

2
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)

1
  • Thanks! Not sure I would have ever figured this one out on my own.
    – stanek
    Commented Sep 27, 2023 at 19:57
1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.