我正在尝试将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/react@15.3.1/dist/react.js"></script>
<script type="text/javascript" src="https://unpkg.com/react-dom@15.3.1/dist/react-dom.js"></script>
<script type="text/javascript" src="/app/bundle.js"></script>
...
当我加载尝试在浏览器中加载它时,我的React应用程序无法加载。我可以在控制台窗口中看到以下错误消息:
Uncaught Error: Cannot find module 'react'
我不明白为什么反应没有被加载。
答案 0 :(得分:6)
你的package.json中没有React。您的项目很难使用尚未安装的软件包。
只需将"react": "^15.3.1"
添加到你的package.json并进行新的npm安装,你应该没问题。
答案 1 :(得分:0)
在本地目录中执行http://192.168.99.100
。
它对我有用。
答案 2 :(得分:0)
您打算在打字稿中使用react。试试这个
npm install --save react react-dom @types/react @types/react-dom
答案 3 :(得分:0)
我正在使用Gatsby,React放在我的 package.json 中,内容如下:
"react": "^16.7.0",
"react-dom": "^16.7.0",
我之前运行gatsby develop
时,它一直在工作。但是,当我安装了两个插件(npm install --save less gatsby-plugin-less
)时,每当我执行gatsby develop
时,就会弹出“错误:找不到模块'react'”。
奇怪的是,我只是在 package.json 文件夹上做了npm install
并解决了问题。
答案 4 :(得分:0)
我遇到了这个问题,就我而言,我指的是一个.env文件,但我的存储库中没有该文件。取回文件,它可以工作。确保检查项目中正在使用的这些文件类型。没有明确的错误信息。