Introduction
As a DevOps engineer, you’re tasked with managing various aspects of a Node.js application’s lifecycle—from setting up the environment to monitoring performance. This guide will delve deep into the actual commands you’ll need to master each stage.
This is extension to previous article previous article Leveraging-node-js-in-devops but focusing on commands.
Setting Up the Node.js Environment
- Install Node.js and npm
- Windows, macOS, Linux:
Download the installer from the official Node.js website. - Verify Installation:
node -v npm -vThese commands should return version numbers, confirming a successful installation. - Windows, macOS, Linux:
- Use Node Version Manager (NVM)
- Install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash- Switch Node.js versions:
nvm use <version>
Initialize a Node.js Project
- Initialize npm and Create package.json:
npm init
Answer the prompted questions to create your package.json file.
- Install Packages:
- Production Dependency:
bash npm install <package-name> --save - Development Dependency:
bash npm install <package-name> --save-dev
- Production Dependency:
Version Control with Git
- Initialize a Git Repository:
git init
- Commit Code:
git add .
git commit -m "Initial commit"Code language: JavaScript (javascript)
Building and Testing
- Transpiling with Babel:
- Install Babel:
npm install --save-dev @babel/core @babel/cli- Transpile Code:
npx babel src --out-dir dist - Unit Testing with Jest:
- Install Jest:
npm install --save-dev jest- Run Tests:
npm test - End-to-End Testing with Cypress:
- Install Cypress:
bash npm install cypress --save-dev - Run Cypress Tests:
bash npx cypress run
- Install Cypress:
Deployment and Containers
- Dockerize Your Application:
- Build Docker Image:
bash docker build -t <image-name> . - Run Docker Container:
bash docker run -p 3000:3000 <image-name>
- Build Docker Image:
CI/CD Pipeline Configuration
- Initialize GitHub Actions:
Create a.github/workflows/main.ymlfile in your repository. - Automate Tests in CI Pipeline:
Include the following in yourmain.ymlfile to run tests:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: '14'
- name: Install Dependencies
run: npm install
- name: Run Tests
run: npm testCode language: JavaScript (javascript)
Monitoring and Logging
- Install Winston for Logging:
npm install winston
- Set up Prometheus and Grafana for Monitoring:
(Usually involves running Docker containers or installing specific services. The commands will vary based on your infrastructure.)
Security Checks
- Scan Dependencies with Snyk:
- Install Snyk:
bash npm install -g snyk - Test for Vulnerabilities:
bash snyk test
- Install Snyk:
By mastering these commands and the stages they correspond to, you’ll become an effective DevOps engineer in a Node.js environment.Initialize a Node.js Project