I'm trying to import package.json in my TypeScript application:
import packageJson from '../package.json';
My tsconfig.json contains the following:
{
"compilerOptions": {
"rootDir": "./src/"
"outDir": "./dist/",
"baseUrl": ".",
"resolveJsonModule": true
}
}
The problem is that when I compile this, I get
error TS6059: File '/path/to/package.json' is not under 'rootDir' '/path/to/app/src/'. 'rootDir' is expected to contain all source files.
I'm not sure I understand the issue, because both ./src/ and ./dist/ have the same parent .., so TypeScript could just leave alone the import '../package.json' and it would work from either rootDir or outDir.
Anyway, I've tried the following, with unsatisfactory results:
- remove
rootDir- compilation works, but thedistwill containdist/src, which I don't want - remove
outDir- thensrcgets polluted with.jsfiles (and.js.mapifsourceMapwas true) - add
@ts-ignore- compilation stops the the file that imports../package.json
What's the workaround for this limitation, to keep generated files in dist, and allow importing from the parent directory of rootDir?
// @ts-ignorehelp?// @ts-ignoredoesn’t help.importstatement doesn't jive with the giventsconfig. Produces error. See my suggested edit.baseUrlinstead ofrootDir?requireinstead ofimport. Of course this assumes you're on the node platform. And when distributing the package you're not distributingdistbut the entire directory containingpackage.json.