Getting-started problem, Rescript/React

I’m trying to follow instructions for making a Rescript/React project, starting here: Installation | React , and then here : Installation | ReScript Language Manual as a kind of subroutine, and I need help making it work.

I apologize for the length of this note, but I wanted to be sure to include everything I tried, and exactly how it failed. Just in case it matters, I’m using MacOS 12.0.1.

Before I get into details, though:

When I’m done, I want to use the ReProcessing module, which is written in Reason rather than ReScript, and I I’d like to write my program that uses ReProcessing (or the fork of it found here: https://github.com/RawToast/reprocessing) in Reason rather than ReScript if possible. I worry that this Reason program will not be able to use React if it’s the ReScript version that I’ve installed. So my preliminary question is really this:

  1. Can I use a Reason module as part of a ReScript project?
  2. Can I write a Reason program as part of my project and connect with the ReScript/React stuff installed by following the instructions above?
  3. The Reprocessing project uses a bunch of “bsb”-type things to compile. Do I just replace “bsb” with “rescript” to make it work?

Assuming the answers to those three questions are “yes”, or something like it, here’s how the installation went, along with some confusion along the way. I apologize in advance for not understanding the whole “build” process in Reason/ReScript. I’ve tried to make sense of it, and failed.

Starting at https://rescript-lang.org/docs/react/latest/installation, I’m given “requirements”:

  • rescript@9.1.2 or later (alternatively for older codebases: bs-platform@8.3 or later)
  • react@16.8.1 or later

but no suggestion about how to meet them. One possibility is the install them globally, another is to install them for this project. I’m going to guess the latter. The first step seems to be to create a ReScript project using instructions here: Installation | ReScript Language Manual ; these require “node” and “npm”.

`

% node --version
v16.13.2
% npm --version
8.1.2

Then

% git clone https://github.com/rescript-lang/rescript-project-template
% cd rescript-project-template
% npm install
Cloning into 'rescript-project-template'...
[...]
found **0** vulnerabilities

% npm run build
> rescript-project-template@0.0.1 build
> rescript
rescript: [3/3] src/Demo.cmj
% node src/Demo.bs.js
Hello, World!

Looking good so far. Returning to the previous page, I need to install rescript@9.1.2 and react@16.8.1:

% npm install rescript@9.1.2 
changed 1 package, and audited 2 packages in 5s
found **0** vulnerabilities

% npm install react@16.8.1  
added 7 packages, and audited 9 packages in 1s
found **0** vulnerabilities

Then I’m told to “Add following dependency to your ReScript project”, but it appears that what’s meant is "type the following command, which will add a dependency to your package.json file after ". And right here, things go off the rails:

% npm install @rescript/react --save
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: **rescript-project-template**@**0.0.1**
npm ERR! Found: **react**@**16.14.0**
npm ERR! node_modules/react
npm ERR! **react**@"**^16.8.1**" from the root project
npm ERR! peer **react**@"**>=16.8.1**" from **@rescript/react**@**0.10.3**
npm ERR! node_modules/@rescript/react
npm ERR! **@rescript/react**@"*****" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer **react**@"**17.0.2**" from **react-dom**@**17.0.2**
npm ERR! node_modules/react-dom
npm ERR! peer **react-dom**@"**>=16.8.1**" from **@rescript/react**@**0.10.3**
npm ERR! node_modules/@rescript/react
npm ERR! **@rescript/react**@"*****" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR! See /Users/jfh/.npm/eresolve-report.txt for a full report.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/jfh/.npm/_logs/2022-02-09T20_08_51_074Z-debug.log

Making a best-guess attempt, I figured I’d try installing react@17.0.2, because that was the largest number I saw in the error report:

npm install react@17.0.2
removed 3 packages, changed 1 package, and audited 6 packages in 1s
found **0** vulnerabilities

and then once again try the previous step:

% npm install @rescript/react --save
added 3 packages, and audited 9 packages in 995ms
found **0** vulnerabilities

OK. Success (maybe); perhaps someone needs to edit those “how to get started” docs, though.

I edited my bsconfig.json file as instructed and created Test.res in the source directory:

% cat > src/Test.res
// src/Test.res
@react.component
let make = () => {
<div> {React.string("Hello World")} </div>
}

I’m then told to run npx rescript and I should see a successful build.

% npx rescript 
**Different compiler version**: clean current repo
Cleaning... 5 files.
Dependency on @rescript/react
rescript: [38/38] src/legacy/ReactDOMRe.cmj
rescript: [49/49] install.stamp
Dependency Finished
rescript: [5/6] src/Test.cmj
FAILED: src/Test.cmj
**We've found a bug for you!**
/Users/jfh/Desktop/example/rescript-project-template/src/Test.res:4:4-6
2 │ @react.component
3 │ let make = () => {
**4** │ <**div**> {React.string("Hello World")} </div>
5 │ }
6 │
The value div can't be found
rescript: [6/6] src/Demo.cmj
FAILED: cannot make progress due to previous errors.

…and with that, my ReScript/React adventure has ground to a halt.

Any suggestions? Did I do something wrong?

–John

Hi John, those steps seem to work for me. Here’s a suggestion:

Create a new empty directory.

Add this package.json:

{
  "name": "rescript-project-template",
  "version": "0.0.1",
  "scripts": {
    "build": "rescript",
    "clean": "rescript clean -with-deps",
    "start": "rescript build -w"
  },
  "keywords": [
    "rescript"
  ],
  "author": "",
  "license": "MIT",
  "dependencies": {
    "@rescript/react": "^0.10.3",
    "react": "^17.0.2",
    "rescript": "^9.1.2"
  }
}

Add this bsconfig.json

{
  "name": "rescript-project-template",
  "version": "0.0.1",
  "sources": {
    "dir" : "src",
    "subdirs" : true
  },
  "package-specs": {
    "module": "commonjs",
    "in-source": true
  },
  "suffix": ".bs.js",
  "bs-dependencies": ["@rescript/react"],
  "reason": { "react-jsx": 3 }
}

Add this src/Test.res

@react.component
let make = () => {
  <div> {React.string("Hello World")} </div>
}

Then run the commands:

npm i
npx rescript

Does that work for you?

Thanks very much for trying to help out.

That does work for me, yes. It worries me a bit that the other does not. I thought that maybe I had something installed that might be causing problems, so (in my home directory) I tried this:

% npm -g ls
/Users/jfh/.npm-global/lib
├── jshint@2.13.4
└── yarn@1.22.17

which seems benign enough. Maybe I should just quit now that I have something working, but…since I’m in this “don’t know what I’m doing, so I like to start from other working setups,” the fact that the standard installation led to defeat worries me.

Is it possible that the list of operations that I described worked for you because of things you already have set up in your environment (e.g., globally-installed packages)?

Also: the standard Rescript/React template has scripts to start a local server and see your project running, which I’d like to use if only I could make it work (or if I could modify this very simple setup that you’ve given me). Is there a simple way to do that? [Oops…I’m mistaken — that was some other template that I tried to copy/use. Apologies.]

Or was your sample here just a test to try to diagnose my problem?

I suspect the starter project was developed for Node 14, and doesn’t work immediately on Node 16, which may have been one of the problems you encountered.

I’m not aware of anything special to get it working, i.e. no globals, etc.

It’s usually pretty simple.

[1] Install rescript which is just the compiler, and
[2] add a bsconfig.json to the root of the project.

And add a couple of scripts into package.json to simplify running the compiler:

"res:build": "rescript",
"res:clean": "rescript clean",
"res:watch": "rescript build -w"

was your sample here just a test to try to diagnose my problem?

Yes, mostly to see if there was something more fundamental causing the problems for you. I’m just a general ReScript user without knowledge of its internals, and to date I haven’t had any problems. Hopefully your future experience goes more smoothly.

1 Like
  1. Yes (for now. It may break at some point in the future).
  2. Pretty much 100% yes. There are some minor points where ReScript syntax can do things ReasonML syntax can’t, but I doubt they’d come up in a React project.
  3. If it’s just executing bsb then yes rescript will do the same. It’s command line arguments that are different, I believe the common ones all have equivalents.

Thanks, Kevin. Your ultra-simple example has finally made it a little easier for me to understand (or at least guess) what’s going on with bsconfig and package.json, and to have the confidence that removing node_modules and starting over is a plausible thing to do when I get into some wedged state. Much obliged.

Thanks; given those answers, I’ll proceed onwards. It’d be a pity to get 90% done only to find out that what I was planning couldn’t be completely anyhow.