node-packages-installed-version
v1.0.5
Published
Small library developed to get some or list all installed version of project dependencies in a NodeJS environment.
Readme
Node Packages Installed Version
Small library developed to get the current exact version of some or all installed dependencies in a NodeJS project.
Features
Getting started
This package was inspired by the need to know what Prisma ORM version is in currently in use while I was developing the Prisma ORM generator called "@ladislaogarcia/zod-prisma-generator".
Install
With npm:
npm install node-packages-installed-versionUsage
// Import the package
import { getNodePackagesInstalledVersion } from 'node-packages-installed-version';
// Get the installed version of all the installed dependencies
getNodePackagesInstalledVersion();
// Get the installed version of an installed dependency in particular
getNodePackagesInstalledVersion(packageName);API
getNodePackagesInstalledVersion(packageName?)
packageName
Type: string
This is the name in npmjs.com of the package which version is required. If it is omitted, the package will retrieve all the dependencies of the project.
Only retrieves dependencies directly related with the project. It means the package only get dependencies from nivel zero.
Then, it will returns:
null; // If the package is not a dependency or if it is not installedor this structured data with the package name as key of the data value with the version.
NPIVDependencyList {
// Pacakge Name
[packageName: string]: {
name: string; // Pacakge Name
version: {
major: number; // Major version as number.-> Eg: 4
minor: number; // Minor version as number.-> Eg: 2
patch: number; // Patch version as number -> Eg: 5
full: string; // Complete text version -> Eg: "4.2.5"
};
},
[anotherPackageName: string]: {
name: string; // Next Pacakge Name
...
};
}So it is easy to retrieve the data of any wanted package. Just use the name of the package as index of the returned data to retrieve it. And use it.
// It will retrieve the version of each dependency.
const data = getNodePackagesInstalledVersion();
// 'packageName' is the name of the package for retrieve its data.
const pkgData = data[packageName];
// That is the same as if it is done...
const pkgData = getNodePackagesInstalledVersion(packageName);
// It is depends of the project needs.You can run a demostration that tries to retrieve the installed version of three use cases:
- 'Prettier': Installed dependency.
- 'Storybook': Not a dependency of this project.
It can run to check for non-installed dependencies. It is only required to install any package and remove it manually from inside 'node_modules'. Do not use 'npm uninstall' because it also will erase the dependency from 'package.json'.
Then, add it to 'items' array as this:
{
packageName: '<npm-package-name-added>',
text: 'Not Installed Dependency',
},You can run it just typing in a terminal
npm run demoYou will receive a response like this:
Getting data from "PRETTIER" as INSTALLED DEPENDENCY
DEPS:
{
prettier: {
name: 'prettier',
version: { major: 3, minor: 5, patch: 3, full: '3.5.3' }
}
}
Getting data from "STORYBOOK" as NOT A DEPENDENCY
DEPS:
null