Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 3ea13b5

Browse files
author
Michal Vlasák
committed
🐛Check package.json existence before loading and using it
1 parent 5c547e7 commit 3ea13b5

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ const maxLevelWrite: pino.WriteFn = function (this: any, data: object): void {
7878
};
7979

8080
const initFormatters = (options: CosmasOptions & { loggerName?: string }) => {
81-
const pkgJson = JSON.parse(fs.readFileSync(path.resolve(path.join(__dirname, '..', 'package.json')), 'utf8'));
81+
const pkgPath = path.resolve(path.join(__dirname, '..', 'package.json'));
82+
const pkgJson = fs.existsSync(pkgPath) ? JSON.parse(fs.readFileSync(pkgPath, 'utf8')) : undefined;
8283

8384
const formatters: pino.LoggerOptions['formatters'] = {};
8485
if (!options.pretty && !options.disableStackdriverFormat) {
@@ -91,8 +92,10 @@ const initFormatters = (options: CosmasOptions & { loggerName?: string }) => {
9192
formatters.log = (object: { [key: string]: any }) => {
9293
if (options.pretty) return object;
9394

94-
// put pkgVersion to non-pretty outputs
95-
object[pkgVersionKey] = pkgJson.version;
95+
if (pkgJson) {
96+
// put pkgVersion to non-pretty outputs
97+
object[pkgVersionKey] = pkgJson.version;
98+
}
9699
if (options.loggerName) {
97100
object[loggerNameKey] = options.loggerName;
98101
}

0 commit comments

Comments
 (0)