node.jsreact-nativeecmascript-6babel-cli

How to setup babel-cli to run react-native ES6 JavaScript code on command line?


My goal is to run ES6 JavaScript code containing console.log statements on the command line to help debug code before integrating with my React Native app. This code contains only JS functions.

This is what I tried:

Install babel-cli
   npm install --save-dev babel-cli

File: .babelrc ( in project root )
{
  "presets": ["react-native"]
}

Run sandbox:
./node_modules/.bin/babel-node app/components/sandbox.js

I get the following run-time error:

   /react-native/myapp/source/node_modules/react-native/Libraries/react-native/react-native.js:15
    if (__DEV__) {
        ^
        ReferenceError: __DEV__ is not defined

I had this working once.

What am I missing?


Solution

  • React Native is a different environment than Node, not just transpiled code. You cannot run React Native apps directly on Node like you cannot run web pages on Node.

    When testing, you can use the mocking feature of jest to mock React Native specific modules. The react-native preset for jest already does that.