简体   繁体   中英

Run react native es6 javascript through babel

What's the easiest way to run a javascript module that exists in my react native source tree using babel?

Let's say I have the following file

// hi.js
import _ from 'lodash'    
console.log(_.upperCase('hi'));

And I want to just run this quickly from the command line to test it out or use it in a utility script or something like that. I can't use node directly because of the import statement. I know I can install and configure babel... but since babel is already bundled and configured with RN can I just use that somehow?

eg > workspace/node_modules/babe-core/bin/babel-node hi.js

Can I just install babel-cli into the project?

Thanks!

You can compile a file using the babel command line:

babel hi.js --out-file hi-compiled.js --plugins=es2015,react

This will allow you to then run the file with node:

node hi-compiled.js

Alternatively you could use babel-node which would do it in one command:

babel-node hi.js --plugins es2015

All this can be achieved with npm install -g babel-cli

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM