0

I want check the version of the mysql module in my typescript code.

I imagine code like this:

import mysql from "mysql"

if(mysql.version == "2.18.1"){
   ...
}

How can I do this?

Gerd
  • 2,265
  • 1
  • 27
  • 46

2 Answers2

0

One possible way is to get the version directly from your package.json, for example with package. Something like this:

const package = require('package')(module);
console.log(package.dependencies.mysql);

You could also do JSON.parse and fs.readFile.

Edit:

Maybe you can add npm and @types/npm to your dependencies and then:

import npm from 'npm';

npm.load((err, result) => {
  npm.commands.list([], (err, result) => {
    console.log(result);
  });
});

Result:

[...]
├── dotenv@8.2.0
└── express@4.17.1
pzaenger
  • 11,381
  • 3
  • 45
  • 46
0
let pjsonMysql: {version: string} = require('../node_modules/mysql/package.json');
console.log("version: " + pjsonMysql.version);

if(pjsonMysql.version == '2.18.1'){

}

It based on https://stackoverflow.com/a/10855054/1514029

The version is allways in package.json!

on mysql package for example

{
  "name": "mysql",
  "description": "A node.js driver for mysql. It is written in JavaScript, does not require compiling, and is 100% MIT licensed.",
  "version": "2.18.1",
  "license": "MIT",
  "author": "Felix Geisendörfer <felix@debuggable.com> (http://debuggable.com/)",
  "contributors": [
...
]
}
Gerd
  • 2,265
  • 1
  • 27
  • 46