Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The module has a bad import

Tags:

date

elm

While trying to import the Date module and using it in a simple Elm file, I'm getting the following error:

-- UNKNOWN IMPORT ---------------------------- app/javascript/CountdownTimer.elm

The CountdownTimer module has a bad import:

    import Date

I cannot find that module! Is there a typo in the module name?

The "source-directories" field of your elm.json tells me to only look in the src
directory, but it is not there. Maybe it is in a package that is not installed
yet?

CountdownTimer is the name of the file and the module, taken from here, which seems to work fine, but not for me.

I'm using Elm 0.19.0 and Rails 6.0.0beta1, which seems not to be a constraint, since if I enter to the Elm REPL in any place, and try to import date, I face the same error:

> import Date
-- UNKNOWN IMPORT ---------------------------------------------------------- elm

The Elm_Repl module has a bad import:

    import Date

I cannot find that module! Is there a typo in the module name?

When creating a package, all modules must live in the src/ directory.

My elm.json file looks like:

{
    "type": "application",
    "source-directories": [
        "src"
    ],
    "elm-version": "0.19.0",
    "dependencies": {
        "direct": {
            "elm/browser": "1.0.1",
            "elm/core": "1.0.2",
            "elm/html": "1.0.0",
            "elm/time": "1.0.0",
            "elm/json": "1.1.2",
            "elm/url": "1.0.0",
            "elm/virtual-dom": "1.0.2"
        },
        "indirect": {
        }
    },
    "test-dependencies": {
        "direct": {},
        "indirect": {}
    }
}
like image 876
Karol Karol Avatar asked Jan 21 '19 11:01

Karol Karol


1 Answers

The code you are trying to use is outdated. Date was removed from core in Elm 0.19, with no direct replacement, or indirect replacement that covers the exact same functionality, as far as I'm aware. elm/time is intended as the official successor of both Date and Time, but it offers no date/time string parsing which is what you need here.

The old Date.fromString was just a thin wrapper over the JavaScript parsing API, and therefore not very consistent or well-defined. Hence why it was removed. In Elm 0.19 you have to use a third-party package that offers more specific functionality, such as elm-iso8601-date-strings.

If what you receive are ISO 8601 strings you should be able to replace parseTime with:

import Iso8601
import Time

parseTime : String -> Time.Posix
parseTime string =
    Iso8601.toTime string
        |> Result.withDefault (Time.millisToPosix 0)

If, however, what you receive is not in ISO 8601 format, you need to find another package that parses that specific format.

like image 190
glennsl Avatar answered Oct 26 '22 15:10

glennsl