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 thedist
will containdist/src
, which I don't want - remove
outDir
- thensrc
gets polluted with.js
files (and.js.map
ifsourceMap
was 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-ignore
help? – Nevarez// @ts-ignore
doesn’t help. – Frearimport
statement doesn't jive with the giventsconfig
. Produces error. See my suggested edit. – TablespoonbaseUrl
instead ofrootDir
? – Naiveterequire
instead ofimport
. Of course this assumes you're on the node platform. And when distributing the package you're not distributingdist
but the entire directory containingpackage.json
. – Dott