【发布时间】:2017-05-11 15:37:33
【问题描述】:
我使用 Webpack 来捆绑我所有的 npm 模块。这是我的 webpack.config.js:
"use strict";
module.exports = {
entry: './main.js',
output: { path: __dirname, filename: 'bundle.js' },
module: {
loaders: [
{
test: /.js?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react']
}
},
{test: /\.json$/, loader: "json"},
]
},
};
这是我所指的 main.js。如您所见,我尝试导入 React、React-dom、fixed-data-table、chartjs、jquery 和 visjs。
import React from 'react';
import ReactDOM from 'react-dom';
import {Table, Column, Cell} from 'fixed-data-table';
import Chart from 'chartjs';
import jquery from 'jquery';
import vis from 'vis';
一切都很好,我从我的 index.html 中删除了 react、chartjs、jquery 等 src,并完全引用了新创建的 bundle.js。
在我的功能性 .js 文件中,内容派生自我的反应类,我将以下内容添加到开头(我假设错误源于此)
import React from './bundle';
import ReactDOM from './bundle';
import {Table, Column, Cell} from './bundle';
import Chart from './bundle';
import vis from './bundle';
这导致我的浏览器开发工具给我错误:未捕获的引用错误:React 未定义。
我在捆绑过程中哪里出错了?我假设捆绑很顺利,因为没有错误。但是如何正确导入,例如,在另一个 .js 文件中的 React?
这是我的 package.json:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.3.17",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-runtime": "^6.3.19",
"chartjs": "^0.3.24",
"webpack": "^1.12.9"
},
"dependencies": {
"chartjs": "^0.3.24",
"fixed-data-table": "^0.6.0",
"react": "^0.14.3",
"react-dom": "^0.14.3",
"vis": "^4.17.0"
}
}
【问题讨论】:
-
你的 package.json 是什么样的?仔细检查您是否实际安装了 react 依赖项。
-
我已编辑以包含 package.json。
标签: javascript jquery node.js webpack