gulp抛出一个错误:TypeError(' Path必须是一个字符串。收到' + inspect(path))

时间:2016-06-09 11:06:35

标签: node.js gulp

我使用的是节点v6.2.0。我有一个简单的gulp文件,可以编译ts文件并复制一些库。

但是当我尝试复制libs时,我不断收到上述错误。

gulpfile.js:

const gulp = require("gulp");
const del = require("del");
const tsc = require("gulp-typescript");
const sourcemaps = require('gulp-sourcemaps');
const tsProject = tsc.createProject("tsconfig.json");
const tslint = require('gulp-tslint');
const config = require('./gulp.config.js');

const $ = require('gulp-load-plugins')({
    lazy: true
});

// clean the contents of the distribution directory
gulp.task('clean', function() {
    return del(config.dest, { force: true });
});

gulp.task('resources', function() {
    _log('copy - ' + config.assets, $.util.colors.yellow);
    return gulp.src(config.assets, { base: root })
        .pipe(gulp.dest(config.dest));
});

/**
 * Copy all required libraries into build directory.
 */
gulp.task("libs", () => {
    _log('copying libs to:' + config.dest + 'lib' ,$.util.colors.yellow);
    return gulp.src([
            'es6-shim/es6-shim.min.js',
            'systemjs/dist/system-polyfills.js',
            'systemjs/dist/system.src.js',
            'reflect-metadata/Reflect.js',
            'rxjs/**',
            'zone.js/dist/**',
            '@angular/**'
        ], { cwd: 'node_modules/**' })
        .pipe(gulp.dest(config.dest + 'lib'));
});

/**
 * Compile TypeScript sources and create sourcemaps in build directory.
 */
gulp.task("compile", () => {
    var tsResult = gulp.src(config.root + "**/*.ts")
        .pipe(sourcemaps.init())
        .pipe(tsc(tsProject));
    return tsResult.js
        .pipe(sourcemaps.write("."))
        .pipe(gulp.dest(config.dest));
});

gulp.task("default", ['compile', 'resources', 'libs'], () => {
    _log('Building the project ...', $.util.colors.yellow);

});

function _log(msg, color) {
    color = color || $.util.colors.blue;
    $.util.log(color(msg));
}

function _logError(error) {
    _log('**** START OF ERROR ***', $.util.colors.red);
    _log(error, $.util.colors.red);
    _log('**** END OF ERROR ***', $.util.colors.red);

    return error;
}

gulp.config.js

module.exports = (function() {
    var root = './';
    var config = {
        root: root,
        dest: '../dist/dashboard/',
        assets: [
            root + 'assets/**/*.*',
        ]

    };

    return config;
})();

错误堆栈:

  

[14:03:08]使用gulpfile   C:\工作空间\主\网络服务器\公共\角客户\仪表板\ gulpfile.js   [14:03:08]开始'编译' ... [14:03:08]开始'资源' ...   [14:03:08]复制 - ./assets / ** / [14:03:08]开始' libs' ...   [14:03:08]将libs复制到:../ dist / dashboard / lib(node:11076)   弃用警告:' root'已被弃用,请使用全球' path.js:7       抛出新的TypeError(' Path必须是一个字符串。收到' + inspect(path));       ^

     

TypeError:Path必须是字符串。收到{   DTRACE_NET_SERVER_CONNECTION:[功能],DTRACE_NET_STREAM_END:   [功能],DTRACE_HTTP_SERVER_REQUEST:[功能],
  DTRACE_HTTP_SERVER_RESPONSE:[功能],DTRACE_HTTP_CLIENT_REQUEST:   [功能],DTRACE_HTTP_CLIENT_RESPONSE:[功能],
  COUNTER_NET_SERVER_CONNECTION:[功能],
  COUNTER_NET_SERVER_CONNECTION_CLOSE:[功能],
  COUNTER_HTTP_SERVER_REQUEST:[功能],
  COUNTER_HTTP_SERVER_RESPONSE:[功能],
  COUNTER_HTTP_CLIENT_REQUEST:[功能],
  COUNTER_HTTP_CLIENT_RESPONSE:[功能],全球:[通告],
  进程:进程{        标题:' gulp',        版本:' v6.2.0',   ....

1 个答案:

答案 0 :(得分:2)

你的问题就在这一行:

return gulp.src(config.assets, { base: root })

您的gulpfile中未定义变量root。 Node.js将此解释为对(已弃用的)undocumented alias for global

的引用

你可能想要:

return gulp.src(config.assets, { base: config.root })