Node: cd myjourney03

adebiyiitunuayo

FFFF:0000h

Posted on May 27, 2022

Node: cd myjourney03

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, 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

Click on it to see what it's about

Heyy, it's on github
Think github, think Opensource.

upper-case

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

So I install this package on my CLI

upper-case installed

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

So here's part of the codes in the index.js file
index.js

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

var demo = require('upper-case');
Enter fullscreen mode Exit fullscreen mode

And write a text using the upperCase() function seen earlier.
So the whole thing looks like

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);
Enter fullscreen mode Exit fullscreen mode

server

Then I run the server

ServerUp

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

Package-Effect
šŸ’” 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.

šŸ’– šŸ’Ŗ šŸ™… šŸš©
adebiyiitunuayo
FFFF:0000h

Posted on May 27, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Node: cd myjourney03
node Node: cd myjourney03

May 27, 2022