node --version
-
Make a new Directory
mkdir coding-train-node-demo
-
Opening / Changing Directories
cd coding-train-node-demo
- open the coding-train-node-demo directory
.
- this current directory
..
- go back up a level
code .
- open up the VSC editor
npm init
The utility will walk you some questions--you can enter answers or just say yes.
The File System package can be used to access local files on your computer, and is part of node system.
-
CommonJS module
const fs = require('fs');
-
ES module
import * as fs from 'fs'
Note: to load an ES module you must edit the package.json file to set "type": "module"
Some package I will be using:
npm install cowsay
or npm i cowsay
This will add the package to the package.json as a dependencies. It will also add a new directory called node_modules.
- When was it created?
- Is it actively maintained?
- Look at Github repo - are there any recent commits?
- cowsay repo
-
Importing everything from a package
import * as cowsay from 'cowsay'
-
Importing individual functions from a package
import { say } from 'cowsay'
You must install the dependencies for the project.
npm install
Livestream discussing installation of the Coding Train website
When updating the website, it is helpful to run the website locally in order to quickly see the changes made. Make sure you have Node.js version 18.x
installed and then perform the following steps:
- Clone down the repo to your computer and
cd
into the folder - Run
npm install
- Run
npm run dev
This will start a local server and the website will now be accessible on localhost:8000. Changes to most files will be auto-updated in the browser while the website is running.
Note: In some cases, specially when updating loaded content, you need to restart the local server with npm run dev
.
And in some cases that may fail because of Gatsby's cache usage. If you run npm run clean
before npm run dev
, that should fix it.