Ad

Babel-node Fails To Require Jsx File In Node_modules

- 1 answer

I know babel-node ignores node_modules by default, so I ran it three different ways to override it, all failed:

  1. ran babel-node app.js with .babelrc:

    {
      "presets": ["es2015", "react"],
      "only": [
        "app",
        "node_modules/react-components"
      ]
    }
    

    result: SyntaxError: Unexpected token < for the required jsx node module

  2. ran babel-node app.js with .babelrc:

    {
      "presets": ["es2015", "react"],
      "ignore": "node_modules\/(?!react-components)"
    }
    

    result: SyntaxError: Unexpected token < for the require jsx node module

  3. ran babel-node ./bin/www --ignore '/node_modules/(?!react-components) with .babelrc:

    {
      "presets": ["es2015", "react"]
    }
    

    result:

    [project]/node_modules/babel-preset-react/node_modules/babel-plugin-transform-react-jsx/lib/index.js:12
        var visitor = require("babel-helper-builder-react-jsx")({
                                                               ^
    
    TypeError: object is not a function
    

Using the register hook with the ignore option worked correctly.

ran node app.js with this code in the beginning of app.js

require('babel-core/register')({
    ignore: /node_modules\/(?!react-components)/
});

Even though this works, I still want to know why my implementations with babel-node does not work. Thanks.


references:

Ad

Answer

This is a known bug in babel, where it ignores only and ignore.

The relevant bug is T6726, which has been fixed in babel 6.14.0.

Ad
source: stackoverflow.com
Ad