30

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": ".",
  }
}
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114

7 Answers7

68

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.

8

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:

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

Referring to the question asked - this should work:

{
  "include": [
    "src/**/*",
    "package.json" // <---
  ]
}
Gil
  • 854
  • 1
  • 11
  • 23
7

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

RyanZ
  • 71
  • 1
  • 1
5

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

{ 
    "include": [
      "src/**/*",
      "package.json"
    ]
}
Atombit
  • 953
  • 9
  • 12
4

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.

3

Try adding:

"resolveJsonModule": true,
"esModuleInterop": true 

to your compiler options

Kaca992
  • 2,211
  • 10
  • 14
  • 3
    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 work – Paul Razvan Berg Feb 02 '20 at 18:19
  • If this doesn’t work, you’re most likely running into this bug: https://github.com/microsoft/TypeScript/pull/34676#issuecomment-557636399 – Florian Wendelborn Feb 13 '20 at 13:42
0

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.

bb1950328
  • 1,403
  • 11
  • 18