Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 7.0 + esbuild: running app gives error: Command "build" not found

Newly generated Rails 7.0 with esbuild option errors out on startup.

rails new [project name] --javascript=esbuild --css=tailwind

On creating a new rails 7 project, I try to start the application using bin/dev which uses now uses foreman. However, it errors out with 'error command "build" not found.'

bin/dev
                                                                                                      !10520
16:07:31 web.1  | started with pid 53018
16:07:31 js.1   | started with pid 53019
16:07:31 css.1  | started with pid 53020
16:07:32 js.1   | yarn run v1.22.17
16:07:32 css.1  | yarn run v1.22.17                    **************             
16:07:32 js.1   | error Command "build" not found. <== *****THIS*****
16:07:32 js.1   | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
16:07:32 css.1  | error Command "build:css" not found.
16:07:32 css.1  | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
16:07:33 css.1  | exited with code 1
16:07:33 system | sending SIGTERM to all processes
16:07:33 js.1   | exited with code 1
16:07:33 web.1  | terminated by SIGTERM
like image 987
notapatch Avatar asked Sep 14 '25 10:09

notapatch


1 Answers

Issue

The problem is that with npm < 7.1 rails generation expects you to add build commands to the package.json scripts.

rails new my_app --javascript=esbuild --css=tailwind
...
Add "scripts": { "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds" } to your package.json
...
Add "scripts": { "build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css" } to your package.json

$ cat ol_npm/package.json
{
  "name": "app",
  "private": "true",
  "dependencies": {
    "@hotwired/stimulus": "^3.0.1",
  ...
  }
  // !! script section missing !!
  // Add the above scripts
}

Later npm (>= 7.1), add it in to package.json for you. The best long term fix is to upgrade npm (solution 1), however, you can still add scripts by hand (see solution 2 below) and it will work.

Solution

1. npm upgrade:

The fix required upgrading npm. Then running the installer again.

  1. js-bundling requires npm 7.1+
  2. run the installer again

Examples of re-running installer

   ./bin/rails javascript:install:[esbuild|rollup|webpack]
   ./bin/rails css:install:[tailwind|bootstrap|bulma|postcss|sass]

With this done Rails automatically updates package.json with the required scripts.

2. Add the script to package.json

If for some reason, you can't upgrade node/npm then you simply have to copy the "Add script" commands into package.json as instructed.

  1. Add script (see below)
{
  "name": "app",
  "private": "true",
  "dependencies": {
    "@hotwired/stimulus": "^3.0.1",
    ...
  },
  "scripts": {
    "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds",
    "build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css"
  }
}
  1. yarn build
  2. yarn build:css
like image 126
notapatch Avatar answered Sep 16 '25 07:09

notapatch