1

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.

Maxim Palenov
  • 650
  • 7
  • 17
  • 1
    Does this answer your question? [How to display the app version in Angular?](https://stackoverflow.com/questions/34907682/how-to-display-the-app-version-in-angular) – Mark S. Dec 17 '19 at 16:11
  • 3
    Unfortunately not, I'm aware about that way but it doesn't for angular libraries. – Maxim Palenov Dec 18 '19 at 08:59
  • This solved my problem: https://stackoverflow.com/a/63294323 Consider `resolveJsonModule` needs to be added to `tsconfig.app.json`, not `tsconfig.json`. – maganap Nov 30 '20 at 18:08

3 Answers3

5

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.

Josef
  • 2,869
  • 2
  • 22
  • 23
  • 1
    As of angular 10, consider `resolveJsonModule` needs to be added to `tsconfig.app.json`, not to `tsconfig.json` – maganap Nov 30 '20 at 18:09
  • 1
    I'm in Angular 6 and using this approach yields an issue with doing the build-prod or aot. See: https://stackoverflow.com/questions/58931483/error-building-with-ng-build-prod-fine-without-prod-flag-and-ng-serve – edjm Dec 31 '20 at 12:33
  • 3
    Getting this error: Should not import the named export 'version' (imported as 'version') from default-exporting module (only default export is available soon) – Rafael Guimaraes Siqueira Aug 25 '21 at 19:20
1

What about this code :

const packageJson = require('../../package.json');
console.log(packageJson.version);
Benjamin Barbé
  • 494
  • 6
  • 18
  • 4
    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. – Maxim Palenov Dec 18 '19 at 08:58
0

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).

Gergő Kajtár
  • 454
  • 4
  • 12