Home > Software engineering >  Understanding node modules
Understanding node modules

Time:11-07

How do I work with node modules?

tldr; How do I look at a node module I've installed and know where to go and what I'm looking for

If I use npm i googleapis for example, it downloads the node module for Googles APIs but how do I browse the module and work out what's useful for me? To try and eliminate any ambiguity from the question, I'll use this use case.

I'm developing a Discord bot and I want to add statistics to one of the commands. Here is the supplied code from Google:

<script src="https://apis.google.com/js/api.js"></script>
<script>
  /**
   * Sample JavaScript code for youtube.channels.list
   * See instructions for running APIs Explorer code samples locally:
   * https://developers.google.com/explorer-help/code-samples#javascript
   */

  function authenticate() {
    return gapi.auth2.getAuthInstance()
        .signIn({scope: "https://www.googleapis.com/auth/youtube.readonly"})
        .then(function() { console.log("Sign-in successful"); },
              function(err) { console.error("Error signing in", err); });
  }
  function loadClient() {
    gapi.client.setApiKey("YOUR_API_KEY");
    return gapi.client.load("https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest")
        .then(function() { console.log("GAPI client loaded for API"); },
              function(err) { console.error("Error loading GAPI client for API", err); });
  }
  // Make sure the client is loaded and sign-in is complete before calling this method.
  function execute() {
    return gapi.client.youtube.channels.list({
      "part": [
        "snippet,contentDetails,statistics"
      ],
      "id": [
        "UC_x5XG1OV2P6uZZ5FSM9Ttw"
      ]
    })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }
  gapi.load("client:auth2", function() {
    gapi.auth2.init({client_id: "YOUR_CLIENT_ID"});
  });
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>

Now Google offers a supported library which means I can replace the external script tags and import from the node package, specifically the parts I need.

So I'll need to import or require whatever gives me access to things like:
gapi.auth2.getAuthInstance
gapi.client.setApiKey
gapi.client.youtube.channels.list

For someone who is new to nodejs, instead of copy and pasting from every piece of documentation and hoping it works, how do I comfortably look at a node package and find the things I need and can use?

Edit #1

I think my use of the google apis case threw off the direction of the question and changed the scope of what I asked so I'm going to correct the best I can.

The assumption should be made that there is no documentation on the package whether it's so poorly written, doesn't exist at all or the documentation is down for an extended period of time during a time sensitive development.

At that point, is there any possible way to look at the node_modules folder, the specific package that needs to be worked with and work out what's going on? Is there any way to look at the structure of a package and recognise "well most likely what I need is in this folder or file"

CodePudding user response:

That's what documentation is for. When someone writes an API for a package; to make things clearer for the consumer, he should document the exported functions well enough.

The best way to get the documentations for node packages is to search for the package at www.npmjs.com .

For google apis you can go to that page here to see the "get started" and some examples. And you can go here to see the full detailed APIs of that package.

Answering Edit #1

Well, in that case, it could be a difficult task, depends on the how structured and organized the package is.

Since we are talking about nodejs, you should look for the package.json file and search for the path of the main file "main": "<PATH HERE>".

Then, you can go to that main file and try to locate what exactly is being exported. You can search for module.exports or the export keyword.

Everything that is explicitly exported is intended to be used as an API.

I'm not familiar with any other way other than go deeper in the package's files and identify what exactly is being exported.

  • Related