Installing Angular and Environment Setup
# CHAPTER 2
Installing Angular and Environment Setup
1. Chapter Introduction
Unlike a simple HTML website where you just create anindex.html file and open it in Chrome, Angular requires an environment setup. Because Angular is written in TypeScript, the browser cannot understand it. It must be "transpiled" (translated) into standard JavaScript before it runs. To handle this, we need to install Node.js and the official Angular Command Line Interface (CLI).
2. Learning Objectives
By the end of this chapter, you will be able to:- Install Node.js and NPM on your machine.
- Install the Angular CLI globally.
- Use the CLI to generate a new Angular project.
- Launch the Angular local development server.
3. Step 1: Installing Node.js
Angular requires Node.js to manage its development server and build process. Node.js comes bundled with NPM (Node Package Manager), which we will use to download the Angular framework.-
1.
Go to
nodejs.org.
- 2. Download the LTS (Long Term Support) version.
- 3. Install it using the default settings.
- 4. Verify the installation by opening your terminal or command prompt and typing:
4. Step 2: Installing the Angular CLI
The Angular CLI is a powerful tool provided by the Angular team. It automates the painful process of configuring Webpack, TypeScript transpilers, and testing frameworks.To install it globally on your computer, run this command in your terminal:
Once installed, verify it by checking the version:
*(Note: Every Angular CLI command starts with ng).*
5. Step 3: Creating an Angular Project
Navigate to the folder where you want to store your project using thecd command, and then generate a new app:
The CLI will ask you a few questions:
-
Which stylesheet format would you like to use? Choose
CSSorSCSS.
-
Do you want to enable Server-Side Rendering (SSR)? For now, type
N(No).
The CLI will download hundreds of megabytes of dependencies into a node_modules folder. This takes a minute or two.
6. Step 4: Running the Angular App
Once the installation finishes, you must move into the newly created project folder before you can run it!Wait until it says Compiled successfully. Now, open your web browser and navigate to:
http://localhost:4200/
Congratulations! You should see the default Angular welcome screen!
7. Step 5: Visual Studio Code Setup
To edit your code, we highly recommend Visual Studio Code (VS Code). To make development easier, install the following VS Code Extensions:- Angular Language Service: Provides autocomplete and error checking inside HTML templates.
- Material Icon Theme: Adds nice icons to your file explorer so you can easily distinguish between Component, Service, and Module files.
8. Common Mistakes
-
Running
ng serveoutside the project folder: Beginners often runng new app-name, then immediately typeng servewithout typingcd app-namefirst. The terminal will throw an error saying it cannot find the angular workspace.
-
Outdated Node.js: If
ng newfails with cryptic errors, it almost always means your version of Node.js is too old. Update Node to the latest LTS version.
9. Real-World Analogy
Think of the Angular CLI as a highly skilled construction foreman. If you ask him to build a house (ng new), he brings the concrete, the wood, and the architects for you. You don't have to worry about configuring the blueprint; you just tell him what color to paint the walls!
10. MCQs with Answers
Why does Angular require Node.js to be installed?
What is NPM?
What command installs the Angular CLI globally?
What command generates a brand new Angular project?
What command starts the local development server so you can view your app in the browser?
What is the default port that Angular runs on locally?
If you get an error "This command is not available when running the Angular CLI outside a workspace", what did you likely forget to do?
What VS Code extension is deemed absolutely essential for Angular developers as it provides autocomplete in HTML files?
What does the -g flag do in npm install -g?
11. Interview Questions
- Q: Explain what the Angular CLI is and why it is beneficial for development teams.
- Q: What is the underlying compiler/bundler that the Angular CLI uses behind the scenes to bundle the application? (Answer: Webpack, though modern Angular is transitioning to Vite/Esbuild).
12. Summary
Setting up an Angular environment is a one-time process. By installing Node.js and the Angular CLI, you equip your machine with an enterprise-grade build system. Theng serve command provides "Hot Module Replacement"—meaning every time you save a file in VS Code, your browser will instantly refresh to show the changes without you having to click reload!
13. Next Chapter Recommendation
When you generate a new app, the CLI creates dozens of files and folders automatically. It can be overwhelming! In Chapter 3: Angular Project Structure, we will demystify this folder structure and explain exactly whatangular.json, package.json, and the src folder do.