【问题标题】:Rails 7.0 + esbuild: running app gives error: Command "build" not foundRails 7.0 + esbuild:运行应用程序给出错误:找不到命令“build”
【发布时间】:2021-12-25 18:52:17
【问题描述】:

新生成的 Rails 7.0 启动时出现 esbuild 选项错误。

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

在创建新的 rails 7 项目时,我尝试使用 bin/dev 启动应用程序,现在使用的是工头。但是,它会出现“找不到错误命令“build”。”

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-7


    【解决方案1】:

    问题

    问题在于,使用 npm package.json 脚本中。

    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
    }
    
    

    稍后的 npm (>= 7.1),为你将它添加到 package.json 中。最好的长期解决方案是升级 npm(解决方案 1),但是,您仍然可以手动添加脚本(参见下面的解决方案 2),它会起作用。

    解决方案

    1。 npm 升级:

    修复需要升级 npm。然后再次运行安装程序。

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

    重新运行安装程序的示例

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

    完成此操作后,Rails 会使用所需的脚本自动更新 package.json。

    2。将脚本添加到package.json

    如果由于某种原因,您无法升级 node/npm,那么您只需按照说明将“添加脚本”命令复制到 package.json 中。

    1. 添加脚本(见下文)
    {
      "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. 纱线构建
    2. 纱线构建:css

    【讨论】:

      【解决方案2】:

      我想添加到@notapatch 答案以在第 2 步之后运行以下命令:

      yarn build
      

      然后

      yarn build:css
      

      不客气。

      【讨论】:

      • 您好@Nelson-Miranda 我已经更改了答案。让我知道你是不是这个意思?
      猜你喜欢
      • 2019-01-17
      • 2019-06-21
      • 1970-01-01
      • 2023-02-04
      • 2015-12-21
      • 1970-01-01
      • 2021-09-20
      • 2018-01-14
      • 1970-01-01
      相关资源
      最近更新 更多