Home
ⓢⓐⓨⓔⓕ'𝓼 𝓽𝓮𝓬𝓱 𝓫𝓵𝓸𝓰
About
Hello World Project with NodeJS
Published
2018-07-16 15:02
Tagged on
hello-world
nodejs
## Install Node.JS - Download node.js from nodejs.org. - Unpack anywhere you prefer. - Export path variable. - Open .bashrc file to export path variable (I prefer this way! :P) ``` $ gedit .bashrc ``` - Add the following lines to the .bashrc file. ``` export NODE_HOME="/home/sayef/node-4.5.0" export PATH=${NODE_HOME}/bin:$PATH ``` - Save, exit and source the .bashrc file to come into effect the changes. ``` $ source .bashrc ``` ## Install NPM (node package manager) NPM is a command line interface program to manage node.js libraries. We can install some other fantastic and handy libraries to work with nodejs. We can initiate a server, see the changed effects in browser using those npm packages and making lot more funs with this package manager. Following command will install npm. ``` $ sudo apt-get install npm ``` We can check our node version and npm version using ``` $node -v v4.5.0 $npm -v 2.15.9 ``` ## Create NodeJS Project 1. Create a preferred directory. In my case ``` $ mkdir /home/sayef/Dev/WebProjects/NodeProjects/hello ``` 2. Go to the directory and initialize npm there- ``` $ npm init ``` 3. A lot of stuffs will be showing in the screen. Just hit through down and will be ended up by saying Is this ok? (yes). This will create package.json file for us with preloaded some environment settings in the directory. Lets check it- ``` $ ls package.json ``` 4. Now we just want to create some javascript file, which is definite motivation. So create a javascript file named index.js in the same directory and write up some js stuffs inside the file. We can use atom or sublime text, two best text editor in this regard. ``` console.log("Hi There!"); ``` 5. Introduce this index.js file with node so that we can further command over this js file through node commands and other handy stuffs. ``` $ node index.js ``` 6. To get a quick client side setup, we need two basic libraries, budo and watchify. Also if we want to save this two libraries for further dependencies, we just add save-dev option which will update package.json. ``` $ npm install budo watchify --save-dev ``` 7. After reloading (if needed) the package.json file we will see something like that end of the file- ``` "devDependencies": { "budo": "^9.2.0", "watchify": "^3.7.0" } ``` So next time we will just write npm install and this two packages will be loaded automatically and will not need to specify explicitly using npm commands. 8. We have a scripts section in the package.json file like following- ``` "scripts": { "start": "beefy index.js 8080 -- -d", "test": "grunt nodeunit -v" } ``` This section automates things while loading the project. We can add budo commands so that we can see the changes live in the browser. It will set up the server while we load the project and will be available to watch from browser. Update the scripts section like following- ``` "scripts": { "start": "budo index.js --live" } ``` Make some changes in index.js- ``` document.write("Hello World!"); ``` 9. After saving the package.json file just go to the terminal and write ``` $ npm start ``` And it will start up a little server on port 9966. If we go to browser and hitting localhost:9966 we can see the effects of index.js file changes. So we are done! We can write some javascript codes and deploy in a little server and see the effects in the browser.
SHARE THIS POST
Facebook
Twitter
Google+
Related Posts
Creating Desktop App With Electron
-
2018-07-16 15:32