Rails7 Stimulus エラー「TypeError: Failed to resolve module specifier “stimulus”. Relative references must start with either」が発生した場合の対処法

  • 作成日 2024.06.26
  • rails
Rails7 Stimulus エラー「TypeError: Failed to resolve module specifier “stimulus”. Relative references must start with either」が発生した場合の対処法

Stimulusで、エラー「TypeError: Failed to resolve module specifier “stimulus”. Relative references must start with either」が発生した場合の対処法を記述してます。

環境

  • OS Ubuntu24.04
  • rails 7.1.3
  • ブラウザ chrome 122.0.6261.69

エラー全文

以下のコードで発生。

import { Application } from "stimulus"

const application = Application.start()

// Configure Stimulus development experience
application.debug = false
window.Stimulus   = application

export { application }

ちなみにfirefoxだと以下のエラーとなります。

Uncaught TypeError: specifier “stimulus” は露出していますが、どこにも再マップされていません。モジュール指定の相対パスは “./” または “../”, “/” のいずれかで始まらなければなりません。

原因

stimulusのパスが違ったため

対処法

正しいパスを指定する。自分の場合は、以下のパスとなります。

import { Application } from "@hotwired/stimulus"

const application = Application.start()

// Configure Stimulus development experience
application.debug = false
window.Stimulus   = application

export { application }