Search code examples
webpackreasonbucklescriptreason-react

Can't resolve ReasonReact.js in webpack


I'm trying to get ReasonML working with an existing React code base. I'm not using create-react-app. This is a hand-built project. For the most part, it looks like everything is set up fine. However, I'm getting an error when I try to run Webpack with the default compiled bs.js files.

Error: Can't resolve '../lib/js/src/ReasonReact.js'

My bsconfig.js is almost directly copy/pasted from the ReasonML docs:

{
  "name": "reason-react",
  "reason": {"react-jsx" : 2},
  "sources": ["src"],
  "package-specs": [{
    "module": "commonjs",
    "in-source": true
  }],
  "suffix": ".bs.js",
  "namespace": true,
  "bs-dependencies": ["reason-react"],
  "refmt": 3
}

Bucklescript is generating the appropriate bs.js files in the location where I'd expect them. All well and good. However, I'm not surprised that Webpack can't find what it's looking for. There is no /lib/js/src/ReasonReact.js file. There's a ReasonReact.js file in /lib/bs, and one in node_modules/reason-react/lib/js.

When I edit the compiled ReasonML files, and point the ReasonReact variable to the node_modules ReasonReact.js file, everything works.

I've looked over several tutorials and articles. Maybe I've missed something, but I can't find anything about the error I'm encountering. Does anyone have any ideas as to why the pathing would be off in the compiled bs.js file?


Solution

  • The problem is that you have named your project reason-react. In combination with having namespace turned on, this creates a local namespace module called ReasonReact which interferes with module resolution of the dependency.

    You could probably fix this just by setting namespace to false in bsconfig.json, since it doesn't sound like you are making a library that is going to be consumed by others and therefore don't need namespacing anyway. But you should also avoid using the same name as one of your dependencies since it so easily causes confusing issues like this.

    Remember to change the name in both bsconfig.json and package.json to avoid issues.