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
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.
The fix required upgrading npm. Then running the installer again.
./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.
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.
{
"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"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With