LoginSignup
0
0

More than 1 year has passed since last update.

nodebrew + npm + NestJS + Docker + PostgreSQLの開発環境構築

Posted at

新しい技術を利用している環境にジョインするため、事前に技術スタックを聞いて触っておいた時のメモ。

0. 前提条件

  1. 利用PC
    macOS Monterey
    バージョン 12.5.1
    
    MacBook Pro(14インチ、2021)
    チップ Apple M1 Pro
    メモリ 16GB
    起動ディスク Macintosh HD
    ストレージ 500GB
    

1.1. Homebrewのインストール

  1. homebrewのインストール
    % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    ・・・
    Press RETURN/ENTER to continue or any other key to abort: ←ENTERを押す
    。。。
    ==> Installation successful!
    
  2. インストールできているか確認
    % brew -v
    Homebrew 3.6.1
    Homebrew/homebrew-core (git revision c170d14e690; last commit 2022-09-18)
    

1.2. nodebrewのインストール

  1. 既にnodeがインストールされているか確認
    % brew ls | grep node
    
  2. (既にnodeがインストールされていたら)nodeのアンインストール
    % brew uninstall --force node
    
  3. nodebrewのインストール
    % brew install nodebrew
    
  4. nodebrewがインストールできているか確認
    % nodebrew -v
    nodebrew 1.2.0
    
  5. nodebrewのセットアップ
    % nodebrew setup
    
    1. nodebrewのパスを環境変数として追加
      # 利用しているshの確認
      % echo $SHELL
      
      # bashを利用している場合
      % echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bash_profile
      % source ~/.bash_profile
      
      # zshを利用している場合
      % echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.zshrc
      % source ~/.zshrc
      

1.3. nodeのインストール

  1. nodebrewでインストールしたいバージョンを確認してnodeをインストール
    1. インストールしたいバージョンを確認
      % nodebrew ls-remote
      
    2. nodeのインストール
      % nodebrew install-binary stable # ←安定版をインストール
      % nodebrew install-binary latest # ←最新版をインストール
      % nodebrew install-binary <version> # ←バージョン指定でインストール
      
      ()
      % nodebrew install-binary stable
      ・・・
      Installed successfully
      
    3. nodeがインストールされたか確認
      % nodebrew ls
      
      ()
      % nodebrew ls
      v18.9.0 # ←インストールされているバージョンが表示される
      
      current: none
      
    4. 利用するnodeのバージョンを指定
      % nodebrew use <version>
      
      ()
      % nodebrew use v18.9.0
      use v18.9.0
      
    5. 指定したバージョンのnodeが指定されているか確認
      % nodebrew ls
      
      ()
      % nodebrew ls
      v18.9.0 # ←インストールされているバージョンが表示される
      
      current: v18.9.0
      

1.4. nestjsのプロジェクトを作成

  1. npmでnestjsのCLIをインストール
    % npm i -g @nestjs/cli
    
    added 255 packages, and audited 256 packages in 27s
    
    39 packages are looking for funding
    run `npm fund` for details
    
    found 0 vulnerabilities
    npm notice 
    npm notice New patch version of npm available! 8.19.1 -> 8.19.2
    npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.19.2
    npm notice Run npm install -g npm@8.19.2 to update!
    npm notice 
    
  2. npmでnestjsのプロジェクトを作成
    % nest new <project_name> # ←プロジェクトの名前を入れる
    ・・・
    ? Which package manager would you ❤️  to use? npm # ←npmを選択
    ・・・
    🚀  Successfully created project nestjs_app
    👉  Get started with the following commands:
    
    $ cd <project_name>
    $ npm run start
    
                                            
                            Thanks for installing Nest 🙏
                    Please consider donating to our open collective
                            to help us maintain this package.
                                            
                                            
                🍷  Donate: https://opencollective.com/nest
    

1.5. サーバーの起動

  1. プロジェクトのディレクトリに移動
    % cd <project_name>
    
  2. サーバーを起動する
    % npm run start
    
  3. ブラウザで確認
    • ブラウザで "http://localhost:3000/" にアクセスして "Hello World!" が表示されていることを確認する

1.6. postgresを利用してdocker化する

  1. docker-desktopのインストール
  2. dockerがインストールされたか確認
    # dockerのバージョン確認
    % docker -v
    Docker version 20.10.14, build a224086
    
    # docker composeのバージョン確認
    % docker compose version
    Docker Compose version v2.5.1
    
  3. プロジェクトにDockerfileとdocker-compose.ymlを作成する
    % touch Dockerfile
    
    % touch docker-compose.yml
    
  4. Dockerfileを編集する
    % vim Dockerfile
    
    # Dockerfileの内容
    FROM node:latest
    
    WORKDIR /app
    
    COPY package*.json ./
    
    RUN npm ci
    RUN npm i @nestjs/cli
    
    COPY . .
    
    EXPOSE 3001
    
    CMD ["npm", "run", "start:dev"]
    
  5. docker-compose.ymlを編集する
    % vi docker-compose.yml
    
    # docker-compose.ymlの内容
    version: '3'
    
    services:
      service:
        build:
          context: .
          dockerfile: Dockerfile
        ports:
          - 3000:3000
        restart: always
        volumes:
          - ./src:/app/src
      postgres:
        image: postgres:latest
        environment:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: password
          POSTGRES_DB: postgres
          TZ: 'Asia/Tokyo'
        ports:
          - 5432:5432
        volumes:
          - ./postgres/data:/var/lib/postgresql/data
    
  6. dockerを起動する
    % docker compose up --build
    
    ※既にサーバーを起動していた場合はそちらをさっきに落としておく
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0