title: npm code-coverage ELIFECYCLE error with phantomjs
body: <p>I want to run angular jasmine unit tests on a linux machine. </p>
  
  <p>The error I get is:</p>
  
  <pre><code>&gt; ng test --watch=false --code-coverage
  
  [32m26 02 2020 02:44:05.933:INFO [karma-server]: [39mKarma v4.1.0 server started at http://0.0.0.0:9876/
  [32m26 02 2020 02:44:05.938:INFO [launcher]: [39mLaunching browsers PhantomJS with concurrency unlimited
  [32m26 02 2020 02:44:05.970:INFO [launcher]: [39mStarting browser PhantomJS
  [32m26 02 2020 02:44:29.272:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: [39mConnected on socket Ew46sEHwaLSfgmQWAAAA with id 52382003
  PhantomJS 2.1.1 (Linux 0.0.0): Executed 0 of 0 SUCCESS (0 secs / 0 secs)
  [1A[2KPhantomJS 2.1.1 (Linux 0.0.0): Executed 0 of 0 ERROR (0.04 secs / 0 secs)
  npm ERR! code ELIFECYCLE
  npm ERR! errno 1
  npm ERR! my-app@0.0.0 code-coverage: `ng test --watch=false --code-coverage`
  npm ERR! Exit status 1
  npm ERR! 
  npm ERR! Failed at the my-app@0.0.0 code-coverage script.
  npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
  </code></pre>
  
  <p>The npm log file is:</p>
  
  <pre><code>info it worked if it ends with ok
  verbose cli [ '/localdisk/opt/nodejs/node-v10.16.0-linux-x64/bin/node',
  verbose cli   '/localdisk/opt/nodejs/current/bin/npm',
  verbose cli   'run',
  verbose cli   'code-coverage' ]
  info using npm@6.9.0
  info using node@v10.16.0
  verbose run-script [ 'precode-coverage', 'code-coverage', 'postcode-coverage' ]
  info lifecycle my-app@0.0.0~precode-coverage: my-app@0.0.0
  info lifecycle my-app@0.0.0~code-coverage: my-app@0.0.0
  verbose lifecycle my-app@0.0.0~code-coverage: unsafe-perm in lifecycle true
  verbose lifecycle my-app@0.0.0~code-coverage: PATH: /localdisk/opt/nodejs/node-v10.16.0-linux-x64/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/localdisk/home/bambooagent/agent1/xml-data/build-dir/my-project/selfportal-ui/node_modules/.bin:/usr/java/jdk1.7/bin:/localdisk/opt/nodejs/current/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin
  verbose lifecycle my-app@0.0.0~code-coverage: CWD: /localdisk/home/bambooagent/agent1/xml-data/build-dir/my-project/selfportal-ui
  silly lifecycle my-app@0.0.0~code-coverage: Args: [ '-c', 'ng test --watch=false --code-coverage' ]
  silly lifecycle my-app@0.0.0~code-coverage: Returned: code: 1  signal: null
  info lifecycle my-app@0.0.0~code-coverage: Failed to exec code-coverage script
  verbose stack Error: my-app@0.0.0 code-coverage: `ng test --watch=false --code-coverage`
  verbose stack Exit status 1
  verbose stack     at EventEmitter.&lt;anonymous&gt; (/localdisk/opt/nodejs/node-v10.16.0-linux-x64/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:301:16)
  verbose stack     at EventEmitter.emit (events.js:198:13)
  verbose stack     at ChildProcess.&lt;anonymous&gt; (/localdisk/opt/nodejs/node-v10.16.0-linux-x64/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
  verbose stack     at ChildProcess.emit (events.js:198:13)
  verbose stack     at maybeClose (internal/child_process.js:982:16)
  verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
  verbose pkgid my-app@0.0.0
  verbose cwd /localdisk/home/bambooagent/agent1/xml-data/build-dir/my-project/selfportal-ui
  verbose Linux 3.10.0-1062.1.1.el7.x86_64
  verbose argv "/localdisk/opt/nodejs/node-v10.16.0-linux-x64/bin/node" "/localdisk/opt/nodejs/current/bin/npm" "run" "code-coverage"
  verbose node v10.16.0
  verbose npm  v6.9.0
  error code ELIFECYCLE
  error errno 1
  error my-app@0.0.0 code-coverage: `ng test --watch=false --code-coverage`
  error Exit status 1
  error Failed at the my-app@0.0.0 code-coverage script.
  error This is probably not a problem with npm. There is likely additional logging output above.
  verbose exit [ 1, true ]
  </code></pre>
  
  <p>My karma.conf.js is:</p>
  
  <pre><code>process.env.CHROME_BIN = require('puppeteer').executablePath();
  
  module.exports = function(config) {
    config.set({
      basePath: '',
      frameworks: ['jasmine', '@angular-devkit/build-angular'],
      plugins: [
        require('karma-jasmine'),
        require('karma-chrome-launcher'),
        require('karma-phantomjs-launcher'),
        require('karma-jasmine-html-reporter'),
        require('karma-coverage-istanbul-reporter'),
        require('@angular-devkit/build-angular/plugins/karma'),
      ],
      client: {
        jasmine: {
          random: true,
        },
        clearContext: false, // leave Jasmine Spec Runner output visible in browser
      },
      coverageIstanbulReporter: {
        dir: require('path').join(__dirname, 'coverage'),
        reports: ['html', 'lcovonly'],
        fixWebpackSourcePaths: true,
      },
  
      reporters: ['progress', 'kjhtml'],
      port: 9876,
      colors: true,
      logLevel: config.LOG_INFO,
      autoWatch: true,
      customLaunchers: {
        ChromeHeadless: {
         base: 'chrome',
         flags: ['--headless', '--disable-gpu', '--no-sandbox'],
       },
      },
      browsers: ['PhantomJS'],
      singleRun: true,
    });
  }; 
  </code></pre>
  
  <p>tsconfig.spec.json:</p>
  
  <pre><code>{
    "extends": "../tsconfig.json",
    "compilerOptions": {
      "outDir": "../out-tsc/spec",
      "baseUrl": "./",
      "module": "commonjs",
      "types": [
        "jasmine",
        "node"
      ],
      "target": "ES5"
    },
    "files": [
      "test.ts",
      "polyfills.ts"
    ],
    "include": [
      "**/*.spec.ts",
      "**/*.d.ts"
    ]
  }
  </code></pre>
  
  <p>tsconfig.app.json:</p>
  
  <pre><code>{
    "extends": "../tsconfig.json",
    "compilerOptions": {
      "outDir": "../out-tsc/app",
      "baseUrl": "./",
      "module": "es2015",
      "types": ["node"]
    },
    "exclude": [
      "test.ts",
      "**/*.spec.ts"
    ]
  }
  </code></pre>
  
  <p>tsconfig.json:</p>
  
  <pre><code>{
    "compileOnSave": false,
    "preserveWhitespaces": false,
    "compilerOptions": {
      "outDir": "./dist/out-tsc",
      "sourceMap": true,
      "declaration": false,
      "moduleResolution": "node",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "target": "es5",
      "downlevelIteration": true,
      "typeRoots": [
        "node_modules/@types"
      ],
      "lib": [
        "es2017",
        "dom"
      ]
    }
  }
  </code></pre>
  
  <p>What could be going wrong?</p>