将“require”库导入ReactJS项目

时间:2017-04-06 09:51:34

标签: node.js reactjs npm require

我正在学习 ReactJS 语言,现在我需要将请求库添加到我的项目中。所以我通过npm install命令从github.com/request/request下载了库,现在我需要将这个库导入到我的项目中。

但是当我使用时:

import Request from 'request';

var request = require('request');

然后我无法构建我的应用程序(由于一些错误)。我不确定,如果我可以通过这种方式将第三方模块(或如何调用)导入 ReactJS

你可以给我一点帮助吗?

====错误包括====

在我的.jsx文件中,我有:

import React from 'react';
var request = require('request');
import {Table, TableBody, TableFooter, TableHeader, TableHeaderColumn, TableRow, TableRowColumn} from 'material-ui/Table';
import TextField from 'material-ui/TextField';
import RaisedButton from 'material-ui/RaisedButton';
import FloatingActionButton from 'material-ui/FloatingActionButton';
import DialogWindow from '../components/DialogWindow.jsx'

import ContentAdd from 'material-ui/svg-icons/content/add';
import PlayArrow from 'material-ui/svg-icons/av/play-arrow';
import PlaylistAdd from 'material-ui/svg-icons/av/playlist-add';

如您所见,请求在第二行导入。当我尝试构建我的应用程序时,我会收到此错误:

ERROR in ./~/forever-agent/index.js
Module not found: Error: Can't resolve 'net' in 'folder with project\node_modules\forever-agent'
 @ ./~/forever-agent/index.js 6:10-24
 @ ./~/request/request.js
 @ ./~/request/index.js
 @ ./src/containers/Album.jsx
 @ ./src/routes.js
 @ ./src/index.jsx
 @ multi (webpack)-dev-server/client?http://127.0.0.1:8888 webpack/hot/dev-server react-hot-loader/patch ./src/index.jsx

ERROR in ./~/tough-cookie/lib/cookie.js
Module not found: Error: Can't resolve 'net' in 'folder with project\node_modules\tough-cookie\lib'
 @ ./~/tough-cookie/lib/cookie.js 32:10-24
 @ ./~/request/lib/cookies.js
 @ ./~/request/index.js
 @ ./src/containers/Album.jsx
 @ ./src/routes.js
 @ ./src/index.jsx
 @ multi (webpack)-dev-server/client?http://127.0.0.1:8888 webpack/hot/dev-server react-hot-loader/patch ./src/index.jsx

ERROR in ./~/tunnel-agent/index.js
Module not found: Error: Can't resolve 'net' in 'folder with project\node_modules\tunnel-agent'
 @ ./~/tunnel-agent/index.js 3:10-24
 @ ./~/request/lib/tunnel.js
 @ ./~/request/request.js
 @ ./~/request/index.js
 @ ./src/containers/Album.jsx
 @ ./src/routes.js
 @ ./src/index.jsx
 @ multi (webpack)-dev-server/client?http://127.0.0.1:8888 webpack/hot/dev-server react-hot-loader/patch ./src/index.jsx

ERROR in ./~/forever-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'folder with project\node_modules\forever-agent'
 @ ./~/forever-agent/index.js 7:10-24
 @ ./~/request/request.js
 @ ./~/request/index.js
 @ ./src/containers/Album.jsx
 @ ./src/routes.js
 @ ./src/index.jsx
 @ multi (webpack)-dev-server/client?http://127.0.0.1:8888 webpack/hot/dev-server react-hot-loader/patch ./src/index.jsx

ERROR in ./~/tunnel-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'folder with project\node_modules\tunnel-agent'
 @ ./~/tunnel-agent/index.js 4:10-24
 @ ./~/request/lib/tunnel.js
 @ ./~/request/request.js
 @ ./~/request/index.js
 @ ./src/containers/Album.jsx
 @ ./src/routes.js
 @ ./src/index.jsx
 @ multi (webpack)-dev-server/client?http://127.0.0.1:8888 webpack/hot/dev-server react-hot-loader/patch ./src/index.jsx

ERROR in ./~/request/lib/har.js
Module not found: Error: Can't resolve 'fs' in 'folder with project\node_modules\request\lib'
 @ ./~/request/lib/har.js 3:9-22
 @ ./~/request/request.js
 @ ./~/request/index.js
 @ ./src/containers/Album.jsx
 @ ./src/routes.js
 @ ./src/index.jsx
 @ multi (webpack)-dev-server/client?http://127.0.0.1:8888 webpack/hot/dev-server react-hot-loader/patch ./src/index.jsx

1 个答案:

答案 0 :(得分:0)

看起来像库的已知问题。在https://github.com/request/request/issues/1529(向Webpack配置添加colourID配置对象)中描述了修复,以使库在浏览器中工作。

但是,您可能需要考虑替代库,例如isomorphic-fetchaxios