Thursday, September 26, 2019

Node.js - npm - semantic versioning

Node.js developers follow the semantic versioning spec. Here is the small excerpt summary from the website:

Given a version number MAJOR.MINOR.PATCH, increment the:
  1. MAJOR version when you make incompatible API changes,
  2. MINOR version when you add functionality in a backwards compatible manner, and
  3. PATCH version when you make backwards compatible bug fixes.


Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.



References:
  1. https://docs.npmjs.com/about-semantic-versioning
  2. https://semver.org/



Node.js - npm - simple commands



To list all npm packages installed.

~$ npm ls
ubuntu@1.0.0 /home/ubuntu
`-- underscore@1.9.1

To remove locally installed npm package
~$ npm rm underscore --save
npm WARN ubuntu@1.0.0 No description
npm WARN ubuntu@1.0.0 No repository field.

removed 1 package in 1.363s
found 0 vulnerabilities

Listing the packages installed using npm.
~$ npm ls
ubuntu@1.0.0 /home/ubuntu
`-- (empty)

Installing the removed package locally again.
~$ npm install underscore --save
npm WARN ubuntu@1.0.0 No description
npm WARN ubuntu@1.0.0 No repository field.

+ underscore@1.9.1
added 1 package from 1 contributor and audited 1 package in 0.697s
found 0 vulnerabilities

