构建 JavaScript 项目 Travis-CI 时出错

Error on build JavaScript project Travis-CI

我有一个项目,我正在使用 Gulp.js 在本地构建。但是当我将它推到 GitHub 时,Travis 开始构建它。我收到以下错误:

module.js:338
    throw err;
    ^
Error: Cannot find module 'gulp-concat'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/home/travis/build/thyagostall/freecell/gulpfile.js:4:11)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)

.travis.yml:

language: node_js
node_js:
  - "0.12"
before_script:
  - npm install -g gulp gulp-concat gulp-connect gulp-html-replace gulp-eslint
script: gulp

还有我的gulpfile.js:

var gulp = require('gulp'),
    connect = require('gulp-connect'),
    concat = require('gulp-concat'),
    htmlreplace = require('gulp-html-replace');

var distDir = './build/',
    sourceDir = './src/';

gulp.task('webserver', function() {
    connect.server({
        root: distDir,
        port: 2345,
        livereload: true
    });
    gulp.watch('src/**/*.*', ['build', 'images', 'styles', 'vendor']);
});

gulp.task('images', function() {
    gulp.src(sourceDir + '**/*.{jpg,png,gif}')
        .pipe(gulp.dest(distDir));
});

gulp.task('styles', function() {
    gulp.src(sourceDir + '**/*.css')
        .pipe(gulp.dest(distDir));
});

gulp.task('build', function() {
    gulp.src([sourceDir + '**/utility.js', sourceDir + '**/game_events.js', sourceDir + '**/memento.js', sourceDir + '**/*.js'])
        .pipe(concat('main.js'))
        .pipe(gulp.dest(distDir))
        .pipe(connect.reload());
});

gulp.task('vendor', function() {
    gulp.src(sourceDir + '**/*.html')
        .pipe(htmlreplace({
            'vendor': 'vendor.js',
            'js': 'main.js'
        }))
        .pipe(gulp.dest(distDir));
});

gulp.task('lint', function() {
    gulp.src(sourceDir + '**/*.js')
        .pipe(eslint())
        .pipe(eslint.format())
});

gulp.task('default', ['build', 'images', 'styles', 'vendor']);

我是否缺少任何明显的东西?

首先,将您的 .travis.yml 更改为:

language: node_js
node_js:
  - "0.12"
before_script:
  - npm install -g gulp
script: gulp

你只需要全局安装gulp,因为它提供了gulp命令行工具。根据 official npm documentation:

There are two ways to install npm packages: locally or globally. You choose which kind of installation to use based on how you want to use the package.

If you want to use it as a command line tool, something like the grunt CLI, then you want to install it globally. On the other hand, if you want to depend on the package from your own module using something like Node's require, then you want to install locally.

其次,我对你的 github 帐户(从你的 Whosebug 个人资料链接)做了一些调查,发现 project you're trying to build. Looking at the package.json 我看到 [=17] 中没有 gulp-concat =].这可能是由于键入:

npm install gulp-concat

而不是:

npm install gulp-concat --save-dev

这可以解释为什么您可以在本地构建项目,但在 Travis-CI 上却失败了。将 gulp-concat 添加到您的 devDependencies 应该可以解决问题。