Rails

【Rails7】bin/devでerror Command “build” not foundが出てしまう場合の対処法

Rails

 

この記事の内容
  1. 概要
  2. 対処法
  3. 参考

概要

Rails7のアプリケーションで% bin/devでサーバーを立ち上げようとするも、「error Command “build” not found.」が出てしまい起動ができない。

% bin/dev                                                                                                                                    
10:10:29 web.1  | started with pid 12030
10:10:29 js.1   | started with pid 12031
10:10:29 css.1  | started with pid 12032
10:10:29 css.1  | yarn run v1.22.4
10:10:29 js.1   | yarn run v1.22.4
10:10:30 js.1   | error Command "build" not found.
10:10:30 js.1   | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
10:10:30 css.1  | error Command "build:css" not found.
10:10:30 css.1  | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
10:10:30 web.1  | => Booting Puma
10:10:30 web.1  | => Rails 7.0.3 application starting in development 
10:10:30 web.1  | => Run `bin/rails server --help` for more startup options
10:10:30 css.1  | exited with code 1
10:10:30 system | sending SIGTERM to all processes
10:10:30 js.1   | exited with code 1
10:10:30 web.1  | Exiting
10:10:30 web.1  | terminated by SIGTERM
  • ruby: 3.1.2
  • rails: 7.0.2.3

対処法

package.jsonに以下の記述を追記する。

"scripts": {
    "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds",
    "build:css": "sass ./app/assets/stylesheets/application.bootstrap.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules"
  }

 

追記後は以下のようになります。

{
  "name": "app",
  "private": "true",
  "dependencies": {
    "@hotwired/stimulus": "^3.0.1",
    "@hotwired/turbo-rails": "^7.1.1",
    "@popperjs/core": "^2.11.4",
    "bootstrap": "^5.1.3",
    "bootstrap-icons": "^1.8.1",
    "esbuild": "^0.14.30",
    "sass": "^1.49.11"
  },
  "scripts": {
    "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds",
    "build:css": "sass ./app/assets/stylesheets/application.bootstrap.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules"
  }
}

参考