14

So I've been playing around with React.js, gulp and browserify. Everything works great until I try to require a module in my main.js file. I am requiring components/modules in other files with no problem (see below), but when I try to require one in my main.js file, I receive the following error when running gulp serve:

Error: Cannot find module './components/Feed' from '/Users/kevin/Documents/MyWork/web/react/app/src/js' at /Users/kevin/Documents/MyWork/web/react/node_modules/browserify/node_modules/resolve/lib/async.js:55:21 at load (/Users/kevin/Documents/MyWork/web/react/node_modules/browserify/node_modules/resolve/lib/async.js:69:43) at onex (/Users/kevin/Documents/MyWork/web/react/node_modules/browserify/node_modules/resolve/lib/async.js:92:31) at /Users/kevin/Documents/MyWork/web/react/node_modules/browserify/node_modules/resolve/lib/async.js:22:47 at Object.oncomplete (fs.js:107:15)

If I remove the require (for Feed.js) statement from main.js, everything compiles and gets put in the appropriate dist folder and runs fine (except for react.js complaining about a missing module, of course).

Firstly, my folder structure looks like this:

app
│-- gulpfile.js
│-- package.json
│
└───src
│   │
│   ├───js
│       │-- main.js
│       └───components
│            │-- Feed.js
│            │-- FeedList.js
│            │-- FeedItem.js
│            │-- FeedForm.js
│            │-- ShowAddButton.js
│            └
│   
└───dist

My gulpfile looks like this:

var gulp = require('gulp'),
    connect = require('gulp-connect'),
    open = require("gulp-open"),
    browserify = require('browserify'),
    reactify = require('reactify'),
    source = require("vinyl-source-stream"),
    concat = require('gulp-concat'),
    port = process.env.port || 3031;

// browserify and transform JSX
gulp.task('browserify', function() {
    var b = browserify();
    b.transform(reactify);
    b.add('./app/src/js/main.js');
    return b.bundle()
        .pipe(source('main.js'))
        .pipe(gulp.dest('./app/dist/js'));
});

// launch browser in a port
gulp.task('open', function(){
    var options = {
        url: 'http://localhost:' + port,
    };
    gulp.src('./app/index.html')
    .pipe(open('', options));
});

// live reload server
gulp.task('connect', function() {
    connect.server({
        root: 'app',
        port: port,
        livereload: true
    });
});

// live reload js
gulp.task('js', function () {
    gulp.src('./app/dist/**/*.js')
        .pipe(connect.reload());
});

// live reload html
gulp.task('html', function () {
    gulp.src('./app/*.html')
    .pipe(connect.reload());
});

// watch files for live reload
gulp.task('watch', function() {
    gulp.watch('app/dist/js/*.js', ['js']);
    gulp.watch('app/index.html', ['html']);
    gulp.watch('app/src/js/**/*.js', ['browserify']);
});

gulp.task('default', ['browserify']);

gulp.task('serve', ['browserify', 'connect', 'open', 'watch']);

My main.js file looks like this:

var React = require('react'),
    Feed = require('./components/Feed');

React.renderComponent(
    <Feed />,
    document.getElementById('app')
);

The Feed.js file looks like:

var React = require('react');
var FeedForm = require('./FeedForm');
var ShowAddButton = require('./ShowAddButton');
var FeedForm = require('./FeedForm');
var FeedList = require('./FeedList');

var Feed = React.createClass({

    getInitialState: function() {
        var FEED_ITEMS = [
            { key: '1', title: 'Just something', description: 'Something else', voteCount: 49 },
            { key: '2', title: 'Some more stuff', description: 'Yeah!', voteCount: 34 },
            { key: '3', title: 'Tea is good', description: 'yepp again', voteCount: 15 },
        ];

        return {
            items: FEED_ITEMS
        };
    },

    render: function() {
        return (
            <div>
                <div className="container">
                    <ShowAddButton />
                </div>

                <FeedForm />

                <br />
                <br />

                <FeedList items={this.state.items} />
            </div>
        );
    }

});

module.exports = Feed;

I'm sure I'm overlooking something really simple...but I've been at this for hours and can't seem to crack it. Any help would be greatly appreciated. (Obviously I'm not posting the code for the other components for the sake of being as brief as possible...but they aren't the problem).

6
  • var Feed.js? Is that a typo? Commented Mar 1, 2015 at 0:00
  • Sorry...yes...corrected. Commented Mar 1, 2015 at 0:02
  • 2
    Can you please create a github repo with all the code? I created a project with the code you provided and it built correctly. Commented Mar 1, 2015 at 0:43
  • @SteveLacy Alright...github repo is here github.com/kevindeleon/react-test This is basically some test code from a lesson I found online, but that's the exact code that I am running on my system that is failing for me. OS X yosemite, if it makes any difference. Commented Mar 1, 2015 at 1:12
  • @SteveLacy -- also...obviously one would need to run npm i to install all needed node_modules....but I assume anyone reading this already likely knows that. Commented Mar 1, 2015 at 1:15

3 Answers 3

30

Your filenames are not what you think they are. Note this:

$ find app -type f | awk '{print "_"$0"_"}'
_app/dist/js/main.js_
_app/index.html_
_app/src/js/components/Feed.js _
_app/src/js/components/FeedForm.js _
_app/src/js/components/FeedItem.js_
_app/src/js/components/FeedList.js_
_app/src/js/components/ShowAddButton.js_
_app/src/js/main.js_

Your Feed.js file is actually Feed.js<SPACE>. Same for FeedForm.js. Renaming them makes your example repo build fine.

Sign up to request clarification or add additional context in comments.

4 Comments

Holy crap man...thanks so much! I have no idea how that happened. You saved me hours! I feel like a goon. :(
Wow! I'm very curious how you noticed this :-)
@FakeRainBrigand If I had to guess...he probably just had a hunch and then ran the command he left above ($ find app -type f | awk '{print "_"$0"_"}') to confirm.
I got lucky. I was typing in the file name and zshs tab completion completed the filename with a space at the end, then I ran that command above to see if there were any others.
2

Please try with code:

var React = require('react');
var FeedForm = require('./FeedForm.jsx');
var ShowAddButton = require('./ShowAddButton.jsx');
var FeedForm = require('./FeedForm.jsx');
var FeedList = require('./FeedList.jsx');

My issues resolved just after adding file extention(.jsx)

Thanks Sulok

3 Comments

Explanation please...?
his explanation is that you need to explicitly specify the file extensions. worked for me.
@s2t2 in gulpfile.js you can specify var bundler = browserify({ entries: ['./scripts/jsx/app.jsx'], transform: [reactify], extensions: ['.jsx'], debug: true, cache: {}, packageCache: {}, fullPaths: true }); extensions property is important then you can remove .jsx extension
0

May be the Quick Solution is..

  "start": "watchify ./src/js/* ./src/jsx/*.jsx -v -t babelify -o ./build/app/bundle.js"

in your Script add *.jsx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.