Installing module global or local using npm
More than anything else, the driving force behind the npm 1.0 rearchitecture was the desire to simplify what a package installation directory structure looks like.
In npm 0.x, there was a command called
Also, there was that activation/deactivation thing. That’s confusing.
Just like how global variables are kind of gross, but also necessary in some cases, global packages are important, but best avoided if not needed.
In general, the rule of thumb is:
You can probably think of other ways to do it by messing with environment variables. But I don’t recommend those ways. Go with the grain.
If your package's
To update a node.js in windows download the latest version and install it.
node -v or node --version
npm -version
npm update -g
To update npm use the below command
npm i -g npm
https://www.npmjs.com --> this mentions the library which are available for node
When we install the npm in global we will have them loaded
C:\Users\ptummala\AppData\Roaming\npm\node_modules
To install typescript
npm install -g typescript
To install /update the latest version of typescript
npm install -g typescript@latest
to look for type script version
tsc -v
If angular cli is not matching with global in project then use the below commands
To known the angular cli version
ng --version or ng -version or ng -v
npm install --save-dev @angular/cli@latest
To install/update the latest verison of angular
npm install -g @angular/cli@latest
To know the latest version of Angular give
If ng command is not working after angular was installed try to look at the below options by running one after the other
a. Path variable :
Set the path path variable to have below path
c:\users\ptummala\AppData\Roaming\npm
b. Run the below commadn to clean the cache
npm cache clean
c. Run the link command
npm link @angular/cli
Command
npm audit
npm audit fix
To install node modules
https://atmos.washington.edu/~nbren12/reports/journal/2018-07-16-NN-conservation/node_modules/npm/html/doc/cli/npm-install.html
npm 1.0: Global vs Local installation
npm 1.0 is in release candidate mode. Go get it!More than anything else, the driving force behind the npm 1.0 rearchitecture was the desire to simplify what a package installation directory structure looks like.
In npm 0.x, there was a command called
bundle that a lot of people liked. bundle let you install your dependencies locally in your project, but even still, it was basically a hack that never really worked very reliably.Also, there was that activation/deactivation thing. That’s confusing.
Two paths
In npm 1.0, there are two ways to install things:- globally —- This drops modules in
{prefix}/lib/node_modules, and puts executable files in{prefix}/bin, where{prefix}is usually something like/usr/local. It also installs man pages in{prefix}/share/man, if they’re supplied. - locally —- This installs your package in the current working directory. Node modules go in
./node_modules, executables go in./node_modules/.bin/, and man pages aren’t installed at all.
Note: When I installed in my ubtunu it has used the {prefix} as /usr/
Which to choose
Whether to install a package globally or locally depends on theglobal config, which is aliased to the -g command line switch.Just like how global variables are kind of gross, but also necessary in some cases, global packages are important, but best avoided if not needed.
In general, the rule of thumb is:
- If you’re installing something that you want to use in your program, using
require('whatever'), then install it locally, at the root of your project. - If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your
PATHenvironment variable.
When you can't choose
Of course, there are some cases where you want to do both. Coffee-script and Express both are good examples of apps that have a command line interface, as well as a library. In those cases, you can do one of the following:- Install it in both places. Seriously, are you that short on disk space? It’s fine, really. They’re tiny JavaScript programs.
- Install it globally, and then
npm link coffee-scriptornpm link express(if you’re on a platform that supports symbolic links.) Then you only need to update the global copy to update all the symlinks as well.
npm link in a future installment.)You can probably think of other ways to do it by messing with environment variables. But I don’t recommend those ways. Go with the grain.
Slight exception: It’s not always the cwd.
Let’s say you do something like this:cd ~/projects/foo # go into my project
npm install express # ./node_modules/express
cd lib/utils # move around in there
vim some-thing.js # edit some stuff, work work work
npm install redis # ./lib/utils/node_modules/redis!? ew.
In this case, npm will install redis into ~/projects/foo/node_modules/redis. Sort of like how git will work anywhere within a git repository, npm will work anywhere within a package, defined by having a node_modules folder.Test runners and stuff
If your package's
scripts.test command uses a command-line program installed by one of your dependencies, not to worry. npm makes ./node_modules/.bin the first entry in the PATH environment variable when running any lifecycle scripts, so this will work fine, even if your program is not globally installed:{ "name" : "my-program"
, "version" : "1.2.3"
, "dependencies": { "express": "*", "coffee-script": "*" }
, "devDependencies": { "vows": "*" }
, "scripts":
{ "test": "vows test/*.js"
, "preinstall": "cake build" } }
To update a node.js in windows download the latest version and install it.
node -v or node --version
npm -version
npm update -g
To update npm use the below command
npm i -g npm
https://www.npmjs.com --> this mentions the library which are available for node
When we install the npm in global we will have them loaded
C:\Users\ptummala\AppData\Roaming\npm\node_modules
To install typescript
npm install -g typescript
To install /update the latest version of typescript
npm install -g typescript@latest
to look for type script version
tsc -v
If angular cli is not matching with global in project then use the below commands
To known the angular cli version
ng --version or ng -version or ng -v
npm install --save-dev @angular/cli@latest
To install/update the latest verison of angular
npm install -g @angular/cli@latest
To know the latest version of Angular give
If ng command is not working after angular was installed try to look at the below options by running one after the other
a. Path variable :
Set the path path variable to have below path
c:\users\ptummala\AppData\Roaming\npm
b. Run the below commadn to clean the cache
npm cache clean
c. Run the link command
npm link @angular/cli
Command
npm audit
npm audit fix
To install node modules
https://atmos.washington.edu/~nbren12/reports/journal/2018-07-16-NN-conservation/node_modules/npm/html/doc/cli/npm-install.html
If package is not getting installed then to check dependency
npm ls <<node-module>>
npm ls node-sass
to install the dependency check the below
npm install node-sass@4.12.0
NPM modules we have
a. Custom modules - which are developed by us and used in our project
b. third party modules - which are developed by others and used from npm and others can also use
c. Core Modules - These are present as part of Node
http vs express
https://www.geeksforgeeks.org/what-are-the-differences-between-http-module-and-express-js-module/
https://www.quora.com/What-is-the-difference-between-Express-and-the-HTTP-module-in-Node-js
Alternative for ExpressJS
https://blog.back4app.com/expressjs-alternatives/#:~:text=This%20article%20will%20explore%20ten,js%20ecosystem.
To create a packages.json then we will use the below command
npm init
To invoke powershell in nodejs refer the below article but when i tried it did not work
https://www.jeansnyman.com/posts/executing-powershell-commands-in-a-nodejs-api/
https://newbedev.com/execute-powershell-script-from-node-js
Callback in javascript
https://www.geeksforgeeks.org/how-to-create-a-custom-callback-in-javascript/#:~:text=A%20custom%20callback%20function%20can,passed%20is%20actually%20a%20function.&text=console.,-log(%20%22This%20is
require
to import a module/package we use the keyword require
var fs = require("fs");
var options = {encoding: 'utf8', flag: 'r'};
//Callback with functionname
fs.readFile('./data.txt',options,sampleasync);
//callback with anonymous function
fs.readFile('./data.txt',options,function(err,fileData){
if(err){
console.log("File not found"+err)
}else{
console.log(fileData)
}
endprogram();
});
//callback with anonymous function
fs.readFile('./data.txt',options,(err,fileData)=>{
if(err){
console.log("File not found"+err)
}else{
console.log(fileData)
}
endprogram();
});
console.log("Program ends");
function sampleasync(err,fileData){
if(err){
console.log("File not found"+err);
}else{
console.log("from sample data : "+fileData);
}
endprogram();
}
function endprogram(){
console.log("End of read")
}