ohm: Uncaught TypeError: ohm.makeRecipe is not a function

i have a generated ohm bundle file in my npm package i use the npm package in my react app but the client browser that loads the react app is throwing Uncaught TypeError: ohm.makeRecipe is not a function error in my package.json: "ohm-js": "^16.3.2",

is this version related? should i generate the bundle differently? i generated by the following command npx ohm generateBundles --withTypes src/grammar.ohm

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

For anyone else, a temporary workaround for nextjs as of 16.4.0 is to change the require in your cli generated ohm-bundles:

let ohm;
if (typeof window !== 'undefined') ohm = require('ohm-js').default;
else ohm = require('ohm-js');

Never mind — I was able to reproduce this with webpack Ohm v16.3.4. It seems to be a problem related to ES modules and our use of a default export. For the record, I was able to fix it by modifying the .ohm-bundle.js file, replacing require('ohm-js') with require('ohm-js').default.

The good news is, it’s already fixed on main and will be in the next release of Ohm (v17) and @ohm-js/cli. I’m also adding a test that ensures that grammars bundled with webpack work properly.