Unpredictable dependency behaviour in Node JS while using the config npm module

310 views Asked by At

I am new to node js and npm and trying to externalise properties using the node module https://github.com/lorenwest/node-config.

I am seeing some unpredictable behaviour with dependencies. When I run npm install and gulp bundle, sometimes I get this issue :

Error: Cannot find module 'coffee-script' from '/Users/jonny/Documents/temp/myapp/node_modules/config/lib'
    at /Users/jonny/Documents/temp/myapp/node_modules/browserify/node_modules/resolve/lib/async.js:46:17
    at process (/Users/jonny/Documents/temp/myapp/node_modules/browserify/node_modules/resolve/lib/async.js:173:43)
    at ondir (/Users/jonny/Documents/temp/myapp/node_modules/browserify/node_modules/resolve/lib/async.js:188:17)
    at load (/Users/jonny/Documents/temp/myapp/node_modules/browserify/node_modules/resolve/lib/async.js:69:43)
    at onex (/Users/jonny/Documents/temp/myapp/node_modules/browserify/node_modules/resolve/lib/async.js:92:31)
    at /Users/jonny/Documents/temp/myapp/node_modules/browserify/node_modules/resolve/lib/async.js:22:47

What I mean by sometimes it works is, error was there day before yesterday and not yesterday and it is appearing again today.

Coffee-script is actually installed as part of the dependencies during the npm install:

[email protected] node_modules/grunt
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected]
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected]
├── **[email protected]**

Here is my gulp file:

var browserify = require('browserify')
var gulp = require('gulp')
var gutil = require('gulp-util')
var jshint = require('gulp-jshint')
var nodemon = require('gulp-nodemon')
var plumber = require('gulp-plumber')
var react = require('gulp-react')
var sass = require('gulp-sass');
var source = require('vinyl-source-stream')
var streamify = require('gulp-streamify')
var uglify = require('gulp-uglify')
var mocha = require('gulp-mocha');
var del = require('del');

var jsSrcPaths = './src/**/*.js*'
var jsLibPaths = './lib/**/*.js'
//TODO: Not sure if we need to delete all occurance of @insin as it is the name of the repo creator on https://github.com/insin/isomorphic-lab
var bundledDeps = [
  'events',
  'react',
  '@insin/react-router',
  'superagent-ls',
  'newforms',
  'run-parallel'
]

process.env.NODE_ENV = gutil.env.production ? 'production' : 'development'
process.env.HOST = '127.0.0.1'
process.env.PORT = '3000'

gulp.task('mocha-test', function() {
  require ('babel/register');
  require('./test/setup');
    return gulp.src(['test/components/*Test.js'], { read: false })
        .pipe(mocha({
            reporter: 'spec'
        }));

});

gulp.task('mocha-watch', function() {
  gulp.watch(['src/**', 'test/**'], ['mocha-test']);
});

gulp.task('transpile-js', function() {
  return gulp.src(jsSrcPaths)
    .pipe(plumber())
    .pipe(react({harmony: true}))
    .pipe(gulp.dest('./lib'))
})

gulp.task('lint-js', ['transpile-js'], function() {
  return gulp.src(jsLibPaths)
    .pipe(jshint('./.jshintrc'))
    .pipe(jshint.reporter('jshint-stylish'))
})

gulp.task('bundle-js', ['lint-js'], function() {
  var b = browserify('./lib/client.js', {
    debug: !!gutil.env.debug,
    detectGlobals: false
  })
  bundledDeps.forEach(function(dep) { b.external(dep) })
  b.transform('envify')

  var stream = b.bundle()
    .pipe(source('app.js'))

  if (gutil.env.production) {
    stream = stream.pipe(streamify(uglify()))
  }

  return stream.pipe(gulp.dest('./static/myapp/js'))
})

gulp.task('bundle-deps', function() {
  var b = browserify({
    debug: !!gutil.env.debug,
    detectGlobals: false
  })
  bundledDeps.forEach(function(dep) { b.require(dep) })
  b.transform('envify')

  var stream = b.bundle()
    .pipe(source('deps.js'))

  if (gutil.env.production) {
    stream = stream.pipe(streamify(uglify()))
  }

  return stream.pipe(gulp.dest('./static/myapp/js'))
})

gulp.task('bundle', ['clean','sass','copy-css', 'copy-img', 'copy-js', 'bundle-deps', 'bundle-js'])

gulp.task('server', function(cb) {
  nodemon({
    script: './lib/server.js'
  , ignore: ['./src/*', './static/*']
  , ext: 'jade js'
  , delay: 5
  })
  cb()
})

gulp.task('watch', function() {
  gulp.watch(jsSrcPaths, ['bundle-js'])
})

gulp.task('default', ['bundle-js', 'watch'])

gulp.task('copy-css', function() {
  gulp.src('./node_modules/someapp-elements/someapp/public/stylesheets/**/*')
      .pipe(gulp.dest('./static/someapp/css'));
});

gulp.task('copy-img', function() {
    gulp.src('./node_modules/someapp-elements/someapp/public/images/**/*')
        .pipe(gulp.dest('./static/someapp/img'));
});

gulp.task('copy-js', function() {
    gulp.src('./node_modules/someapp-elements/someapp/public/javascripts/**/*')
        .pipe(gulp.dest('./static/someapp/js'));
});

gulp.task('sass', function () {
  gulp.src('./src/sass/*.scss')
      .pipe(sass({errLogToConsole: true}))
      .pipe(gulp.dest('./static/myapp/css'));
});

gulp.task('clean:someapp', function(cb) {
  del(['static/someapp/css/**/*','static/someapp/img/**/*','!.gitignore'], cb);
});


gulp.task('clean:lib', function(cb) {
  del(['lib/**/*'], cb); //TODO: deleting the entire directory is not working so deleting the content
});

gulp.task('clean:css', ['clean:someapp'])

gulp.task('clean', ['clean:lib','clean:css' ])

If I try to include the latest version of coffee-script again, I get "Cannot find module ..." for these chain of dependencies:

  , "iced-coffee-script":"1.8.0-a"
  , "js-yaml":"^3.3.1"
  , "hjson":"1.7.3"
  , "yaml":"0.2.3"
  , "toml":"2.2.2"
  , "cson":"3.0.1"
  , "properties":"1.2.1"

I have tried deleting the node_modules folder and trying npm install again but the issue remains.

0

There are 0 answers