Exercise - Configure the package.json
You're a Node.js developer at Tailwind Traders. Knowing how to set up a new Node.js project is an important skill to have. Setup includes generating a package.json
file and creating some common scripts to use throughout the project lifecycle.
Open project in development container
A simple development environment has been provided for you. If you already have Node.js LTS installed on your computer, you can skip this section and clone the sample repository and use your local environment.
Start the process to create a new GitHub Codespace on the
main
branch of theMicrosoftDocs/node-essentials
GitHub repository.On the Create codespace page, review the codespace configuration settings and then select Create new codespace
Wait for the codespace to start. This startup process can take a few minutes.
Open a new terminal in the codespace.
Validate that Node.js is installed in your environment:
node --version
The dev container uses a Node.js LTS version such as
v20.5.1
. The exact version might be different.The remaining exercises in this project take place in the context of this development container.
Set up a new Node.js project
For this unit, the JavaScript source code has been provided for you. Your job is to create the package.json
file.
In the terminal, change to the folder for this exercise:
cd node-dependencies/3-exercise-package-json
View the contents of the folder:
ls -R
In this folder, you should see a src subfolder with an index.js file:
./src: index.js
Run the following command to create the
package.json
file with default values:npm init -y
The package.json file that looks similar to this example:
{ "name": "3-exercise-package-json", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }
Modify the
package.json
with these property values:name
: "tailwind-trader-api"description
: "HTTP API to manage items from the Tailwind Traders database"main
: "index.js"keywords
: ["API", "database"]author
: "Sam"
Your package.json file should now look like this code:
{ "name": "tailwind-trader-api", "version": "1.0.0", "description": "HTTP API to manage items from the Tailwind Traders database", "main": "index.js", "dependencies": {}, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": ["api", "database"], "author": "Sam", "license": "ISC" }
In the
scripts
section, add a new script namedstart
above thetest
script:"start": "node ./src/index.js",
Save your changes and close the package.json file.
Start your project with the
start
action by entering this command:npm start
You should see this output:
Welcome to this application
You now have a good package.json
file that you can build upon as your project grows.