Fetch information from package.json

Asked

Viewed 491 times

0

I tried to do this using require, but it didn’t work and there was this error :

ERROR in src/app/footer/footer.component.ts(8,21): error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i @types/node`.

Footer.component.ts :

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-footer',
  templateUrl: './footer.component.html',
})
export class FooterComponent implements OnInit {
  packageJsonInfo = require('Workspace\develop\workspace-projetos_pessoais\geladeira-de-casa-ng\geladeira-ng\package.json');

  constructor() { }

  ngOnInit() {
    console.log(this.packageJsonInfo.author);
    console.log(this.packageJsonInfo.version);
  }
}

Package.Json :

{
  "name": "geladeira-ng",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "postinstall": "npm prune"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^7.0.4",
    "@angular/common": "~7.0.0",
    "@angular/compiler": "~7.0.0",
    "@angular/core": "~7.0.0",
    "@angular/forms": "~7.0.0",
    "@angular/http": "~7.0.0",
    "@angular/platform-browser": "~7.0.0",
    "@angular/platform-browser-dynamic": "~7.0.0",
    "@angular/router": "~7.0.0",
    "core-js": "^2.5.4",
    "intl": "1.2.5",
    "rxjs": "~6.3.3",
    "zone.js": "~0.8.26",
    "jquery": "3.1.1",
    "reflect-metadata": "0.1.9",
    "web-animations-js": "^2.2.5",
    "font-awesome": "4.7.0"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.10.0",
    "@angular/cli": "~7.0.5",
    "@angular/compiler-cli": "~7.0.0",
    "@angular/language-service": "~7.0.0",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^8.10.46",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.1.6"
  }
}

I’ve tried to npm i @types/node

  • You can’t just import the file? import pkg from "package.json" -> pkg.author.

  • I don’t really know, I’m studying Typescript and Angular and I looked for the first solution in Stack Overflow gringo, this import I put where in the code?

  • Place on top of the import { Component, OnInit } from '@angular/core'; pointing to the correct path of package.json.

  • I managed to capture the version by doing what I showed in the reply, before I had tried to import the way you said and it had not worked out tbm, but I believe it was because I put the wrong path.

1 answer

1


I managed to recover the version number by doing the following, but I do not know if it is a good practice:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-footer',
  templateUrl: './footer.component.html',
})
export class FooterComponent implements OnInit {
  constructor() {
  }
  public appVersion;

  ngOnInit() {
    const { version: appVersion } = require('package.json');
    console.log(appVersion);
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.