Problem with naming modules in 0.19.1

The compiler tells me to split module names depending on the directory structure. But it also complains if two files in different directories have the same name. Example:

  1. I have a Main.elm file which has the main and update functions. I have some initialisation stuff in a file Init/Init.elm with a module Init. But the compiler complains about this.

  2. When I change the Init.elm file to Main.elm, and the module to Init.Main, the compiler complains that I have two modules called Main, even though one is named Init.Main .

  3. This means I will have to come with unique names for the last part of every module, making things convoluted.

Is there a better way of doing this?

Are you sure? I think the compiler allows:

/Main.elm  (contains module Main)
/Init/Main.elm (contains module Init.Main)

I’m still using 0.19.0, but I would be surprised if it has changed in 0.19.1?

Try to reproduce this error in a SSCCE. I have tried creating a folder structure with a Main.elm and a folder named Init containing another file Main.elm that contains the module Init.Main and it works just fine.

Later Edit: Upon some more experiments I realized that the error might be due to multiple source directories.

If you have a src folder with a Main.elm and a init folder with a Main.elm and both these folders listed in the source-directories, you will get into a conflict.

2 Likes

A mistake I’ve made often is forgetting to rename the module after renaming the file.

Have you ensured src/Init/Init.elm was named Init. Init inside the file? Did you change it when you renamed to Init.Main?

“the error might be due to multiple source directories.”

It was this. Using the naming scheme suggested by the compiler will collide with any source directory pointing to the file’s directory.

Also, the original problem was that the root file must now, as of 0.19.1, be named “Main.elm” with capital M.

Does it have to be Main.elm? My current side project in 0.19.1 I’m building with parcel and has 2 entry modules, Server.Main and Client.Main

Are they compiled to separate js files or the same? Can you have two entry points in the same file.

They are compiled to 2 separate JS files. I’m fairly certain you can have multiple entry points in the same file, in my case though they are separate because they are being loaded by separate HTML files.

You can build multiple Main files into a single js file by passing them into elm make together. For example, you could run elm make src/Init.Main.elm src/Main.elm. This will produce one file with one instance of the elm runtime and with both of your entry points. When you import the file in javascript, perhaps as Elm, then you reference them like: Elm.Init.Main.init(...) and Elm.Main.init(...).

3 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.