使用config npm模块时,Node JS中的不可预测的依赖行为

时间:2015-06-09 09:43:59

标签: javascript node.js coffeescript npm

我是node jsnpm的新手,并尝试使用节点模块https://github.com/lorenwest/node-config来外部化属性。

我看到一些不可预测的依赖行为。当我运行npm installgulp bundle时,有时我会遇到此问题:

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

我的意思是,有时它的作用是,错误是前天而不是昨天,它今天又出现了。

Coffee-script实际上是在npm install:

期间作为依赖项的一部分安装的
grunt@0.4.5 node_modules/grunt
├── which@1.0.9
├── dateformat@1.0.2-1.2.3
├── eventemitter2@0.4.14
├── getobject@0.1.0
├── rimraf@2.2.8
├── colors@0.6.2
├── async@0.1.22
├── grunt-legacy-util@0.2.0
├── hooker@0.2.3
├── nopt@1.0.10 (abbrev@1.0.7)
├── exit@0.1.2
├── minimatch@0.2.14 (sigmund@1.0.1, lru-cache@2.6.4)
├── glob@3.1.21 (inherits@1.0.0, graceful-fs@1.2.3)
├── lodash@0.9.2
├── **coffee-script@1.3.3**

这是我的gulp文件:

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' ])

如果我再次尝试包含最新版本的咖啡脚本,我会为这些依赖链获得“Cannot find module ...”:

  , "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"

我尝试删除node_modules文件夹并再次尝试npm install,但问题仍然存在。

0 个答案:

没有答案