LoginSignup
1
0

More than 1 year has passed since last update.

[ERROR] Could not resolve "@rails/request" を解決する!

Posted at

エラー内容

以下のようなエラーが出たときの解決方法を記録しておきます。

$ docker-compose run --rm app yarn build
Creating rails7-bramee_app_run ... done
yarn run v1.22.17
$ esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=assets
✘ [ERROR] Could not resolve "@rails/request"

    app/javascript/controllers/***_controller.js:
      2 │ import { FetchRequest } from "@rails/request"
        ╵                              ~~~~~~~~~~~~~~~~

  You can mark the path "@rails/request" as external to exclude it from the bundle, which will
  remove this error.

1 error
node:child_process:879
    throw err;
    ^

Error: Command failed: /myapp/node_modules/@esbuild/linux-x64/bin/esbuild app/javascript/application.js app/javascript/importmap.json --bundle --sourcemap --outdir=app/assets/builds --public-path=assets
    at checkExecSyncError (node:child_process:841:11)
    at Object.execFileSync (node:child_process:876:15)
    at Object.<anonymous> (/myapp/node_modules/esbuild/bin/esbuild:211:28)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  status: 1,
  signal: null,
  output: [ null, null, null ],
  pid: 36,
  stdout: null,
  stderr: null
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ERROR: 1

上記のエラーから、package.json@rails/requestがないことが推測できます。

解決方法

ですので、以下のコマンドを実行することで解決することができます。

docker-compose run --rm app yarn add @rails/request.js

npm を使用している場合は以下です。

docker-compose run --rm app npm install --save @rails/request.js

注意点

私は以下のようなコマンドを実行していました。

docker-compose run --rm app yarn add @rails/request

↑のコマンドでは、以下の結果になり、失敗します。

yarn add v1.22.17
[1/4] Resolving packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/@rails%2frequest: Not found".
info If you think this is a bug, please open a bug report with the information provided in "/myapp/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
ERROR: 1

なぜここにハマったのかは、JavaScriptファイルに原因がありました。
@rails/request.jsを使用しているJavaScriptファイルは以下のようになっていました。

import { Controller } from "@hotwired/stimulus"

// 以下の一文が"@rails/request.js"ではなく、"@rails/request"となっていた
import { FetchRequest } from "@rails/request"

// Connects to data-controller="notice"
export default class extends Controller {
  ...
  }
}

以下から正確なパッケージを検索することで気づきました。。。

公式な情報を頼りにすることが一番大事であるとわかっていても、
楽な方法を選択してしまいますね。。。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0