Set up for Node JS coding and Express

PussinSocks·2022년 7월 16일
0

CloneCoding

목록 보기
1/20

Upload the project to git repository

  1. In terminal: git init
  2. Go to github and make a new repository.
  3. In terminal: git remote add origin githubrepositoryURL

This make the git to catch the project we are working on

setup package.json

  1. In terminal: npm init
  2. fill out the form on the terminal, or in the package.json file.

What is package.json?

package.json is a NodeJS file and the file contains information about the developers, the project, and also the packages used.

There are three important objects in package.json:
1. "main"
2. "scripts" : Something (js files) that I want to run
3. "dependencies"

When a package.json is created, there's gonna be an object says "main":"index.js".(Of course it is when you create a file called index.js)

execute js file with node:

node index.js 

But we are not going to use this term a lot because "main" is primary entry point. and this is used when we publish the package and someone wants to install the program.

scripts

So in this case we need to create "scripts".

"scripts" is something we want to run for ourselves.

"scripts":{
	"nameToCall":"node fileName.js"
    },

Difference between the "scripts" and "main" is that it has node in front of the filename. (So fileName.js is watched by NodeJS already?🤔)

and this is how to run on the terminal with npm.
(make sure you call it inside of the folder)

npm run nameToCall

Express

A package to build server.

  • Installing express(and many other packages) through terminal:
npm i express

or

npm install express
  • Make sure you save the package.json or close it before you install express, or version conflict may happen.

When you install express on the project, you will see

  • node_modules folders: Where all the packages you installed with npm is stored
  • package-lock.json: To prevent the modification of the version or the code of packages (Security reason)

dependencies

dependencies indicates the packages which required to run the project

So, when you install the express, there's gonna be an object:

"dependencies":"express": "^4.18.1"

Which tells you the package and the version of the package.
This dependencies information not only tells you what kind of packages are there, but also let you install all the packages when you simply

npm i

or

npm install

npm reads the package.json and reads the dependencies and downloads the exact same version of the packages for you!

Advantage of dependencies:

The advantage of dependencies is that the project becomes very light (using .gitignore).
You don't have to upload or download the heavy project contains all the packages. (Great benefit when you work in a team or in different computer)

Babel: A JavaScript compiler

Babel is a package which translates JavaScript syntax which NodeJS can't understand (new version of syntax) to an old version syntax which NodeJS can understand and process. Link to Babel

Install babel

npm install --save-dev @babel/core

When you install babel packages on in the project, you will see new type of dependencies object in your package.json file.

  "devDependencies": {
    "@babel/core": "^7.18.6",
    }

What is "devDependencies"?

Just like dependencies, it is something is needed. If the dependencies are the packages required to run program, "devDependencies" are the packages which programmers, or the developers needs.

Create babel.config.json (In terminal)

  • Where babel looks in to this file and setup the preset plugin

babel presets

  • *preset is a very big set of plugin. Ex) Babel plugin for javaScript, React, TypeScript, etc.
    babel looks at the config file and plugin presets installed runs

Babel node

  • To use babel from the script, not from JavaScript.

Nodemon

A package will watch javaScript files and if Nodemon finds the edit from the file, it will automatically restart. (Never let my console die.)

"scripts":{
	"node index.js"
	}

Runs index.js with NodeJS.

"scripts":{
	"babel-node index.js"
	}

This makes the babel to run the NodeJS.

"scripts":{
	"nodemon --exec babel-node index.js"
	}

nodemon executes "babel-node index.js"

In conclusion,

  • NodeJS package.json file is like a hub, where all the packages and info and files are managed and controlled.
  • Express.js packages to build server and goes in the dependencies.
  • Babel and Nodemon packages are installed to make efficient programming environment for developers.(devDependencies)
profile
On a journey to be a front-end developer

0개의 댓글