Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import syntax not working with webpack

I got Illegal import declaration error. when I tried to integrated a react js repo with webpack

I migrated the original source code from https://github.com/dptoot/react-event-calendar/blob/master/example/src/example.js

How could I fix Illegal import declaration error ?

I think the import syntax only works in some js lib ?

Error

ERROR in ./app/main.js
Module build failed: Error: Parse Error: Line 2: Illegal import declaration
    at throwError (/Users/poc/sandbox/ha/node_modules/jsx-loader/node_modules/jstransform/node_modules/esprima-fb/esprima.js:2823:21)

main.js

var React = require('react');
const EventCalendar = require('react-event-calendar');

import moment from 'moment';
import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col';
import Button from 'react-bootstrap/lib/Button';
import ButtonToolbar from 'react-bootstrap/lib/ButtonToolbar';
import Popover from 'react-bootstrap/lib/PopOver';
import Overlay from 'react-bootstrap/lib/Overlay';

webpack.config.js

var path = require('path');
var webpack = require('webpack');


var config = module.exports = {
  // the base path which will be used to resolve entry points
  context: __dirname,
  // the main entry point for our application's frontend JS
  entry: './app/main.js',
  output: {
    filename: 'main.js'
  },

  resolve: {
      extensions: ['', '.js', '.jsx', '.ts']
  },  

  module: {
    loaders: [
         {
          test: /\.jsx?$/, 
          exclude: /node_modules/, 
          loader: 'jsx-loader?insertPragma=React.DOM&harmony' }
    ]
  }

};
like image 462
newBike Avatar asked Sep 23 '15 02:09

newBike


2 Answers

Use Babel via babel-loader to transform import declarations (and other ES2015 if you want). http://babeljs.io/docs/setup/#webpack

like image 75
JMM Avatar answered Sep 29 '22 22:09

JMM


As @JMM answered, it seems you need babel-loader. in addition, I was still facing the same problem, and finally get resolved by editing webpack.config.js such like

   module: {
     loaders: [
-      {test: /\.jsx?$/, loader: 'babel-loader'},
-      {test: /\.jsx$/, loader: 'jsx-loader'}
+      {test: /\.jsx$/, loader: 'jsx-loader'},
+      {test: /\.jsx?$/, loader: 'babel-loader'}
     ]
   },

or because jsx-loader looks no longer working with this config, it can be deleted.

I hope it would help

like image 26
otiai10 Avatar answered Sep 29 '22 23:09

otiai10