如何从角度库中的package.json获取版本

kat*_*zz0 1 typescript angular

我无法包含package.json角度库中的版本,但它可以在项目中使用。我这样使用导入:

import { version } from '../../package.json';
Run Code Online (Sandbox Code Playgroud)

并得到以下错误:

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.
Run Code Online (Sandbox Code Playgroud)

该途径包括产生安全风险的require整体。仅包含版本号,但适用于角度应用程序而不是库。package.jsonimport { version } from '../../package.json'

Jos*_*sef 5

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

import { version } from 'package.json'
Run Code Online (Sandbox Code Playgroud)

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

  • 收到此错误:不应从默认导出模块导入命名的导出“版本”(作为“版本”导入)(仅默认导出很快可用) (3认同)