3

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

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

在创建一个新的 rails 7 项目时,我尝试使用bin/devwhich uses now uses foreman 启动应用程序。但是,它会因“找不到错误命令“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
4

2 回答 2

7

问题

问题是 npm < 7.1 rails generation 要求您将构建命令添加到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 捆绑需要 npm 7.1+
  2. 再次运行安装程序

重新运行安装程序的示例

   ./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
于 2021-12-25T18:52:17.563 回答
1

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

yarn build

接着

yarn build:css

别客气。

于 2022-02-06T16:45:53.420 回答