Search code examples
javascriptruby-on-railsrubynode.jsbrowserify

formsy-react npm module breaks rails app when I require it using browserify-rails


I have a combination of rails/sprockets and browserify that really makes things complicated for debugging. It looks like I have an issue with formsy-react and not with other modules.

This works:

//= require mysprocketslibs
window.mymodule = require('mymodule');
mymodule.simplefn();

(I wrote a bare bones module to test this, and a different third party module also worked)

But I have an error when I require formsy-react in my rails app's JS:

Uncaught TypeError: Cannot read property '0' of undefined

The offending line is at the point where browserify wraps the code with require in a function:

},{}],"/Users/me/myproject/node_modules/formsy-react/node_modules/form-data-to-object/index.js":[
function(require,module,exports){
arguments[4]["/Users/me/myproject/node_modules/formsy-react-components/node_modules/formsy-react/node_modules/form-data-to-object/index.js"][0].apply(exports,arguments)
},

In other words the browser thinks that arguments[4][...form-data-to-object/index.js] is undefined.

The file index.js is present in the filesystem where that path is pointing.

package.json:

{
   "name": "myproject",
   "private": true,
   "dependencies": {
     "browserify": "~> 10.2.4",
     "browserify-incremental": "^3.0.1",
     "formsy-react": "^0.17.0",
     "react": "^0.14.0",
     "mymodule": "1.0.0"
   },
   "engines": {
      "node": ">= 0.10"
   }
 }

I doubt this is formsy-react issue, because surely someone else would have encountered the problem since it is immediately at the very first time the module is required.


Solution

  • The issue was that browserify-rails caches dependencies and my application.js was still referring to a previously uninstalled module. In this case formsy-react-components which I should have spotted in the offending line above.

    Fixed by running rake tmp:cache:clear.