DEV Community

Cover image for Setting Up an Angular Development Environment: A Comprehensive Guide
josematoswork
josematoswork

Posted on • Updated on • Originally published at angulardive.com

Setting Up an Angular Development Environment: A Comprehensive Guide

Setting Up an Angular Development Environment: A Comprehensive Guide

Angular is one of the most popular front-end JavaScript frameworks, and with good reason. It allows developers to write structured and maintainable code, and provides a wide range of out-of-the-box tools and features. But in order to take advantage of all that Angular has to offer, you need to have a solid development environment in place.

In this article, we’ll walk you through the process of setting up your Angular development environment from scratch, covering everything from installing the necessary software to getting your first Angular project up and running.

Prerequisites

Before we dive into the actual setup process, it’s important to make sure you have all the necessary tools and software installed on your computer.

Here’s what you’ll need:

  • Node.js v14.x or later

  • npm v7.x or later

  • Angular CLI v12.x or later

  • A text editor of your choice (we recommend Visual Studio Code)

If you don’t already have these tools installed, you can download them from their respective websites and follow the installation instructions.

Setting Up the Angular CLI

The Angular CLI is a command-line interface that provides a wide range of tools and features for developing Angular applications. It allows you to easily create new projects, generate components and other code files, and run various development and testing commands.

To install the Angular CLI, open your terminal or command prompt and run the following command:

npm install -g @angular/cli

This will install the latest version of the Angular CLI globally on your computer.

Creating a New Angular Project

With the Angular CLI installed, you can create a new Angular project with just one command:

ng new my-app

This will create a new Angular project called "my-app" in your current working directory. The process may take several minutes, depending on your internet connection and system resources.

Once the project is created, navigate to its directory by running:

cd my-app

Now you can run the project by running:

ng serve

This will start a development server and open your web browser to display the default Angular app page at "http://localhost:4200".

Understanding the Project Structure

Before we move on to writing actual code, let’s take a quick look at the structure of our new project.

The root directory of the project contains several important files and folders:

  • e2e/: This folder contains end-to-end (e2e) tests for your Angular app. You can ignore this folder for now.

  • node_modules/: This folder contains all the third-party packages installed by npm, including Angular itself and its dependencies.

  • src/: This is the main folder where you’ll be writing your Angular code. It contains subfolders for your app’s components, assets, styles, and more.

  • .editorconfig: This file contains configuration settings for your text editor, such as indentation style and character encoding.

  • .gitignore: This file contains a list of files and folders that should be excluded from version control (e.g. temporary build files).

  • angular.json: This file contains configuration settings for the Angular CLI, such as build options, testing settings, and default project settings.

  • package.json: This file contains metadata about your Angular app, such as its name, version, and dependencies.

  • README.md: This file contains a brief introduction and documentation for your Angular app.

  • tsconfig.json: This file contains configuration settings for the TypeScript compiler, such as output directory and target version.

For now, the most important folder for us is the "src" folder, which contains the source code for our Angular app. Inside the "src" folder, you’ll find several additional folders:

  • app/: This folder contains the main component for our Angular app, as well as any other components, services, or modules we create.

  • assets/: This folder contains any additional assets used by our app, such as images or fonts.

  • environments/: This folder contains environment-specific settings, such as API endpoints or database credentials.

  • styles/: This folder contains global stylesheets for our app, as well as any custom CSS we create.

  • index.html: This file is the main HTML file for our app, and contains our app’s root component.

Writing Your First Angular Component

Now that we have our development environment set up and our initial project created, it’s time to start writing some code!

Let’s create our first Angular component by running the following command:

ng generate component hello-world

This will generate a new component called "hello-world" inside the "src/app" folder. The Angular CLI will automatically create the necessary TypeScript, HTML, and CSS files for the component, as well as modify the "app.module.ts" file to include the new component in our app.

Open the "hello-world.component.ts" file in your text editor and replace the existing code with the following:

import { Component } from '@angular/core';

@Component({
  selector: 'app-hello-world',
  template: `
    # Hello, world!


  `,
})
export class HelloWorldComponent {}

This defines a new Angular component called "HelloWorldComponent" with a simple template that displays the text "Hello, world!".

Now open the "app.component.html" file and replace the existing code with the following:

<app-hello-world></app-hello-world>

This adds our new "HelloWorldComponent" to the main template for our app.

Finally, save your changes and run the app again by running:

ng serve

If everything went well, you should now see the text "Hello, world!" displayed in your browser at "http://localhost:4200".

Conclusion

Setting up your Angular development environment can seem daunting at first, but it’s actually a relatively simple process once you know what you’re doing. By following the steps outlined in this article, you should now have a functional Angular development environment and be ready to start building your own apps!

Of course, this is just the tip of the iceberg when it comes to Angular development. There are countless additional tools, libraries, and frameworks to explore, as well as best practices and design patterns to master.

If you’re serious about becoming an Angular developer, we recommend taking a more comprehensive course or tutorial to learn the ins and outs of this powerful framework. But for now, you should have everything you need to get started and build some basic apps!

Top comments (0)