Exercise - Configure the package.json

Completed

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.

  1. Start the process to create a new GitHub Codespace on the main branch of the MicrosoftDocs/node-essentials GitHub repository.

  2. On the Create codespace page, review the codespace configuration settings and then select Create new codespace

    Screenshot of the confirmation screen before creating a new codespace.

  3. Wait for the codespace to start. This startup process can take a few minutes.

  4. Open a new terminal in the codespace.

    Tip

    You can use the main menu to navigate to the Terminal menu option and then select the New Terminal option.

    Screenshot of the codespaces menu option to open a new terminal.

  5. 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.

  6. 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.

  1. In the terminal, change to the folder for this exercise:

    cd node-dependencies/3-exercise-package-json
    
  2. 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
    
  3. 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"
    }
    
  4. 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"
    }
    
  5. In the scripts section, add a new script named start above the test script:

    "start": "node ./src/index.js",
    
  6. Save your changes and close the package.json file.

  7. 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.