Module GenStage is not loaded and could not be found

Hi,

I’ve been using GenStage for a few days without any problems, and all of a sudden, this error starts appearing to me:

 == Compilation error in file lib/c.ex ==
 ** (CompileError) lib/c.ex:2: module GenStage is not loaded and could not be found
     (elixir) expanding macro: Kernel.use/1
     lib/c.ex:2: C (module)

I’ve been trying this with a few new clean projects, using mix new. I tried wiping out _build, but nothing works. I’ve also tested this in a couple of machines. This just doesn’t make any sense…

The C module (GenStage docs page code) contains the following code:

defmodule C do
  use GenStage

  def start_link(_opts) do
    GenStage.start_link(C, :ok)
  end

  def init(:ok) do
    {:consumer, :the_state_does_not_matter}
  end

  def handle_events(events, _from, state) do
    # Wait for a second.
    Process.sleep(1000)

    # Inspect the events.
    IO.inspect(events)

    # We are a consumer, so we would never emit items.
    {:noreply, [], state}
  end
end

Is genstage properly specified as a dependency?

3 Likes

Ok now it’s working but I had to specify it as a dependecy, which is something I didn’t previously do. Why would genstage needed to be specified as a dependency like a hex package?

Because gen stage is not part of elixir, but a package available through hex.

I’m not sure though, why it worked previously without specifying it… Only reason I can come up with is, that one installed it globally, which should be generally avoided for any package.

6 Likes

Great help @NobbZ, I had no idea it wasn’t a part of elixir, from everything I read I just assumed it was part of it. Thanks!