我正在尝试将React集成到现有的网页中。这时,我无法加载我的React应用程序。我的React应用程序有两个文件。这时,他们看起来像这样:
myApp.js
import React from 'react';
import ReactDOM from 'react-dom';
import MyComponent from './components/myComponent';
ReactDOM.render(<MyComponent />, document.getElementById('root'));
myComponent.js
import React from 'react';
import ReactDOM from 'react-dom';
class MyComponent extends React.Component {
render() {
console.log('here');
return (
<div>
<h2>Hello (from React)</h2>
<br />
</div>
);
}
}
export default MyComponent;
如您所见,我正在使用ES6。我决定在这个项目上使用ES6。出于这个原因,我在我的Gulp文件中使用Babel来捆绑我的React应用程序。我正在使用Gulp而不是Webpack,因为我的网站已经在使用Gulp。不过,我的package.json文件中的相关细节如下所示:
的package.json
...
"devDependencies": {
"babel-preset-es2015": "^6.14.0",
"babel-preset-react": "^6.11.1",
"babelify": "^7.3.0",
"browserify": "^13.1.0",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-clean-css": "^2.0.11",
"gulp-uglify": "^1.5.4",
"vinyl-source-stream": "^1.1.0"
}
然后我使用以下任务捆绑我的React应用程序:
gulpfile.js
gulp.task('buildApp', function() {
return browserify({ entries: ['./app/myApp.js', './app/components/myComponent.js'] })
.transform("babelify", {presets: ['es2015', 'react']})
.bundle()
.pipe(source('bundle.js'))
.pipe(gulp.dest('./app'))
;
});
运行上述任务时,将生成bundle.js文件。它看起来像这样:
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactDom = require('react-dom');
var _reactDom2 = _interopRequireDefault(_reactDom);
var _myComponent = require('./components/myComponent');
var _myComponent2 = _interopRequireDefault(_myComponent);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_reactDom2.default.render(_react2.default.createElement(NyComponent, null), document.getElementById('root'));
},{"./components/myComponent":2,"react":"react","react-dom":"react-dom"}],2:[function(require,module,exports){
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactDom = require('react-dom');
var _reactDom2 = _interopRequireDefault(_reactDom);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var MyComponent = function (_React$Component) {
_inherits(MyComponent, _React$Component);
function MyComponent() {
_classCallCheck(this, MyComponent);
return _possibleConstructorReturn(this, (MyComponent.__proto__ || Object.getPrototypeOf(MyComponent)).apply(this, arguments));
}
_createClass(MyComponent, [{
key: 'render',
value: function render() {
console.log('here');
return _react2.default.createElement(
'div',
null,
_react2.default.createElement(
'h2',
null,
'Hello (from React)'
),
_react2.default.createElement('br', null)
);
}
}]);
return MyComponent;
}(_react2.default.Component);
exports.default = MyComponent;
},{"react":"react","react-dom":"react-dom"}]},{},[1]);
然后,在我试图加载此应用程序的网页中,我有以下内容:
...
<div id="root"></div>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/react.js"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/react-dom.js"></script>
<script type="text/javascript" src="/app/bundle.js"></script>
...
当我加载尝试在浏览器中加载它时,我的React应用程序无法加载。我可以在控制台窗口中看到以下错误消息:
Uncaught Error: Cannot find module 'react'
我不明白为什么反应没有得到加载。
你的package.json中没有React。您的项目很难使用未安装的软件包。
只需将:"react": "^15.3.1"
添加到您的package.json并执行新的npm安装,您应该没问题。
您打算使用内部打字稿。试试这个
npm install --save react react-dom @types/react @types/react-dom
在您的本地目录中执行npm install react
。它对我有用。
我正在使用Gatsby和React在我的package.json中使用以下内容:
"react": "^16.7.0",
"react-dom": "^16.7.0",
当我跑gatsby develop
时,它以前工作。但是,当我安装了几个插件(npm install --save less gatsby-plugin-less
)时,每当我做gatsby develop
时,“错误:无法找到模块'反应'”就会弹出。
奇怪的是,我刚刚在package.json文件夹上做了npm install
,它解决了这个问题。