0

I created a react jsx file (index.jsx) as shown below. The first line is used to import jquery library in our class.

import $ from 'jquery'
import React from 'react';
import ReactDOM from 'react-dom';

import './main.css';
import './component';
import Search from './search/search'

when I started the webpack-dev-server, I got below error:

index.jsx Line 1: Unexpected token
You may need an appropriate loader to handle this file type.
| import $ from 'jquery';
| import React from 'react';
| import ReactDOM from 'react-dom'

I have added the jquery and react dependencies on my package.json file. I wonder what I missed here.

1
  • Are you using a compiler such as babel? Can u post your webpack.config file? Commented Apr 8, 2016 at 15:40

2 Answers 2

2

The question is tagged as React, but I don't see it. If you want to use React, do something like:

HelloWorld.js:

import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';

var HelloWorld = React.createClass({
    render: function(){
        return <h1> Hello World! </h1>
    }
});

ReactDOM.render(<HelloWorld />, document.getElementById('content'));

index.html:

<html>
    <head>
        <title>Hello World Example</title>
    </head>
    <body>
        <div id='content'/>
    </body>
</html>

If you don't want to use React, please post your other code so we can see how everything couples together, and where things go wrong.

Sign up to request clarification or add additional context in comments.

1 Comment

I do want to use react but I got the error before importing react. Actually, I tried your code but got this error: "index.jsx Line 1: Unexpected token You may need an appropriate loader to handle this file type." I have changed the post to include this error.
0

juste do :

var $ = require("query");
        module.exports = { 
          function(){

           var element = document.createElement('h1');

           element.innerHTML = 'Hello world';

           return element;
         }
    }

another syntaxe can be :

var $ = require("query");
    module.exports = { 
              myFunction : function(){

               var element = document.createElement('h1');

               element.innerHTML = 'Hello world';

               return element;
             }
        }

2 Comments

I want to import jquery can other libraries, where should I place the import code?
at the top of module export like you did, can be : var $ = require("query");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.