mousesports mousesports - 1 year ago 71
Javascript Question

Check if current directory contains package.json and who's name is equal to value

I am trying to do two things when I fire off a custom command in the terminal:

  1. Check if the current directory has a package.json (something like checking if
    process.cwd() + '/package.json'

  2. Check if the name in package.json is equal to some value of "customName"

Answer Source

You can take advantage of the fact that require() can be used to load JSON files.

'use strict';

try {
  var packageData = require(process.cwd() + '/package.json');
} catch (e) {
  // There was no package.json
  console.log('no package.json found!');

console.log("name is",;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download