Coder Social home page Coder Social logo

jonschlinkert / get-pkg Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 3.0 26 KB

Get the package.json for a project from npm.

Home Page: https://github.com/jonschlinkert

License: MIT License

JavaScript 100.00%
package package-json pkg get request npm packagejson javascript node nodejs jonschlinkert download

get-pkg's Introduction

get-pkg NPM version NPM monthly downloads NPM total downloads

Get the package.json for a project from npm.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install

Install with npm:

$ npm install --save get-pkg

Also see get-pkgs, if you need to get more than one package.json.

Usage

const getPkg = require('get-pkg');
const data = await getPkg('generate')

console.log(data);
//=> { name: 'generate', ... }

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

  • get-pkgs: Get the package.json for an array of repos from the npm registry, optionally filtering properties… more | homepage
  • github-base: Low-level methods for working with the GitHub API in node.js/JavaScript. | homepage

Contributors

Commits Contributor
21 jonschlinkert
2 doowb
1 joakimbeng

Author

Jon Schlinkert

License

Copyright © 2023, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on July 24, 2023.

get-pkg's People

Contributors

doowb avatar joakimbeng avatar jonschlinkert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

get-pkg's Issues

get-pkg is broken

It seems, that something has changed on npm, basically the functionality and error handling in this repo is broken, which also causes verb-generate-readme to discontinue to work.

Running the tests of this repo (as of today) returns the following:

getPackages
    1) should get a package.json for the given project
    2) should handle errors
    3) should handle empty string
    4) should handle scoped packages


  0 passing (174ms)
  4 failing

  1) getPackages should get a package.json for the given project:
     Uncaught SyntaxError: Unexpected end of JSON input
      at JSON.parse (<anonymous>)
      at index.js:37:20
      at IncomingMessage.<anonymous> (node_modules/min-request/request.js:68:4)
      at endReadableNT (_stream_readable.js:1064:12)
      at _combinedTickCallback (internal/process/next_tick.js:138:11)
      at process._tickCallback (internal/process/next_tick.js:180:9)

  2) getPackages should handle errors:
     Uncaught SyntaxError: Unexpected end of JSON input
      at JSON.parse (<anonymous>)
      at index.js:37:20
      at IncomingMessage.<anonymous> (node_modules/min-request/request.js:68:4)
      at endReadableNT (_stream_readable.js:1064:12)
      at _combinedTickCallback (internal/process/next_tick.js:138:11)
      at process._tickCallback (internal/process/next_tick.js:180:9)

  3) getPackages should handle empty string:
     Uncaught SyntaxError: Unexpected end of JSON input
      at JSON.parse (<anonymous>)
      at index.js:37:20
      at IncomingMessage.<anonymous> (node_modules/min-request/request.js:68:4)
      at endReadableNT (_stream_readable.js:1064:12)
      at _combinedTickCallback (internal/process/next_tick.js:138:11)
      at process._tickCallback (internal/process/next_tick.js:180:9)

  4) getPackages should handle scoped packages:
     Uncaught SyntaxError: Unexpected end of JSON input
      at JSON.parse (<anonymous>)
      at index.js:37:20
      at IncomingMessage.<anonymous> (node_modules/min-request/request.js:68:4)
      at endReadableNT (_stream_readable.js:1064:12)
      at _combinedTickCallback (internal/process/next_tick.js:138:11)
      at process._tickCallback (internal/process/next_tick.js:180:9)


Scoped package names results in status 401

E.g:

getPkg('@cycle/core')
// throws: Uncaught SyntaxError: Unexpected end of input

Because the url is not generated correct for scoped packages and therefore results in a 401 and an empty body which makes the JSON.parse crash.

Use the unpkg instead of the npm registry?

Because it may be slow, it may crash and what not. While developing Badgen.net Service, we concluded to just switch to https://unpkg.com/${name}@${tag}/package.json.

Not to mention how confusing this npm API can be. It's total madness, seriously. This comment in the current master of get-pkg is one of many things. With Unpkg we everything is a whole lot easier and faster - you have scoped support, versions for all scoped/non-scoped, versions for tagged/non-tagged and etc.

Examples:

@foo/bar
@foo/[email protected]
@foo/bar@next
bar
[email protected]
bar@next

Or at least, we can allow it through some option, so it will unpkg and fallback to yarn.

I end up with this in few minutes (because non of the other packages does it well or are by Sindre, or are too old), before even realizing that get-pkg exist (i started to forget this good ol' packages - you are everywhere 🤣 ).

const parsePackage = require('parse-package-name')
const axios = require('axios')

// packageName: String, endpoint: (name: String, tag: String = 'latest'): String => {}
export default async function packageJson(packageName, endpoint) {
  const { name, version } = parsePackage(packageName)
  const tag = version === '' ? 'latest' : version
  const url = typeof endpoint === 'function'
    ? endpoint(name, tag)
    : `https://unpkg.com/${name}@${tag}/package.json`

  return axios.get(url).then((res) => res.data)
}

/cc @jonschlinkert @doowb

NPM server errors

FYI, there is a NPM server error causing certain requests to fail, but it's not showing on NPM's server status page. If you experience these errors, it's not related to this package.

err.response undefined

TypeError: Cannot read properties of undefined (reading 'status')
    at node_modules/get-pkg/index.js:48:24

Apparently there are cases where the err does not include a response property.

Could be due to npm being down or whatever, a user can implement retry on their end.

But running over a small handful of repos I've seen this multiple times...so it is fairly common case and could be handled better in the lib. I'm hitting this with get-pkgs (passing array of package names)...so I also don't know which specific package is having trouble or whether it is a temporary issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.