Listing the packages installed using npm.
~$ npm ls
ubuntu@1.0.0 /home/ubuntu
`-- underscore@1.9.1

~$ 




Tuesday, September 24, 2019

Node.js - install npm package - underscore

Let us install a module, underscore: https://www.npmjs.com/package/underscore to a folder.

Step 1: Install the npm module:

~$ npm install underscore
npm WARN saveError ENOENT: no such file or directory, open '/home/ubuntu/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/home/ubuntu/package.json'
npm WARN ubuntu No description
npm WARN ubuntu No repository field.
npm WARN ubuntu No README data
npm WARN ubuntu No license field.

+ underscore@1.9.1
added 1 package from 1 contributor and audited 1 package in 0.585s
found 0 vulnerabilities

~$

This has installed the latest version of underscore from npmjs.org. This module is available under node_modules folder.

Let us verify the installation at location:
~$ cd node_modules
~/node_modules$ ls -l
total 4
drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 24 10:56 underscore
~/node_modules$ 

To use this module. Let us open the node REPL and type the following commands:

~/node_modules$ node
> var _ = require("underscore");
Expression assignment to _ now disabled.
undefined
> console.log(_.max([10,20,30,40]))
40
undefined



If you notice, at the time of installation of underscore - it has given us an error that package.json is not found. To solve this problem, let us setup package.json. We set it up using: npm init 


~$ npm init

This utility will walk you through creating a package.json file.

It only covers the most common items, and tries to guess sensible defaults.



See `npm help json` for definitive documentation on these fields

and exactly what they do.



Use `npm install <pkg>` afterwards to install a package and

save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (ubuntu) 
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /home/mhc/package.json:

{
  "name": "ubuntu",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {},
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes) 
~$

Now, Let us reinstall underscore so that we can examine the contents of package.json:

Step: Clear contents of node_modules folder:

~$ cd node_modules/
~/node_modules$ ls -l
total 4
drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 24 11:19 underscore

~/node_modules$ rm -rf *

Step: Install underscore:


~$ npm install underscore

npm WARN mhc@1.0.0 No description

npm WARN mhc@1.0.0 No repository field.



+ underscore@1.9.1

added 1 package from 1 contributor and audited 1 package in 0.537s

found 0 vulnerabilities


~$

Step: Examine package.json:

~$ more package.json
{
  "name": "ubuntu",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "underscore": "^1.9.1"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

~$ 


Whenever we run npm install , we can use the optional command line argument ( --save ). This will tell npm to store the information of what we installed into package.json file. 


Now, if we perform the same steps using the --save option:

~$ npm install underscore --save
npm WARN mhc@1.0.0 No description
npm WARN mhc@1.0.0 No repository field.

+ underscore@1.9.1
added 1 package from 1 contributor and audited 1 package in 0.596s
found 0 vulnerabilities

~$ more package.json
{
  "name": "ubuntu",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "underscore": "^1.9.1"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

~$ 

Advantages of tracking dependencies this way:
  1. Simply by looking at package.json - we can know which published version of particular library we depend upon.
  2. When we are browsing through the source code of modules developed by others. We can come to know what they are depending upon.
  3. We can refresh the node_modules folder.

Clearing the stuff in node_modules folder and then recreating it:

~$ cd node_modules
~/node_modules$ ls -l
total 4
drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 24 12:03 underscore
~/node_modules$ rm -rf *
~/node_modules$ cd ..
~$ npm install
npm WARN ubuntu@1.0.0 No description
npm WARN ubuntu@1.0.0 No repository field.

added 1 package from 1 contributor and audited 1 package in 0.473s
found 0 vulnerabilities

~$ cd node_modules/
~/node_modules$ ls -l
total 4
drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 24 12:09 underscore
~/node_modules$ 

Having package.json file is a big advantage as we can ship the source code containing package.json without the contents in the node modules. At the client place, we can use npm install to setup the dependencies.



JavaScript - Uninstall and re-Install nodejs and npm in Ubuntu 18.04

Since, I wanted to uninstall and re-install the latest stable version of nodejs. 

Step 1: Remove all node versions and nvm

# Remove nvm
sudo rm -rf ~/.nvm
hash -r

# Remove latest node version
sudo npm uninstall -g n

# Remove n
cd ~/src/n && sudo make uninstall && cd .. && sudo rm -r n

# Remove latest nodejs version
sudo apt-get purge -y nodejs npm

# Remove nodejs-legacy version
sudo apt-get purge -y nodejs-legacy npm

sudo apt -y autoremove

# Remove nodejs files
sudo rm -rf /usr/local/lib/node_modules/npm
sudo rm -rf /usr/local/lib/node_modules/n
sudo rm -f /usr/local/bin/node
sudo rm -f /usr/local/bin/npm
sudo rm -f /usr/bin/node
sudo rm -rf /usr/local/n/versions/node


Let us install using NVM. (Node Version Manager)

Step 2: Download the shell script to install nvm:
$ curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh -o install_nvm.sh

Step 3: Install NVM - using shell script:
$ bash install_nvm.sh
You have $NVM_DIR set to "/home/ubuntu/.nvm", but that directory does not exist. Check your profile files and environment.

Since, it couldn't find the .nvm directory as it got removed during the uninstallation process. Hence, we will manually re-create it.

Step 4: Make directory .nvm: 
$ mkdir .nvm

Hence, Rerunning command in Step 3: 
$ bash install_nvm.sh
=> Downloading nvm from git to '/home/ubuntu/.nvm'
=> Cloning into '/home/ubuntu/.nvm'...
remote: Enumerating objects: 267, done.
remote: Counting objects: 100% (267/267), done.
remote: Compressing objects: 100% (242/242), done.
remote: Total 267 (delta 31), reused 80 (delta 15), pack-reused 0
Receiving objects: 100% (267/267), 119.47 KiB | 344.00 KiB/s, done.
Resolving deltas: 100% (31/31), done.
=> Compressing and cleaning up git repository

=> nvm source string already in /home/ubuntu/.bashrc
=> bash_completion source string already in /home/ubuntu/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
$

Step5 : List down the LTS releases:

$ nvm ls-remote | grep "Latest LTS"
         v4.9.1   (Latest LTS: Argon)
        v6.17.1   (Latest LTS: Boron)
        v8.16.1   (Latest LTS: Carbon)
->     v10.16.3   (Latest LTS: Dubnium)


Step 6: Now let us install Node.js:

$ nvm install 10.16.3
Downloading and installing node v10.16.3...
Downloading https://nodejs.org/dist/v10.16.3/node-v10.16.3-linux-x64.tar.xz...
####################################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v10.16.3 (npm v6.9.0)
Creating default alias: default -> 10.16.3 (-> v10.16.3)

Step 7: Check the Node and NPM Versions:

$ node --version
v10.16.3

$ npm --version
6.9.0



References:


Tuesday, September 3, 2019

Hyderabad - Visit - 002 - KIMS Hospital


Leg 1: Neredmet to Mettuguda




Leg 2: Mettuguda to KIMS



Leg 3: KIMS to Hitech City



Leg 4: Hitech City to Nallagandla