Node: cd myjourney03
Tags: node, javascript, todayilearned, writing
Published: 2022-05-27 20:23:41

It's been a while I posted my learning progress due to health challenges and school. However I decided to resume learning Nodejs and posting my progress since things are good now.

So today was great, I learned about the **NPM** a.k.a Node Package Manager.

> The npm is the package manager for the Node JavaScript platform. It puts modules in place so that node can find them, and manages dependency conflicts intelligently.

Now this seemed confusing at first to me. So I asked myself "what is a package". Now it meant something different to me from what I found out coming from a Java language background where a package is a group of similar type of classes.

So I hit up the Google website. _Hey there's a website named [npmjs](npmjs.com)_, dedicated to the npm I was learning about which is _it_, NPM, the _thing_. I click on this site and run a few more clicks and found:

> A package is a file or directory that is described by a **package.json** file. 
A package is any of the following:
a) A folder containing a program described by a `package.json` file.
b) A gzipped tarball containing (a).
c) A URL that resolves to (b).
d) A `<name>@<version>` that is published on the registry with (c).
e) A `<name>@<tag>` that points to (d).
f) A `<name>` that has a latest tag satisfying (e).
g) A git url that, when cloned, results in (a).


💡 So I now understand that a package in Nodejs term is not a package in Java term and I shouldn't assume but instead confirm things.

I learned that NPM is broad as a concept and as a _thing_. Why? 

> Npm consists of three distinct components:
- The **website**: Use the website to discover packages, set up profiles, and manage other aspects of your npm experience. For example, you can set up organizations to manage access to public or private packages.

> - The **CLI** (Command Line Interface) which runs from a terminal, and is how most developers interact with npm.

> - The **registry** which is a large public database of JavaScript software and the meta-information surrounding it.

💡 So when anyone mentions NPM, you're allowed to ask which part of NPM they are talking about and not just assume it to be the CLI because I'm always in the terminal for it.

So I'm working with the CLI component of NPM via Termux terminal. The npm was installed when I installed nodejs on my device in my terminal so no need for installing it separately.

Now there's a package in NPM (website) named "upper-case" used to convert strings to upper case. 
That is: "hey" turns to "HEY". 


So I search on the website for this package

![npm-upper-case-package](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/abkduwe2bryqtcc4pkp6.png)

Click on it to see what it's about 

_Heyy, it's on github 
Think github, think Opensource._ 
![upper-case](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ps4stwx5gavc0kxx2jhr.png)

How could I forget, Nodejs is Opensource itself and NPM too.

So I install this package on my CLI

![upper-case installed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9y1nvqolzbor4nz49lva.jpg)

Then I check what has been installed, the files and all, see a package.json file and other new files. **node_modules**, what is that, instinct?. I navigate into it. And find files again, navigate to the **dist.es2015** folder instinctively too to find many files but the one file that caught my attention was a simple **index.js** file which I opened with a code editor and found interesting things. 

![upper-case](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/btkk37fy9dhusqa9l6mf.png)

So here's part of the codes in the **index.js** file
![index.js](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pp3rxahkwdvf0r5k43l4.png)

I understood that part of this code was setup so it could be imported or _require_d and I can see an uppercase function which converts strings _javascriptically_ (lol).
Makes sense to me. I exit. 

So now back to my server, time to use this package.
I include the upper-case package in my already set up server
```javascript
var demo = require('upper-case');
```

And write a text using the **upperCase()** function seen earlier.
So the whole thing looks like
```javascript
var http = require('http');
var demo = require('upper-case');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write(demo.upperCase("Hello John!"));
  res.end();
}).listen(8080);
```

![server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amr1cw4rojz3jpdywfdg.png)

Then I run the server

![ServerUp](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97fm2t6ozvvoy60g3a54.png)

I request via port 8080 and my server DISPLAYS the string argument in CAPITAL LETTERS. Amazing. 

![Package-Effect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hd1c0yt3ivfy5nnez66h.png)
💡 So I don't have to stress writing a logic to do this anymore, I just install and download the upper-case package. 
Imagine what other packages on NPM registry can do. 
The power of packages. 
Things learned: NPM, package, modules

Resource: Google, W3schools, Stackoverflow. 

Day 4. Progress.