Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 40 ยท Projects and Practice

Project: Set Up a Node.js Workspace

Build a complete VS Code setup for a Node.js project: settings, extensions, tasks, debugging, format on save and snippets, then check it with a script.

In this project you will put the earlier lessons together by configuring a small Node.js project so that a new teammate gets a good setup the moment the folder opens. You will create a .vscode folder containing settings, recommended extensions, tasks, a debug configuration and a snippet, and a .editorconfig.

The project cannot run inside VS Code on this site, so the files are shown in full for you to create on your computer. A final stand-in script checks a workspace file list against a checklist, so you can practice the idea of an automated "does the setup look complete?" check.

What you will build

TEXT
my-app/
  .editorconfig
  .prettierrc
  package.json
  src/
    index.js
  .vscode/
    extensions.json
    settings.json
    tasks.json
    launch.json
    app.code-snippets

Everything in .vscode is committed to Git, except personal files you decide to ignore. Create a folder, run npm init -y in the integrated terminal and open it in VS Code.

Step 1: a tiny app

src/index.js:

JavaScript
function greet(name) {
  return `Hello, ${name}!`;
}

const people = ["Ada", "Grace", "Linus"];
for (const person of people) {
  console.log(greet(person));
}

In package.json add scripts:

JSON
{
  "scripts": {
    "start": "node src/index.js",
    "test": "node --test",
    "lint": "eslint src"
  }
}

(eslint needs to be installed first with npm install -D eslint; skip the script if you do not want it.)

Step 2: shared editor rules

.editorconfig makes every editor agree:

TEXT
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

.prettierrc:

JSON
{ "singleQuote": true, "semi": true }

.vscode/extensions.json, as explained in essential extensions:

JSON
{
  "recommendations": [
    "editorconfig.editorconfig",
    "esbenp.prettier-vscode",
    "dbaeumer.vscode-eslint"
  ]
}

Step 4: workspace settings

.vscode/settings.json, using what you learned in formatters and linters:

JSON
{
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.codeActionsOnSave": { "source.fixAll.eslint": "explicit" },
  "files.exclude": { "**/.DS_Store": true },
  "search.exclude": { "**/node_modules": true, "**/dist": true },
  "files.eol": "\n",
  "terminal.integrated.defaultLocation": "view"
}

Step 5: tasks

.vscode/tasks.json (see tasks and tasks.json):

JSON
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "start",
      "type": "shell",
      "command": "npm start",
      "problemMatcher": []
    },
    {
      "label": "test",
      "type": "shell",
      "command": "npm test",
      "group": { "kind": "test", "isDefault": true },
      "problemMatcher": []
    },
    {
      "label": "check",
      "dependsOn": ["test"],
      "dependsOrder": "sequence",
      "group": { "kind": "build", "isDefault": true },
      "problemMatcher": []
    }
  ]
}

Ctrl+Shift+B now runs the check task, which runs the tests.

Step 6: debugging

.vscode/launch.json starts the program under the debugger. Press F5 to run it, and click in the gutter to set a breakpoint at the return line in greet.

JSON
{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Run index.js",
      "program": "${workspaceFolder}/src/index.js",
      "skipFiles": ["<node_internals>/**"],
      "console": "integratedTerminal"
    }
  ]
}

Step 7: a snippet

.vscode/app.code-snippets (snippets):

JSON
{
  "Node test": {
    "scope": "javascript",
    "prefix": "ntest",
    "body": [
      "const test = require('node:test');",
      "const assert = require('node:assert');",
      "",
      "test('${1:does something}', () => {",
      "  assert.strictEqual(${2:actual}, ${3:expected});",
      "});"
    ]
  }
}

Step 8: try the loop

  1. Reopen the folder: VS Code offers the recommended extensions.
  2. Mess up the formatting of index.js and save: it should be reformatted.
  3. Add a file src/greet.test.js, type ntest and press Tab.
  4. Run the test task from Terminal > Run Test Task.
  5. Press F5 and step through the loop with F10 (step over) and F11 (step into).
  6. Commit the .vscode folder from Source Control.

Check your work with a script

The stand-in below treats the project as a list of paths and verifies the checklist. In real life you might run something similar in a script or a CI job.

JS Stand-in: workspace checklist

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: validate JSON settings with comments

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file makes VS Code offer extensions to everyone who opens the project?

    extensions.json lists recommended extensions.
  2. 2. Which key starts the debugger using launch.json?

    F5 starts debugging with the selected configuration; Ctrl+F5 runs without debugging.
  3. 3. Why commit the .vscode folder?

    Shared configuration gives every teammate the same working setup.
  4. 4. What is the role of .editorconfig?

    .editorconfig is read by many editors and defines basic formatting rules.

Summary

  • A good project setup lives in the repository: .vscode files, .editorconfig and a formatter config.
  • Extensions, settings, tasks, debugging and snippets work together to make the first run smooth.
  • Verify the setup by reopening the folder as a newcomer would.
  • Automate checks of the setup when the team grows.

Next: Project: Team Configuration and Onboarding