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 the dist
will contain dist/src
, which I don't want
- remove
outDir
- then src
gets polluted with .js
files (and .js.map
if sourceMap
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
?
question from:
https://stackoverflow.com/questions/55753163/package-json-is-not-under-rootdir 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…