SyntaxError: Unexpected token .
Hi, there is an Error when I import component to server side for rendering:
/react-bootstrap-table/dist/react-bootstrap-table.min.css:1
(function (exports, require, module, __filename, __dirname) { .react-bs-table .react-bs-container-header .sort-column,td.re
act-bs-table-expand-cell{cursor:pointer}.react-bs-table-container .react-bs-table-search-form{margin-bottom:0}.react-bs-tab
le-bordered{border:1px solid #ddd;border-radius:5px}.react-bs-table table{margin-bottom:0;table-layout:fixed}.react-bs-tabl
e table td,.react-bs-table table th{overflow:hidden;white-space:nowrap;text-overflow:ellipsis}.react-bs-table-pagination{ma
rgin-top:10px}.react-bs-table-tool-bar{margin-bottom:5px}.react-bs-container-header{overflow:hidden;width:100%}.react-bs-co
ntainer-body{overflow:auto;width:100%}.react-bootstrap-table-page-btns-ul{float:right;margin-top:0}.react-bs-table .table-b
ordered{border:0;outline:0!important}.react-bs-table .table-bordered>thead>tr>td,.react-bs-table .table-bordered>thead>tr>t
h{border-bottom-width:2px}.react-bs-ta
SyntaxError: Unexpected token .
^
The reason why I am trying to render react page in server is about react-router. It doesn't allow me to refresh a not index page or type url directly to go into a specific page, otherwise there will be 404 not found.
So I have to render react to html when there is a http request that not for default index.html coming in.
Libs version
{
"scripts": {
"start": "node ./server/app.js",
"dev": "NODE_ENV=development node dev-server.js",
"build": "rimraf ./dist && NODE_ENV=production webpack"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "0.16.1",
"koa": "^2.2.0",
"koa-bodyparser": "^4.2.0",
"koa-router": "^7.2.0",
"koa-static": "^3.0.0"
"koa-ejs": "^4.0.0",
"lodash": "^4.17.4",
"prop-types": "15.5.10",
"qs": "^6.4.0",
"react": "15.5.4",
"react-bootstrap": "0.31.0",
"react-bootstrap-table": "^3.3.8",
"react-burger-menu": "2.0.2",
"react-dom": "15.5.4",
"react-redux": "5.0.5",
"react-router-dom": "4.1.1",
"redux": "3.6.0",
"redux-thunk": "2.2.0"
},
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^0.28.3",
"extract-text-webpack-plugin": "^2.1.0",
"file-loader": "^0.11.1",
"html-webpack-plugin": "^2.28.0",
"image-webpack-loader": "^3.3.1",
"jsx-loader": "^0.13.2",
"react-hot-boilerplate": "^1.0.0",
"react-hot-loader": "^1.3.1",
"rimraf": "^2.6.1",
"style-loader": "^0.18.1",
"url-loader": "^0.5.8",
"webpack": "2.5.0",
"webpack-dev-server": "^2.4.5"
}
}
server.js
const Koa = require('koa');
const serve = require('koa-static');
const bodyParser = require('koa-bodyparser');
const router = require('koa-router')();
require('babel-core/register')({
presets: ['es2015', 'stage-0','react']
});
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import render from 'koa-ejs';
import path from 'path';
import {StaticRouter} from 'react-router';
import App from './src/routes';
let app = new Koa();
app.use(bodyParser());
app.use(async(ctx, next) => {
console.log(`Process ${ctx.request.method} ${ctx.request.url}...`);
await next();
});
render(app, {
root: path.join(__dirname, '../dist'),
layout: 'index',
viewExt: 'html',
cache: false,
})
router.get('/*', async(ctx, next) => {
let url = ctx.url;
let context = {};
let markup = ReactDOMServer.renderToString(
<StaticRouter
location={url}
context={context}>
<App />
</StaticRouter>
)
console.log('url', url);
await ctx.render('index',markup);
});
app.use(serve('./dist'));
/app.use(router.routes());
let port = process.env.PORT || 9000;
app.listen(port,()=>{
console.log(`listening on: ${port}`);
});
update:
about TypeError: Super expression must either be null or a function, not undefined
I guess it is a bug from yarn, because some time when I add and change libs by yarn, this error will come, and even I re-install by yarn, it still happened.
So my solution is delete the whole nodes_module and run npm i to re-install all modules.
SyntaxError: Unexpected token . just comes out when I import those components which contain react-bootstrap-table, however, it works fine if don't import them in server side. Any advises for the error ?
Thank you so much.
dod you have correct css loader and configuration for webpack, because it seems like something wrong from CSS, right?
I found this issue came from importing the css file in jsx file directly, import /node_modules/react-bootstrap/dist/react-bootstrap-table.min.css. So I just delete the import then it works well.
Thanks @AllenFang
Nice !