首页 > 解决方案 > 如何从角度库中的 package.json 获取版本

问题描述

我不能package.json在 Angular 的库中包含版本,但它可以在项目中使用。我像这样使用导入:

import { version } from '../../package.json';

并得到以下错误:

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.

穿越的方式require包括了package.json会带来安全风险的整体。import { version } from '../../package.json'仅包括版本号,但适用于 Angular 应用程序而不是库。

标签: angulartypescript

解决方案


您可以像常规模块一样导入它:

import { version } from 'package.json'

但请务必添加"resolveJsonModule": truecompilerOptions您的 tsconfig.json 中。


推荐阅读