简体   繁体   English

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

[英]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": truecompilerOptions

What about this code :这段代码怎么样:

const packageJson = require('../../package.json');
console.log(packageJson.version);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 库 - 导出 package.json 版本 - Angular library - export package.json version 从 angular 库访问 package.json - Access package.json from angular library Angular / node.js 如何将应用程序版本从 package.json 复制到另一个非 json 文件 - Angular / node.js how to copy the app version from package.json to anothe non json file 如何使用 angular 6 中的库发布指定已发布的 package.json? - How to specify published package.json with library publishing in angular 6? 如何从package.json加载angular(v4 +)应用程序中的版本号? - How to load version number in angular (v4+) app from package.json? 如何在 prod 模式下在 angular 中显示 package.json 的版本 - How to display the version from package.json in angular when built in prod mode 在角度库项目 package.json 文件中保持版本号同步 - Keeping version numbers in sync in angular library project package.json files 如何在角度项目中禁用 package.json 中的 eslint? - How to disable eslint from package.json on an angular project? 了解如何从package.json文件中提取依赖项,以便角度应用程序扫描非捆绑/缩小版本 - Understand how to extract the dependencies from the package.json file for an angular application to scan the non-bundled/minified version 如何在Angular应用程序中暴露package.json - How to expose package.json in angular app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM