dmmulroy / create-melange-app

The fastest, friendliest, and most delightful way to get started with OCaml, ReasonML, and Melange, geared towards JavaScript and TypeScript developers
104 stars 19 forks source link

"npm create melange-app@latest" failed to install OCaml dependencies to your local opam switch #96

Open zwpdbh opened 3 months ago

zwpdbh commented 3 months ago

Failed to install OCaml dependencies to your local opam switch The scaffolding process failed while running opam install . --deps-only --yes. Opam is OCaml and ReasonML's package manager.

opam switch list

zw@zwpdbh:~/code/ocaml-programming/tmp/hello$ opam switch list
#  switch                                     compiler                   description
→  /home/zw/code/ocaml-programming/tmp/hello  ocaml-base-compiler.5.1.1  /home/zw/code/ocaml-programming/tmp/hello
   /home/zw/code/tmp/ocaml_web/hello_ocaml    ocaml-base-compiler.5.1.1  /home/zw/code/tmp/ocaml_web/hello_ocaml
   5.0.0                                      ocaml-base-compiler.5.0.0  5.0.0
   default                                    ocaml-system.4.08.1        default

[NOTE] Current switch has been selected based on the current directory.
       The current global system switch is 5.0.0.

opam install . --deps-only --yes

zw@zwpdbh:~/code/ocaml-programming/tmp/hello$ opam install . --deps-only --yes
Sorry, no solution found: there seems to be a problem with your request.

No solution found, exiting

cat dune-project

cat dune-project
(lang dune 3.14)
; The above stanza specifies the Dune language version being used.

; Declares the use of the Melange compiler with a specific version.
(using melange 0.1)

; Declares the use of the directory-targets extension, useful for managing
; directory-level build targets.
(using directory-targets 0.1)

; Sets the name of the project.
(name hello)

; Indicates whether OPAM files should be automatically generated.
(generate_opam_files true)

; Provides the source location of the project, typically a GitHub repository.
(source
 (github your-username/hello))

; Lists the authors of the project.
(authors "Your Name <your.email@gmail.com>")

; Specifies the maintainers of the project, which can be different from authors.
(maintainers "Your Name <your.email@gmail.com>")

; Declares the license type under which the project is distributed.
(license MIT)

; Provides a link to the project's documentation.
(documentation https://github.com/your-username/hello)

; The package stanza defines the configuration for an OPAM package.
(package
 (name hello)
 (synopsis "Enter a synopsis of your app here")
 (description "Enter a description of your app here")
; Allows the package to be empty, which can be useful during development.
 (allow_empty)
; Lists the dependencies of the package along with version constraints.
 (depends
  (dune (>= 3.14))
  (melange (>= 3.0.0-51))
  (ocaml (>= 5.1.1))
  opam-check-npm-deps
  ppx_deriving
  (reason (>= 3.11.0))
  (reason-react (>= 0.13.0))
  reason-react-ppx
)
; Tags to categorize or label the package, useful for searches and indexing.
 (tags
  (reasonml, ocaml, melange)))

cat dune

zw@zwpdbh:~/code/ocaml-programming/tmp/hello$ cat dune
; This dune file segment configures the build process for the project,
; focusing on web bundling.

; This stanza excludes the 'node_modules' directory from being
; considered by dune, which is a standard practice to prevent Dune from scanning
; dependency files.
(dirs :standard \ node_modules)

; The 'alias' block named 'all' defines a high-level alias for building the
; entire project. It specifies that the 'all' alias depends on another alias,
; typically named after the bundler being used (e.g., 'vite', 'webpack'). This
; setup allows for easy switching between different bundlers without changing
; the overall build process.
(alias
  (name all)
  (deps
     (alias_rec webpack)))

; The 'rule' block associated with the bundler alias (e.g., 'vite', 'webpack')
; defines the specific steps to build and bundle the web application.
;
; - 'targets (dir dist)' indicates the output directory for the bundled files.
;
; - The 'deps' section lists dependencies like the project alias, bundler config
;   files, and entry HTML file.
;
; - The 'action' specifies the system command to run the bundler, in this case a
;   script from 'node_modules' to run a bundler.
;
; - 'promote (until-clean)' in 'mode' is used for artifact promotion, managing built files.
(rule
  (alias webpack)
   (targets (dir dist))
   (deps
     (alias_rec hello)
     (:webpack ./webpack.config.js)
     (source_tree ./public))
   (action
    (system "../../node_modules/.bin/webpack --mode production \
              --entry ./output/src/App.mjs && cp ./public/index.html dist/index.html"))
   (mode
     (promote (until-clean))))

; The 'melange.emit' block is used to compile ReasonML/OCaml code to JavaScript.
; - 'target output' specifies where the compiled JS files will be placed.
; - It relies on the 'app' library and compiles using specified module systems
;   (here, es6 and mjs).
  (melange.emit
   (target output)
   (alias hello)
   (libraries app)
   (module_systems (es6 mjs)))
muqiuhan commented 3 months ago

Did you dune build @install before running opam install . --deps-only --yes?

muqiuhan commented 3 months ago

Related questions on

You may need to update opam.

dmmulroy commented 3 months ago

Also make sure that you run eval $(opam env) before trying to run opam install . --deps-only --yes (Is the error message missing this step/instruction?)

zwpdbh commented 3 months ago

Also make sure that you run eval $(opam env) before trying to run opam install . --deps-only --yes (Is the error message missing this step/instruction?)

Update: After I installing latest opam from:

bash -c "sh <(curl -fsSL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)"

, I am now using opam 2.1.5.

Then, I could run opam install . --deps-only --yes without problems. However, it got stuck on new error:

 Failed attempting to build your project with Dune

    The scaffolding process failed while running `opam exec -- dune build`.
    Dune is OCaml and ReasonML's build tool.

    Please try `cd`ing into the project directory created by
    `create-melange-app` and running the following commands:

    eval $(opam env)
    dune build

    If the problem persists, please open an issue at
    github.com/dmmulroy/create-melange-app/issues, and or join our discord for
    help at https://discord.gg/fNvVdsUWHE.

    If you open an issue, please `cd` into the directory created by
    `create-melange-app` and include the output from the following commands:

    `opam switch list`,
    `dune build`,
    `cat dune-project`, and `cat dune`

Then, I follow the instruction:

zw@zw:~/code/ocaml_programming/hello$ eval $(opam env)
zw@zw:~/code/ocaml_programming/hello$ dune build
File "dune", line 32, characters 0-350:
32 | (rule
33 |   (alias webpack)
34 |    (targets (dir dist))
....
41 |               --entry ./output/src/App.mjs && cp ./public/index.html dist/index.html"))
42 |    (mode
43 |      (promote (until-clean))))
asset main.js 200 KiB [emitted] [minimized] (name: main) 1 related asset
orphan modules 585 KiB [orphan] 42 modules
cacheable modules 726 KiB
  modules by path ../../node_modules/react/ 7.98 KiB
    modules by path ../../node_modules/react/*.js 404 bytes 2 modules
    modules by path ../../node_modules/react/cjs/*.js 7.59 KiB 2 modules
  modules by path ../../node_modules/react-dom/ 131 KiB
    ../../node_modules/react-dom/client.js 619 bytes [built] [code generated]
    ../../node_modules/react-dom/index.js 1.33 KiB [built] [code generated]
    ../../node_modules/react-dom/cjs/react-dom.production.min.js 129 KiB [built] [code generated]
  modules by path ../../node_modules/scheduler/ 4.33 KiB
    ../../node_modules/scheduler/index.js 198 bytes [built] [code generated]
    ../../node_modules/scheduler/cjs/scheduler.production.min.js 4.14 KiB [built] [code generated]
  ./output/src/App.mjs + 40 modules 583 KiB [built] [code generated]
webpack 5.91.0 compiled successfully in 1550 ms
cp: cannot stat './public/index.html': No such file or directory

my env:

zw@zw:~/code/ocaml_programming/hello$ opam switch list
#  switch                                 compiler                   description
→  /home/zw/code/ocaml_programming/hello  ocaml-base-compiler.5.1.1  /home/zw/code/ocaml_programming/hello
   default                                ocaml-system.4.08.1        default

zw@zw:~/code/ocaml_programming/hello$ cat dune-project
(lang dune 3.14)
; The above stanza specifies the Dune language version being used.

; Declares the use of the Melange compiler with a specific version.
(using melange 0.1)

; Declares the use of the directory-targets extension, useful for managing
; directory-level build targets.
(using directory-targets 0.1)

; Sets the name of the project.
(name hello)

; Indicates whether OPAM files should be automatically generated.
(generate_opam_files true)

; Provides the source location of the project, typically a GitHub repository.
(source
 (github your-username/hello))

; Lists the authors of the project.
(authors "Your Name <your.email@gmail.com>")

; Specifies the maintainers of the project, which can be different from authors.
(maintainers "Your Name <your.email@gmail.com>")

; Declares the license type under which the project is distributed.
(license MIT)

; Provides a link to the project's documentation.
(documentation https://github.com/your-username/hello)

; The package stanza defines the configuration for an OPAM package.
(package
 (name hello)
 (synopsis "Enter a synopsis of your app here")
 (description "Enter a description of your app here")
; Allows the package to be empty, which can be useful during development.
 (allow_empty)
; Lists the dependencies of the package along with version constraints.
 (depends
  (dune (>= 3.14))
  (melange (>= 3.0.0-51))
  (ocaml (>= 5.1.1))
  opam-check-npm-deps
  ppx_deriving
  (reason (>= 3.11.0))
  (reason-react (>= 0.13.0))
  reason-react-ppx
)
; Tags to categorize or label the package, useful for searches and indexing.
 (tags
  (reasonml, ocaml, melange)))

zw@zw:~/code/ocaml_programming/hello$ cat dune
; This dune file segment configures the build process for the project,
; focusing on web bundling.

; This stanza excludes the 'node_modules' directory from being
; considered by dune, which is a standard practice to prevent Dune from scanning
; dependency files.
(dirs :standard \ node_modules)

; The 'alias' block named 'all' defines a high-level alias for building the
; entire project. It specifies that the 'all' alias depends on another alias,
; typically named after the bundler being used (e.g., 'vite', 'webpack'). This
; setup allows for easy switching between different bundlers without changing
; the overall build process.
(alias
  (name all)
  (deps
     (alias_rec webpack)))

; The 'rule' block associated with the bundler alias (e.g., 'vite', 'webpack')
; defines the specific steps to build and bundle the web application.
;
; - 'targets (dir dist)' indicates the output directory for the bundled files.
;
; - The 'deps' section lists dependencies like the project alias, bundler config
;   files, and entry HTML file.
;
; - The 'action' specifies the system command to run the bundler, in this case a
;   script from 'node_modules' to run a bundler.
;
; - 'promote (until-clean)' in 'mode' is used for artifact promotion, managing built files.
(rule
  (alias webpack)
   (targets (dir dist))
   (deps
     (alias_rec hello)
     (:webpack ./webpack.config.js)
     (source_tree ./public))
   (action
    (system "../../node_modules/.bin/webpack --mode production \
              --entry ./output/src/App.mjs && cp ./public/index.html dist/index.html"))
   (mode
     (promote (until-clean))))

; The 'melange.emit' block is used to compile ReasonML/OCaml code to JavaScript.
; - 'target output' specifies where the compiled JS files will be placed.
; - It relies on the 'app' library and compiles using specified module systems
;   (here, es6 and mjs).
  (melange.emit
   (target output)
   (alias hello)
   (libraries app)
   (module_systems (es6 mjs)))

zw@zw:~/code/ocaml_programming/hello$ uname -r
5.15.146.1-microsoft-standard-WSL2
zwpdbh commented 3 months ago

I tried to fix error

webpack 5.91.0 compiled successfully in 1550 ms
cp: cannot stat './public/index.html': No such file or directory

I found there is no such public folder in the project directory. So, I manually create public folder, and move index.html into it.

Then, dune build, without error. At last, run npm run dev shows:

image