New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

get-pkg-prop

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-pkg-prop

Pass a key name & get the corresponding value from the intended package.json

  • 0.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

get-pkg-prop

Pass a key name & get the corresponding value from the intended package.json

travis CI build status

Getting Started

Install

npm install get-pkg-prop -S

Add to source

import { getPkgProp } from "get-pkg-prop";

Use

import { getPkgProp } from 'get-pkg-prop'

const pkgMock = { version: "1.0.0", custom: { foo: "bar", baz: "bax" }   }

(async () => {
  await getPkgProp("name");
  // --> "get-pkg-prop"
  await getPkgProp("version", "path/to/child/module");
  // --> "x.y.z"
  await getPkgProp("custom", pkgMock)
  // --> { foo: "bar", baz: "bax" }
  await getPkgProp("fakeprop");
  // --> false
})

API

getPkgProp(szProperty) Where
  • szProperty is the name of the property.

How

  • This uses pkg-up to find the closest package.json.

Why

  • Useful for confirmations when toggling between local linked deps and remote installed deps. I wanted to enable modules to self-report relevant meta info (like their version)

import { getPkgProp } from 'get-pkg-prop'

(async () => {
  await getPkgProp("name")
  // --> "get-pkg-prop"
  await getPkgProp("version")
  // --> "0.2.6"
  await getPkgProp()
  // --> null; must provide property to check
  await getPkgProp("xyz")
  // --> false; property must exist in package.json
  await getPkgProp("repository")
  // --> { "type": "git", "url": "https://github.com/servexyz/get-pkg-prop" }
  */
})
getPkgProp(szProperty, oPackageJSON) Where
  • szProperty is the name of the property.
  • oPackageJSON is the JSON object which contains your package

Why

  • I added this for mock testing inline package objects.
import { getPkgProp } from 'get-pkg-prop'

const pkgMock = {
  "name": "my-pkg",
  "version": "1.0.0",
  "repository": {
      "type": "git",
      "url": "https://github.com/namespace/my-repo-pkg"
  }
}

(async () => {
  await getPkgProp("name", pkgMock)
  // --> "my-pkg"
  await getPkgProp("name")
  // --> "get-pkg-prop"; defaults to current pkg when unspecified
  await getPkgProp("version", pkgMock)
  // --> "0.2.6"
  await getPkgProp(, pkgMock)
  // --> null; must provide property to check
  await getPkgProp("xyz")
  // --> false; property must exist in package.json
  await getPkgProp("repository", pkgMock)
  // -->  { "type": "git", "url": "https://github.com/namespace/my-repo-pkg" }
  await getPkgProp("repository")
  // pkgMock wasn't specified, it returns value of cwd package:
  // -->  { "type": "git", "url": "https://github.com/servexyz/get-pkg-prop" }
})
getPkgProp(szProperty, szPathToPackageJSON) Where
  • szProperty is the name of the property.
  • szPathToPackageJSON is the path to either your package.json file or the directory which contains the package.json file.

Why

  • Specifying the path allows you to access the package of sub-modules or installed dependencies.

Note

  • "path/to/child/module" and "path/to/child/module/package.json" are processed equally

import { getPkgProp } from 'get-pkg-prop'

(async () => {
  await getPkgProp("name")
  // --> "get-pkg-prop"; defaults to current pkg when unspecifeid
  await getPkgProp("name", "path/to/child/module")
  // --> "child-module-name"
  await getPkgProp("version", "path/to/child/module")
  // --> "x.y.z"
  await getPkgProp(,"path/to/child/module")
  // --> null; must provide property to check
  await getPkgProp("xyz", "path/to/child/module")
  // --> false; property must exist in package.json
  await getPkgProp("repository", "path/to/child/module")
  // --> { "type": "git", "url": "https://github.com/namespace/child-module-name" }
})

FAQ

Why
  • Initially motivated by the desire to print package version
  • Destructuring rocks. const { version } = require('./package.json').
    • Require's caching makes it difficult to use with development. Read more here
    • Unfortunately, Webpack throws a critical warning when using dynamic requires in certain circumstances (see example here). And while there are supposed fixes, I wanted to avoid the cluster altogether since I use webpack in almost every javascript project
  • I wanted to have a more diverse API for different situations.
    • Module self-testing? Call get-pkg-prop("property") without specifying package.
    • Testing child-module? Call get-pkg-prop("property", "/sub/path/to/package.json") with package path specified.
    • Mock testing? Create a mock package JSON object and call it with get-pkg-prop("property", myPkgObj)
return scheme
  • <null> - Whenever the <string> szProperty parameter is missing
  • <string> - Whenever the property is found and extracted from the specified package object
  • <false> - Whenever the property does not exist in the specified package object

Keywords

FAQs

Package last updated on 08 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc