Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get version from package.json in angular library

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.

like image 335
Maxim Palenov Avatar asked Aug 12 '26 05:08

Maxim Palenov


1 Answers

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.

like image 71
Josef Avatar answered Aug 15 '26 09:08

Josef