Cannot find module using gulp and react

subro

I am new to react.js and somehow i have setup react environment with gulp build tool. I want to create custom React component and use it in another component. Please look at my code and help me.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>React and ES6</title>
</head>
<body>

    <div id="testdiv"></div>

    <script src="web/js/dist/login.js"></script>
</body>
</html>

Header.js

import React from "react";

export class Header extends React.Component {
  render(){
    return (
      <nav class="navbar navbar-static-top">
        <div class="navbar image-container"></div>
        <nav class = "navbar menu-container">
          <ul>
            <li><a>Home</a></li>
            <li><a>About</a></li>
            <li><a>Contact Us</a></li>
          </ul>
        </nav>
      </nav>
    );
  }
}

and Login.js

import React from "react";
import {render} from "react-dom";
import {Header} from "./components/Header.js";

class Login extends React.Component {

  render(){
    return(
      <div>
        <Header/>
        <h1>Login Test!!</h1>

      </div>
    );
  }
}
render(<Login/>, window.document.getElementById("testdiv"));

gulpfile.js

// declarations, dependencies
// ----------------------------------------------------------------------------
var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var gutil = require('gulp-util');
var babelify = require('babelify');
var notifier = require('node-notifier');
var watchify = require('watchify');

// External dependencies you do not want to rebundle while developing,
// but include in your application deployment
var dependencies = [
    'react',
    'react-dom'
];

// Gulp tasks
// ----------------------------------------------------------------------------
gulp.task('scripts', function () {
    bundleApp(true);
});

gulp.task('deploy', function (){
    bundleApp(true);
});

gulp.task('watch', function () {
    gulp.watch([
    './app/Controller/*.js'
  ], ['scripts']);
});


var notify = function(title, message) {
  notifier.notify({
    title: title,
    message: message
  });
  gutil.log(title + ': ' + message);
};

// When running 'gulp' on the terminal this task will fire.
// It will start watching for changes in every .js file.
// If there's a change, the task 'scripts' defined above will fire.
gulp.task('default', ['scripts','watch']);

var reactFiles = {
  path: [
    {
      from: ['app/components/Header.js'],
      to: 'Header.js'
    },
    {
      from: ['app/components/Home.js'],
      to: 'Home.js'
    },
        {
      from: ['app/controller/login.js'],
      to: 'login.js'
    }
  ],
  watchPath: ['web/controller/*.js']
};

// Private Functions
// ----------------------------------------------------------------------------
function bundleApp(isProduction) {
    // Browserify will bundle all our js files together in to one and will let
    // us use modules in the front end.
  var finished = 0;
  reactFiles.path.map(function(reactModuleEntry){
    var appBundler = browserify(reactModuleEntry.from)
    .transform(babelify, { presets: ['es2015', 'react'] })
    .bundle();

    appBundler.pipe(source(reactModuleEntry.to))
    .pipe(gulp.dest('./web/js/dist'))
    .on('finish', function() {
            finished++;
            if (finished === (reactFiles.path.length - 1)) {
              notify('Reactify', 'build: done')
              //done();
            }
          });
  });

}

Directory Structure is

ReactPractice
|
|---index.html
|---gulpfile.js
|--app
    |---Components
    |             |--Header.js
    |---Controller
    |             |--Login.js

Error when gulp file is run

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: Cannot find module './components/Header.js' from '/home/subro/reactpractice/app/controller'
    at /home/subro/reactpractice/node_modules/browser-resolve/node_modules/resolve/lib/async.js:55:21
    at load (/home/subro/reactpractice/node_modules/browser-resolve/node_modules/resolve/lib/async.js:69:43)
    at onex (/home/subro/reactpractice/node_modules/browser-resolve/node_modules/resolve/lib/async.js:92:31)
    at /home/subro/reactpractice/node_modules/browser-resolve/node_modules/resolve/lib/async.js:22:47
    at Object.oncomplete (fs.js:107:15)
Yosef Weiner

The path in the import statement is relative to where it is called from. Relative to Login.js, you need to move up a directory to access components/Header.js. Change the import to:

