简体   繁体   English

gulp + browserify + reactjs,Uncaught ReferenceError:未定义React

[英]gulp + browserify + reactjs, Uncaught ReferenceError: React is not defined

I'm trying to follow workflow suggested in this file: https://gist.github.com/nateajohnson/4d16df279d2e3d2b6b16 , so that in the end I get two separate .js files: one for vendor libs and the other for my own code. 我正在尝试按照此文件中建议的工作流程进行操作: https//gist.github.com/nateajohnson/4d16df279d2e3d2b6b16 ,以便最终得到两个单独的.js文件:一个用于供应商库,另一个用于我自己的代码。

My Gulpfile.js: 我的Gulpfile.js:

var gulp = require('gulp'), 
// ... there shouldn't be any problems

gulp.task('build-vendor', function() {
    var b = browserify({ debug: false});

    b.require(nodeResolve.sync('jquery'), { expose: 'jquery' });
    b.require(nodeResolve.sync('react'), { expose: 'react' });
    b.require(nodeResolve.sync('react/addons'), { expose: 'react/addons' });
    b.require(nodeResolve.sync('react-select'), { expose: 'react-select' });

    var stream = b.bundle()
        .pipe(source('vendor.js'))
        .pipe(gulp.dest('assets/build/js'))
        .pipe(buffer())
        .pipe(uglify())
        .pipe(rename('vendor.min.js'))
        .pipe(gulp.dest('assets/build/js'))
        .pipe(gzip())
        .pipe(gulp.dest('assets/build/js'));

    return stream;
}) ;


gulp.task('build-app', function() {

    var b = browserify('assets/dev/js/app.js', { debug: false });

    b.external('jquery');
    b.external('react');
    b.external('react-select');

    var stream = b.transform(babelify, { stage: 0 })
        .bundle()
        .on('error', function(e){
            console.log(e.message);
            this.emit('end');
        })
        .pipe(source('bundle.js'))
        .pipe(gulp.dest('assets/build/js'));

    return stream;
});

When I'm trying to use React variable in my own code, I get the error: 当我在我自己的代码中尝试使用React变量时,我收到错误:

Uncaught ReferenceError: React is not defined 未捕获的ReferenceError:未定义React

I can get rid of this either by including 我可以通过包括摆脱这一点

var React = require('react');

in each of my js files which use react, or by including 在我的每个js文件中使用react或者包含

<script src="https://fb.me/react-0.13.3.min.js"></script>

in my HTML (which obviously not very good solution). 在我的HTML(这显然不是很好的解决方案)。

Just in case, I pushed this project on github: https://github.com/jehaby/ireact/tree/master/kindle 为了以防万一,我在github推动了这个项目: https//github.com/jehaby/ireact/tree/master/kindle

So my questions is why do I get this error and how to solve it the right way ( I'm sure there is some )? 所以我的问题是为什么我会得到这个错误以及如何以正确的方式解决它(我确定有一些)? I believe it might be stupid questions, but I'm new to this tools. 我相信这可能是愚蠢的问题,但我是这个工具的新手。 I've already spent several hours trying to figure out what's wrong and now feel kinda stuck. 我已经花了几个小时试图弄清楚什么是错的,现在觉得有点卡住了。 Will be very grateful for any help. 非常感谢任何帮助。

With browserify and babelify you should declare your dependencies in each file: 使用browserify和babelify,您应该在每个文件中声明您的依赖项:

//node's style
var React = require('react')
// ES 2015 modules
import React from 'react';

Take a look on available examples how to work with ES2015 modules and React: 查看可用示例如何使用ES2015模块和React:

https://github.com/lukehoban/es6features#modules https://github.com/lukehoban/es6features#modules

https://github.com/erikras/react-redux-universal-hot-example/blob/master/src%2Fcomponents%2FCounterButton.js https://github.com/erikras/react-redux-universal-hot-example/blob/master/src%2Fcomponents%2FCounterButton.js

Define 限定

React=require('react');
ReactDOM = require('react-dom');

those global javascript value in a App.js, than put this App.js中的那些全局javascript值,而不是这个

var stream = require('vinyl-source-stream');
var browserify = require('browserify');
    browserify(source + '/app/app.js')
        // bundles it and creates a file called main.js
        .bundle()
        .pipe(stream('main.js'))
        // saves it the dest directory
        .pipe(gulp.dest(destination +'/assets/js'));

in Gulpfile.js. 在Gulpfile.js中。

And for last, put main.js in HTML, and it should work. 最后,将main.js放在HTML中,它应该可以工作。

The problem is if we use var React=require('react'), the object React is not visible from other script, but React=require('react') define a global value. 问题是如果我们使用var React = require('react'),则从其他脚本看不到对象React,但是React = require('react')定义了一个全局值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM