9.3 Using Node.js
New Material
Running Node (potentially on your own computer) lets you build and test a full stack application easily before pushing your files to a server.
Install Node
Section titled “Install Node”This tutorial covers a basic install process for Node.js. You will run these in your Terminal program.
brew install node- Confirm installation
node -v# -> v24.7.0npm -v# -> 11.5.1Open Node interactive shell
Section titled “Open Node interactive shell”Test Javascript in Node’s interactive shell.
- Type
nodeon the command line and press return to open the interactive shell. - At the
>prompt, type1+1and press return. Node will evaluate your expression and return the result.
> 1+12- Node is just Javascript, so almost any code will run.
- Cancel any process in the Terminal using
Ctl+c
Run a script with Node
Section titled “Run a script with Node”Inside an empty Codespaces project…
- Create a file named
index.jsand paste the below code.
let greeting = "Hello, from Node";console.log(greeting);- In the Terminal, run the script with:
node index.js- Create a new folder named:
cron-demoand open it in VS Code (in Mac you can just drag the folder from the finder to the VS Code icon in the dock) - Create a file in this folder named
index.jsand paste the below code.
let greeting = "Hello, from Node";console.log(greeting);- In the Terminal, run the script with:
node index.jsInitialize a new project with NPM
Section titled “Initialize a new project with NPM”Every Node.js project has a package.json to store project dependencies and other metadata.
Inside an empty Codespaces project…
Inside the cron-demo in VS Code (created above)
- Open the Terminal in your editor.
- Run
npm initto initialize a new project. Press return at each prompt. This creates apackage.jsonfile with data you entered. - Open your new
package.jsonfile. Confirm themainfield is set toindex.js. - Add a new sub-property under
scripts. Add a comma after thetestproperty, then add"start": "node index.js"below it (see outcome below). - Run
npm run startwhich runs the command stored instart.
Outcome
{ "name": "cron-demo", "version": "1.0.0", "description": "", "license": "ISC", "author": "", "type": "commonjs", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node index.js" }}Install Nodemon using NPM
Section titled “Install Nodemon using NPM”Nodemon is an NPM package that automatically restarts your node application when it detects file changes in your the project directory.
- Install the nodemon package globally
npm install -g nodemon(the-gflag makes it global) - Run
index.jsusing nodemon (instead of node)nodemon index.js - Edit and save
index.js. Nodemon will restart your script each time you save.
[nodemon] restarting due to changes...[nodemon] starting `node index.js`- Exit the process with
Ctl+C(this exits any process on the Terminal).
Use the Cron package
Section titled “Use the Cron package”Cron is an NPM package that executes scripts on a schedule (e.g. each/second, once/day) or at particular times. Use it to automate any common task like database backups, etc..
- Run
npm i cron --savein the Terminal (iis a shortcut forinstall,--saveadds a dependency to your project’spackage.json) - In
package.jsona new dependency has been added, as well as a newnode_modulesfolder, where the cron package and packages it uses was just installed. - Add the code below to your
index.jsfile.
// require the packagevar CronJob = require('cron').CronJob;// create a new instancevar job = new CronJob( '* * * * * *', function() { console.log('The local time is: '+ new Date().toLocaleString()); }, null, true, 'America/New_York');- Run it with
nodemon index.jsin the Terminal. - Use crontab.guru to experiment with the time.