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?
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
 
    
    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": [
...
]
}
