TS6059

error TS6059: File ‘/root/project/packages/server/package.json‘ is not under ‘rootDir’ ‘/root/project/packages/server/src‘. ‘rootDir’ is expected to contain all source files.

Broken Code ❌

1
import pkg from '../../../../package.json';

Fixed Code ✔️

When using require then we can access files outside the specified root folder for input files (“rootDir” in “tsconfig.json”):

1
const pkg = require('../../../../package.json');

An even better solution would be changing the “rootDir” in your “tsconfig.json”, so that it includes the “package.json” file that you are trying to important. This may require you to also set “allowJs” to true in your “tsconfig.json”.