Reputation: 720
I can't include version from package.json
in angular's library but it works in a project. I use import like that:
import { version } from '../../package.json';
and get the following error:
ERROR: error TS6059: File '/Users/.../library/package.json' is not under 'rootDir' '/Users/.../library/src'. 'rootDir' is expected to contain all source files.
An unhandled exception occurred: error TS6059: File '/Users/.../library/package.json' is not under 'rootDir' '/Users/.../library/src'. 'rootDir' is expected to contain all source files.
See "/private/var/folders/tw/z8v0wkt50g5876740n99hzy00000gp/T/ng-0JDpjC/angular-errors.log" for further details.
The way through require
includes the whole package.json
which incur security risks. import { version } from '../../package.json'
includes only the version number but works for angular applications not libraries.
Upvotes: 3
Views: 10983
Reputation: 182
The solutions proposed under this post do work, however, they pose security threats as the full package.json file wil be exposed on the client side. This should be a more secure solution: In an Angular (>= 2) application, how to include version information from npm package.json file into the compiled output files?
Upvotes: 0
Reputation: 492
As Jozef mentioned make sure you turn on the "resolveJsonModule": true in your tsconfig.json.
import libraryPackage from '../path/to/package/package.json';
Another old solution is to make an http call and load the package.json
file content. This can be useful when your file content dynamically changes (example your deploy server updates the file content when it gets deployed).
Upvotes: -1
Reputation: 3497
You can import it like a regular module:
import { version } from 'package.json'
But be sure to add "resolveJsonModule": true
to compilerOptions
inside your tsconfig.json
.
Upvotes: 4
Reputation: 590
What about this code :
const packageJson = require('../../package.json');
console.log(packageJson.version);
Upvotes: 0