import {Header} from '../components/Header';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Importing images in TypeScript React - "Cannot find module"

From Java

Gulp Error: Cannot find module 'jshint/src/cli'

From Java

Can't get Gulp to run: cannot find module 'gulp-util'

From Dev

Receiving 'Error: Cannot find module' using browserify, gulp, react.js

From Dev

Cannot find module 'karma' while using grunt

From Dev

Gulp with browserify: Cannot find module src/js/main.js

From Dev

Gulp with browserify: Cannot find start module

From Dev

browserify cannot find module 'react'

From Dev

Gulp require "Cannot Find Module" even though it exists

From Dev

Browserify: Cannot find module '...' from '...' (Coffeescript + React)

From Dev

Gulp - throw err cannot find module 'q'

From Dev

Gulp watch doesn´t work - Cannot find module livereload

From Dev

Error: Cannot find module 'gulp-uglifyjs' with "gulp watch

From Dev

Gulp Error: Cannot find module browserify

From Dev

Karma Not Running - cannot find module gulp

From Dev

Got "Cannot find module 'angular2/angular2'" using gulp-typescript

From Dev

Cannot find module after using state syntax

From Dev

Cannot find module gulp.js when publishing to Azure

From Dev

Gulp and Babel: Error: Cannot find module

From Dev

Gulp-Error: Cannot find module 'gulp-watch'

From Dev

React Error: Cannot find module './common'

From Dev

Gulp-Error: Cannot find module 'gulp-watch'

From Dev

gulp cannot find module 'notify-send' on Laravel 5 Homestead

From Dev

Gulp require "Cannot Find Module" even though it exists

From Dev

Error: Cannot find module 'gulp-uglifyjs' with "gulp watch

From Dev

react-native : cannot find module 'npmlog'

From Dev

Gulp and Babel: Error: Cannot find module

From Dev

Typescript+Gulp+Protractor => Cannot find module config.js

From Dev

Cannot find module 'gulp-gitinfo'

Related Related

  1. 1

    Importing images in TypeScript React - "Cannot find module"

  2. 2

    Gulp Error: Cannot find module 'jshint/src/cli'

  3. 3

    Can't get Gulp to run: cannot find module 'gulp-util'

  4. 4

    Receiving 'Error: Cannot find module' using browserify, gulp, react.js

  5. 5

    Cannot find module 'karma' while using grunt

  6. 6

    Gulp with browserify: Cannot find module src/js/main.js

  7. 7

    Gulp with browserify: Cannot find start module

  8. 8

    browserify cannot find module 'react'

  9. 9

    Gulp require "Cannot Find Module" even though it exists

  10. 10

    Browserify: Cannot find module '...' from '...' (Coffeescript + React)

  11. 11

    Gulp - throw err cannot find module 'q'

  12. 12

    Gulp watch doesn´t work - Cannot find module livereload

  13. 13

    Error: Cannot find module 'gulp-uglifyjs' with "gulp watch

  14. 14

    Gulp Error: Cannot find module browserify

  15. 15

    Karma Not Running - cannot find module gulp

  16. 16

    Got "Cannot find module 'angular2/angular2'" using gulp-typescript

  17. 17

    Cannot find module after using state syntax

  18. 18

    Cannot find module gulp.js when publishing to Azure

  19. 19

    Gulp and Babel: Error: Cannot find module

  20. 20

    Gulp-Error: Cannot find module 'gulp-watch'

  21. 21

    React Error: Cannot find module './common'

  22. 22

    Gulp-Error: Cannot find module 'gulp-watch'

  23. 23

    gulp cannot find module 'notify-send' on Laravel 5 Homestead

  24. 24

    Gulp require "Cannot Find Module" even though it exists

  25. 25

    Error: Cannot find module 'gulp-uglifyjs' with "gulp watch

  26. 26

    react-native : cannot find module 'npmlog'

  27. 27

    Gulp and Babel: Error: Cannot find module

  28. 28

    Typescript+Gulp+Protractor => Cannot find module config.js

  29. 29

    Cannot find module 'gulp-gitinfo'

HotTag

Archive