Create a Work Directory
Create a directory on your laptop to hold the files for this assignment. Then start VSC and on the VSC start page select Open and select the new directory that you created.
Start a Node project
Open the VSC terminal (CTRL + `) and print your working directory (using the pwd
command) to make sure your working directory is your project’s root directory. If not, change your working directory using the cd
command.
The node package manager (npm) “is the package manager for the Node JavaScript platform. It puts modules in place so that node can find them, and manages dependency conflicts intelligently [ref].” You can view a list of npm options by running npm help or by visiting the npm commands web page.
Create a new node project by running the command shown below. When you do, you will be presented with a series of prompts for information. Press enter to accept all of the default values except for type. When prompted to enter the type, enter module
. Press enter when asked Is this OK?
$ npm init
When you run npm init
, it creates a file named package.json. Open the file and view it’s contents. This file contains information needed by the node runtime to run the project’s code.
Install npm Packages
An npm package is a library of code that we can install in our project and then import and use in our code. You can search the npm registry for freely available packages at https://www.npmjs.com.
The npm install (or the shorter npm i) command downloads and installs a package (and any packages that it is dependent on) into the current project.
Run the following commands from the VSC terminal to install a set of packages in your project.
$ npm i nodemon --save-dev
After you’ve run the commands you should notice that a new subdirectory named node_modules was created in your project. If you expand node_modules you will see that the package we requested was installed in it. What are all of the other packages? They are the packages that are dependencies of the package we requested. Open up a few and explore their code.
If you open up package.json you should see a new property; devDependencies. The devDependencies property lists the packages that are needed only when our program is run during development. If we were to deploy our project to a production environment, nodemon and its dependencies would not be installed. The –save-dev flag informs npm to add the package to the devDependencies list.
We can also have a dependencies property in package.json. This property lists the packages that are always needed, whether in a development environment or a production environment. We’ll add packages in this list later.
These lists are useful when installing the project on another computer. Instead of copying all of the code in the node_modules directory to the remote computer we need only copy the package.json file and request npm to install all of the modules that are in the dependency lists.
Take some time to search the npm registry to see what sort of modules are available.
Create a JavaScript File
Create a file named index.js and add a console.log
statement in the file.
console.log("Good boy!")
Run index.js from the Command Line
In the console we can run the code in index.js by using the node command followed by the name of the file. Enter the following command to run the code in index.js.
$ node index.js
Modify Scripts in package.json
package.json can also contain scripts that can be run from the command line. Currently there is a script named “test” in package.json. We can run a script by entering npm run followed by the name of the script on the command line. For example:
$ npm run test
Try it. As you see it simply prints to the console ‘Error: no test specified’.
Delete the test script and create a new script named start that will run index.js using node, and a script named dev that will run index.js using nodemon.
Modify the scripts property in the package.json file so that it reads as follows:
... "scripts": { "start": "node index.js", "dev": "nodemon index.js" }, ...
Now run the start script by entering the following terminal command:
$ npm run start
This is the command that would be run in a production environment to start our node program. You’ll notice that when you run the start script the code in index.js is run and then the script terminates.
Now run the dev script by entering the following command in the terminal:
$ npm run dev
Now, you’ll notice that the index.js script is run by nodemon, but nodemon does not terminate. It is still running. In the terminal you’ll notice that nodemon is waiting for you to make changes to your source code files.
Modify index.js and save the file.
As you see, nodemon re-runs the code in index.js after each save. This is helpful when our project is in development – we don’t have to manually re-run our code after making changes – the code is re-run automatically.
Experiment with JavaScript
Now you’re ready to practice and write code in index.js.