A plugin to read and parse parent package.json file as JSON object in TypeScript Node.Js. It is useful when you have a library that requires some attributes from parent Package.Json file. Simply install this library in your parent Node.Js JavaScript/TypeScript based application and call the function.
The objective of the plugin is to read the package.json
file contents as a valid JSON objects wherever you call the function. It will read the package.json
file of the caller location.
npm install parent-package-json-ts
Once the plugin is installed, import packageJson
function from parent-package-json-ts
package anywehere in your project and assign the JSON object in a variable.
import { packageJson } from 'parent-package-json-ts';
const contents = packageJson();
console.log(contents);
/*
{
"name": "parent-package-json-ts",
"version": "0.1.0",
"description": "Read and Parse Parent Package Json file in TypeScript.",
"scripts": {
"test": "jest --coverage",
"lint": "eslint ."
},
"repository": {
"type": "git",
"url": "git+https://github.com/stackblogger/parent-package-json-ts.git"
},
"engines": {
"node": ">= 16.0.0"
}
}
*/
If you don't want to use the plugin's default interface then you can have your own interface and provide it as prefix at the time of packageJson
function call. More details here-
import { packageJson } from 'parent-package-json-ts';
interface PackageJsonContent {
name: string;
version: string;
}
const contents = <PackageJsonContent>packageJson();
console.log(contents);
/*
{
"name": "parent-package-json-ts",
"version": "0.1.0"
}
*/
You can also read the parsed package.json
as JSON object from a custom directory. Simply pass the custom directory path in the function parameter.
Here are more details with example-
import { packageJson } from 'parent-package-json-ts';
const customPath = path.join('some-directory-here');
const contents = packageJson(customPath);
console.log(contents);
/*
{
"name": "parent-package-json-ts",
"version": "0.1.0",
"description": "Read and Parse Parent Package Json file in TypeScript.",
"scripts": {
"test": "jest --coverage",
"lint": "eslint ."
},
"repository": {
"type": "git",
"url": "git+https://github.com/stackblogger/parent-package-json-ts.git"
},
"engines": {
"node": ">= 16.0.0"
}
}
*/