Babel Plugin Module Alias not working w/ require('react')

704 Views Asked by At

I am building an app with electron-forge, that is not really important other than that it extends the .babelrc to .compilerc in my project configuration.

I am using infernojs / inferno-compat and babel-plugin-module-resolver, to alias references to react to inferno-compat

.compilerc / .babelrc

"plugins": ["babel-plugin-inferno", "transform-async-to-generator", "transform-es2015-classes",
          ["module-resolver", {
                    "root": ["."],
                    "alias": {
                        "react": "inferno-compat",
                        "react-dom": "inferno-compat"
                    }
                }]],

So far this works fine w/ .jsx files, but when I install a package, like react-bootstrap-modal that has a line like:

Modal.js:

var react = require('react')

I an error:

Uncaught Error: Cannot find module 'react' at Module._resolveFilename (module.js:485:15)

1

There are 1 best solutions below

0
On

Apparently aliasing modules in packages does not work, because Babel does not compile third party modules by default.

My workaround was to symlink inferno-compat to react and react-dom:

cd node_modules
ln -s inferno-compat react
ln -s inferno-compat react-dom

As a side note: Git works nicely with symlinks. If you check in these symlinks, Git will treat them as normal files and not resolve them.