The Node.js commands discussed in this post are important for any beginner to create web applications in Node.js.
uninstall
This is the opposite command of install command that was discussed in the previous post on node.js. With install command we could install a package and a folder is created under “node_module” folder in your application folder. The uninstall command is used to uninstall any module that is no longer required in a particular project. This command removes the package mentioned along with the folder created under “node_module” folder.
Syntax
npm uninstall package_name
The example discussed below uninstalls the express package from the current project. You will notice that the express folder created in node_module” folder is removed after the following command.
npm uninstall express
update
The next important command is update command. It is used to update the previously installed package in the web application folder. This is essential if the package that you are using in your application is revised by the third party vendor who provided it to the developer community. This command is used to update a package to the latest version of that module. If the package is already the latest version, no change will be displayed.
Syntax
npm update package_name
In the example below, the express package will be updated with its latest version.
npm update express
ls
This command lists down all the modules/packages installed in the npm. This way you will know that which packages are available to be used and which you still have to update.
Syntax
npm ls
search
search command is typed in the shell to search through package metadata for all files available in the registry of packages.
Syntax
npm search package_name
Example
npm search express
init
init command is used to initialize a node.js app
Syntax
npm init
Creating first app in node.js
Now that you are aware of Node.js commands try this example to understand how to create your first node.js application. Copy the below code in a javascript file (here named app.js) and save it in the directory. This folder also contains the packages to be used further on in later stages of project development.
The process.exit(1) is be used in JavaScipt to exit from the node.js program.
var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Welcome to your first Node.js App'); }) var server = app.listen(8081, function () { var host = server.address().address var port = server.address().port console.log("Example app listening at http://%s:%s", host, port) })
This JavaScript file is executed by using the command in the command line or shell.
node app.js
Open the browser and type http://127.0.0.1:8081/. You will see the following as output.
Be First to Comment