Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

babel-node fails to require jsx file in node_modules

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:

  • How do I use babel in a node CLI program?
  • import a module from node_modules with babel but failed
  • http://babeljs.io/docs/usage/cli/
  • http://babeljs.io/docs/usage/options/
like image 622
Tri Noensie Avatar asked Jan 25 '16 20:01

Tri Noensie


1 Answers

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.

like image 84
Wilfred Hughes Avatar answered Oct 06 '22 01:10

Wilfred Hughes