본문 바로가기
TIL: Today I Learned

[TIL] 20201215 The Terminal / Our First Brush with Node / Exploring Modules & The NPM

by 김알리 2020. 12. 15.

Terminal

Terminal

  • Text-based prompt where the user can interact with the machine.
  • Originally, used to be a physical object. Now we use software terminals.

Why Use Terminal

  • Using terminal takes less time than using GUI once you get used to it.
  • Terminal provides a "mainline" into the heart of the computer, giving us access to areas we normally don't interact with.
  • Many of the tools we need are installed and used via the command line. (We don't have a choice but to do it.)

Shell

  • The program running on the terminal.
  • Bash : One of the most popular shells.

Terminal commands

  • ls : Lists the content of the current directory
  • ls -a : Lists the content of the current directory, including hidden files.
  • pwd : Print Working Directory. Prints the path to the working directory. (When you currently are)
  • cd : Change and move between directories. Followed by a folder name, takes us into the folder.
  • cd .. : Go back one directory
  • clear : Clear the console. Ctrl+K also works.
  • mkdir : Make Directory. Separate different folder names with space to make multiple files at once.
  • man : Short for manual. Type q to escape.
  • touch : Creates a file.
  • rm : The file disappears forever. No trashcan.
  • rmdir : Only removes empty folders.
  • rm -rf : Removes directory even when it has contents inside.

Relative vs. Absolute Paths

  • Absolute Path
    • Start with '/' (root directory)
    • Write down the path to the target directory.
  • Relative Path
    • Relative to where I currently am.

Flags

  • Similar to arguments in functions.
  • Some sort of options.
  • Can combine multiple flags and use at the same time.

 

 

 

Node.js

  • A  JavaScript runtime that executes code outside of the browser.
  • Makes it possible to write server-side code, instead of relying on other languages like Python or Ruby.
  • What to build with Node
    • Web Servers
    • Command Line Tools
    • Native Apps
    • Vidoe Games
    • Drone Softwares, etc.

Node Basics

  • How to get out of Node 
    • Press Ctrl+C twice
    • Press Ctrl+D
    • Type .exit
  • REPL
    • Read Evaluate Print Loop
    • Waits the user to type something in so that it can evaluate it back.
    • Typically used for debugging or testing new features.
  • Node.js vs. Client-side JavaScript
    • Because Node doesn't run in browser, we don't have access to all the browser things. (i.e. window, document, DOM APIs, etc)
    • Node comes with a bunch of built=in modules that don't exist in the browser. These modules help us do things like interacting with the operating system and files/folders.
  • Running Node Files on Terminal
    • node file.js 

Process

  • An object (global scope)
  • Provides information about and control over the current Node.js process.
  • Properties
    • version : tells the version of Node
    • cwd() : current working directory
    • argv
      • Returns an array containing the command line arguments passed when the Node.js process was launched. (in terminal : node file.js arguments)
      • Has 2 default elements.
        1. The absolute pathname of the executable that started the Node.js process.
        2. The path to the JavaScript file being executed.
const arg = process.argv[2];

//arg is the argument that came from node.

File System Module (fs)

  • All file system operations have synchronous & asynchronous forms
    • Asynchronous : always takes a completion callback as its last argument.
    • Synchronous : Exceptions using synchronous operations are thrown immediately and maybe handled using try/catch. Will block the entire process until they complete, halting all connections.
  • To use modules 
const fs = require('fs')
  • Methods
    • mkdir() : make directory in the current directory.
    • writeFile() : writes data to the file. Replaces the file if it exists. If it doesn't, creates one.
    • Example
const fs = require('fs');
const folderName = process.argv[2] || 'Project'; //'Project' is the default value

fs.mkdir(folderName);
fs.writeFile(`${folderName}/index.html`);
fs.writeFile(`${folderName}/app.js`);
fs.writeFile(`${folderName}/styles.css`);

 

 

 

module.exports

  • Allows other JS files to access
  • Shorthand : module.exports => exports
  • Example
const add = (x, y) => x+y;
const PI = 3.14159;

//Making function add and constant PI are available in other files, using require.

// exports one thing at a time
module.exports.add = add;
module.exports.PI;


//or, define and exports at the same time 
module.exports.add = (x,y) => x+y;
module.exports.PI = 3.14159;


// exports all at once
const math = {
    add : add,
    PI : PI
}
module.exports = math;
  • How to require
const math = require('./math');
math.PI; //3.14159

// './' is used for local files.

How to require a directory

1. Make an index.js file in the directory

2. In index.js, require all the other files in the directory

3. Exports everything in index.js

4. To use, require the whole directory

// dir/shelter/blue.js
module.exports = {
    name: blue,
    age: 2
}
// dir/shelter/janet.js
module.exports = {
    name: janet,
    age: 5
}
// dir/shelter/sadie.js
module.exports = {
    name: sadie,
    age: 1
}

// dir/shelter/index.js
const blue = require('./blue');
const sadie = require('./sadie');
const janet = require('./janet');
const allCats = [blue, sadie, janet];
module.exports = allCats;


// dir/app.js
const cats = require('./shelter');

 

 

 

NPM

  • Node Package Manager
  • A library of thousands of packages published by other developers.
  • A command line tool to easily install and manage those package in our Node projects.

How to install a package

1. On npm webpage, find a package and type the 'install' command on the terminal.

2. Require the package at the JS file

Local vs. Global installation of packages

  • Local
    • Installing the package in a directory, by installing through terminal. Better most of the times, because different versions of packages can't run together.
  • Global
    • Can be accessed globally (everywhere), doesn't have to be required.
    • Add -g flag (ex: npm install -g cowsay)

package.json

  • Contains meta-data about the package.
  • dependencies
    • Some dependencies need specific version, or around the version.
    • Acts as a record of every package being used in this application.
  • How to create : npm init
  • Typically included in the root directory

node-modules folder

  • Don't share with this with others : too big
  • Instead, with terminal, inside of the package folder, run command 'npm install'. Then all the dependencies get installed inside of the node-modules folder that was newly created.

 

 

 

 

 

* This post is a summary of Udemy Course "The Web Developer Bootcamp" by Colt Steele.