Complete beginner guide to get started with NodeJs.
-
Create a new project folder in your file directory and name it node_basic
-
Open that project folder in VS Code (recommended Editor). To open the project folder you can do either of the following ways.
-
Go inside project folder. Click right click outside file and folder holding 'Left Shift Key' and click on 'Open Powershell window here' or 'Open Linux shell here' or 'Git Bash here' depending on shell you have. Inside the shell in project directory, give command as shown below to open projects in VS Code.
code . -
Simply, drag and drop project folder in VS Code open the project.
-
-
Till Now, no any files and folders are created. We will created necessary file and folders to start the project. Before that make sure that you already had installed latest stable version of Node.
- You can check whether node installed with command below in the terminal
node //if installed, shows Welcome to NodeJs messageIn Run (window) open terminal of nodeJs typing "node.exe" in the box.
In the terminal check Node Version.
node -vExit from Node CLI
process.exit()or Ctrl+D or Ctrl+C(twice) or type ".exist".
Now, lets get back to VS Code and in terminal there inside project folder. Begin node project with the code:
npm init -y'npm init' command allows to set package naeme, version, description and other information about application. Whereas 'npm init -y' allows to take default values and save. npm instand for 'Node Package Manager' while '-y' stands for yes.
This creates new file package.json.
-
Create a index script file and name it either 'app.js' or 'index.js' in project root directory (outside of all folders) (* no any folder should exist till now in project folder).
-
Index file is referred as
index.jsfile here after. Addconsole.loga line of code inindex.jsand save it and run the code. The code is:console.log("Test");Run the code saving the file. In the terminal type code:
node index.jsnode index.jsUpon running it prints
Test.