[英]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.我不能在 angular 的库中包含
package.json
中的版本,但它可以在项目中使用。 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.通过
require
的方式包括整个package.json
,这会带来安全风险。 import { version } from '../../package.json'
includes only the version number but works for angular applications not libraries. import { version } from '../../package.json'
仅包含版本号,但适用于角度应用程序而非库。
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.但一定要在你的 tsconfig.json 中添加
"resolveJsonModule": true
到compilerOptions
。
What about this code :这段代码怎么样:
const packageJson = require('../../package.json');
console.log(packageJson.version);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.