为什么是一个支架' {'在尝试导入时发生意外的令牌反应?

时间:2016-08-22 20:59:43

标签: node.js reactjs npm webpack

我正在尝试使用Facebook的固定数据表模块,只需按照此页面上的基本示例操作: https://facebook.github.io/fixed-data-table/

但是,当我尝试使用webpack使用&nbsp运行prod'来运行' npm webpack -p':

SyntaxError: Unexpected token: punc ({) [./app/index.js:1,0]

这些只是index.js中的一行:

import {Table, Column, Cell} from 'fixed-data-table';

这是我的package.json文件的样子:

{
  "name": "datatables",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "prod": "webpack -p",
    "start": "webpack-dev-server"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "fixed-data-table": "^0.6.3",
    "react": "^15.3.1",
    "react-dom": "^15.3.1"
  },
  "devDependencies": {
    "babel-core": "^6.13.2",
    "babel-loader": "^6.2.5",
    "babel-preset-react": "^6.11.1",
    "css-loader": "^0.23.1",
    "html-webpack-plugin": "^2.22.0",
    "style-loader": "^0.13.1",
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.15.0"
  }
}

我只是想让基本的示例代码正常工作。感谢。

编辑:添加webpack:

var HtmlWebpackPlugin = require('html-webpack-plugin');
var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: __dirname + '/app/index.html',
    filename: 'index.html',
    inject: 'body'
});
var CssWebpack = new HtmlWebpackPlugin({
    template: __dirname + '/app/fixed-data-table.css',
    filename: 'fixed-data-table.css',
    inject: 'head'
});

module.exports = {
    entry: ['./app/index.js'],
    output: {
        path: __dirname + '/dist',
        filename: "index_bundle.js"
    },
    module: {
        loaders: [
            { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" },
            { test: /\.css$/, loader: "style-loader!css-loader" }
        ]
    },
    plugins: [HtmlWebpackPluginConfig]
}

1 个答案:

答案 0 :(得分:4)

Node目前不支持import语句。应该只需要将预设添加到您的webpack加载程序配置中,它就会正确传输

npm install babel-preset-es2015 babel-preset-react

module: {
    loaders: [
        { 
            test: /\.js$/,
            exclude: /node_modules/,
            loader: "babel-loader",
            presets: ["es2015", "react"]
        },
        { test: /\.css$/, loader: "style-loader!css-loader" }
    ]
},