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 apackage.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
Click on it to see what it's about
Heyy, it's on github
Think github, think Opensource.
How could I forget, Nodejs is Opensource itself and NPM too.
So I install this package on my CLI
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.
So here's part of the codes in the index.js file
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');
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);
Then I run the server
I request via port 8080 and my server DISPLAYS the string argument in CAPITAL LETTERS. Amazing.
π‘ 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.
Top comments (0)