node.js - How to compile JSX with Babel into JavaScript -
i'm trying compile simplest react tutorial using browserify. i've run:
sudo npm install browserify
then stated here http://babeljs.io/docs/setup/#browserify
sudo npm install --save-dev babelify
then have file js/script.jsx
var hellomessage = react.createclass({ render: function() { return <div>hello {this.props.name}</div>; } }); reactdom.render(<hellomessage name="john" />, mountnode);
and i'm running folder
browserify js/script.jsx -t babelify --outfile bundle.js
but happens:
djave @ djaves-imac-3 in /volumes/djave/react $ browserify js/script.jsx -t babelify --outfile bundle.js syntaxerror: /volumes/djave/react/js/script.jsx: unexpected token (4:11) 2 | var hellomessage = react.createclass({ 3 | render: function() { > 4 | return <div>hello {this.props.name}</div>; | ^ 5 | } 6 | }); 7 |
what doing wrong? literally first step area may i've missed pretty major step.
more errors (although suspect won't much!)
@ parser.pp.raise (/volumes/djave/react/node_modules/babelify/node_modules/babel-core/node_modules/babylon/index.js:1425:13) @ parser.pp.unexpected (/volumes/djave/react/node_modules/babelify/node_modules/babel-core/node_modules/babylon/index.js:2907:8) @ parser.pp.parseexpratom (/volumes/djave/react/node_modules/babelify/node_modules/babel-core/node_modules/babylon/index.js:754:12) @ parser.pp.parseexprsubscripts (/volumes/djave/react/node_modules/babelify/node_modules/babel-core/node_modules/babylon/index.js:509:19) @ parser.pp.parsemaybeunary (/volumes/djave/react/node_modules/babelify/node_modules/babel-core/node_modules/babylon/index.js:489:19) @ parser.pp.parseexprops (/volumes/djave/react/node_modules/babelify/node_modules/babel-core/node_modules/babylon/index.js:420:19)
all fixed, @azium.
first react preset: http://babeljs.io/docs/plugins/preset-react/
npm install babel-preset-react
next, create file called .babelrc in root of project, , put in following:
{ "presets": ["react"] }
then
browserify js/script.jsx -t babelify --outfile bundle.js
that compiles everything, , make sure react
, react-dom
included (as shown here) can run following:
sudo npm install --save react react-dom
Comments
Post a Comment