1
votes

In my project I have a gulpfile.js which is supposed to copy some files from one directory to another one. Running this locally from Task Runner Explorer in Visual studio works.

When I want to execute this gulp script in Azure DevOps, it complains about a missing package.

My gulpfile.js looks like this:

/// <binding AfterBuild='moduleCopy, copyModuleDependencies' />

var gulp = require('gulp');
var newer = require('gulp-newer');
var rename = require('gulp-rename');


gulp.task('copyModuleDependencies', function (cd) {
    gulp.src('../*Module/bin/Release/netcoreapp3.0/*.dll')
        .pipe(rename({ dirname: '' }))
        .pipe(gulp.dest("bin/Release/netcoreapp3.0/Modules"))
});

gulp.task('moduleCopy', function (cb) {
    gulp.src('../*Module/bin/Release/netcoreapp3.0/*.Module.dll')
        .pipe(newer("bin/Release/netcoreapp3.0/Modules/"))
        .pipe(rename({ dirname: '' }))
        .pipe(gulp.dest("bin/Release/netcoreapp3.0/Modules/"))
});

My package.json file looks like this:

{

  "name": "MyHost",
  "version": "1.0.0",
  "description": "",
  "main": "gulpfile.js",
  "dependencies": {
    "gulp": "^4.0.2",
    "gulp-newer": "^1.4.0",
    "gulp-rename": "^1.4.0",
    "upath": "^1.2.0"
  },
  "devDependencies": {}
}

And my azure-pipelines.yml file:

trigger: - feature/obfuscated

pool: vmImage: 'ubuntu-latest'

variables:
  buildConfiguration: 'Release'

steps:
- script: dotnet build --configuration $(buildConfiguration)
  displayName: 'dotnet build $(buildConfiguration)'
- task: Npm@1
  inputs:
    command: 'install'
    workingDir: 'obfuscated.Host'
- task: Gulp@1
  inputs:
    gulpFile: 'obfuscated.Host/gulpfile.js'
    targets: 'moduleCopy copyModuleDependencies'
    enableCodeCoverage: false

When triggering the build, it seems that npm is able to install the packages specified in the package.json file, but when it's supposed to execute the gulpfile.js this happens:

internal/modules/cjs/loader.js:638 throw err; ^

Error: Cannot find module 'upath'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/vsts/work/1/s/obfuscated.Host/node_modules/chokidar/index.js:13:13)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
##[error]Gulp failed with error: /usr/local/bin/gulp failed with return code: 1
##[section]Finishing: Gulp

Obviously it seems to be missing some packages from packages.json, but why does this work locally?

1

1 Answers

2
votes

I believe this is because you aren't running the gulp task in your pipeline YAML file from your project directory, but rather your solution directory. Because you specify your "gulpfile.js" in the proper path, it is able to find it and discover your tasks, but it then executes gulp from your current working directory, which you have not set, so it defaults to the solution directory.

Try removing the gulpFile input from your gulp task (since it defaults to "gulpfile.js") and add the workingDirectory input, set to your project directory of "obfuscated.Host", as such:

variables:
  buildConfiguration: 'Release'

steps:
- script: dotnet build --configuration $(buildConfiguration)
  displayName: 'dotnet build $(buildConfiguration)'
- task: Npm@1
  inputs:
    command: 'install'
    workingDir: 'obfuscated.Host'
- task: Gulp@1
  inputs:
    workingDirectory: 'obfuscated.Host'
    targets: 'moduleCopy copyModuleDependencies'
    enableCodeCoverage: false