Pipeline error: "sh: 1: ng: Permission denied" (Angular Project)
Asked by Ruben Ferreira:

I have built a pipeline using Bitbucket. The pipeline worked fine without running tests, but the moment I put the tests back into the yml file I get this error from BB:

```
+ npm run test
> deezer-plus@0.0.0 test /opt/atlassian/pipelines/agent/build
> ng test --watch=false --browsers=ChromeHeadless

sh: 1: ng: Permission denied
npm ERR! code ELIFECYCLE
npm ERR! errno 126
npm ERR! deezer-plus@0.0.0 test: `ng test --watch=false --browsers=ChromeHeadless`
npm ERR! Exit status 126
npm ERR!
npm ERR! Failed at the deezer-plus@0.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-08-18T11_59_56_793Z-debug.log
```

Below is my entire yml file:

```
**bitbucket-pipelines.yml**

image: node:14

pipelines:
pull-requests:
'**':
- step:
name: "Install and test"
deployment: production
caches:
- node
script:
- npm -g config set user root
- npm install
- apt update
- npm run install-test-dependancies
- npm run test
branches:
master:
- step:
deployment: development
name: "Build"
caches:
- node
script:
- npm run build
artifacts:
- dist/**

- step:
name: "Deploy to Firebase"
script:
- pipe: atlassian/firebase-deploy:0.2.1
variables:
FIREBASE_TOKEN: $FIREBASE_TOKEN
PROJECT_ID: $FIREBASE_PROJECT

```

The scripts in my \*\*package.json\*\* file look like this:

````

"scripts": {
"ng": "ng",
"start": "ng serve",
"watch": "ng build --watch --configuration development",
"lint": "ng lint",
"e2e": "ng e2e",
"test": "ng test --watch=false --browsers=ChromeHeadless",
"install-test-dependancies": "apt install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget",
"test-with-head": "ng test",
"build": "ng build",
"deploy": "firebase deploy --token $FIREBASE_TOKEN --non-interactive"
},

This is an extract of my **karma.conf.js** file:
```
browsers: ['Chrome', 'ChromeHeadless', 'ChromeHeadlessNoSandbox'],
customLaunchers: {
ChromeHeadless: {
base: 'Chrome',
flags: [
'--headless',
'--disable-gpu',
// Without a remote debugging port, Google Chrome exits immediately.
'--remote-debugging-port=9222',
'--no-sandbox'
],
debug: true,
},
ChromeHeadlessNoSandbox: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
},
````

```
And finally for completeness sake my **/root/.npm/_logs/**

0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'test' ]
2 info using npm@6.14.14
3 info using node@v14.17.5
4 verbose run-script [ 'pretest', 'test', 'posttest' ]
5 info lifecycle deezer-plus@0.0.0~pretest: deezer-plus@0.0.0
6 info lifecycle deezer-plus@0.0.0~test: deezer-plus@0.0.0
7 verbose lifecycle deezer-plus@0.0.0~test: unsafe-perm in lifecycle true
8 verbose lifecycle deezer-plus@0.0.0~test: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/opt/atlassian/pipelines/agent/build/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
9 verbose lifecycle deezer-plus@0.0.0~test: CWD: /opt/atlassian/pipelines/agent/build
10 silly lifecycle deezer-plus@0.0.0~test: Args: [ '-c', 'ng test --watch=false --browsers=ChromeHeadless' ]
11 silly lifecycle deezer-plus@0.0.0~test: Returned: code: 126 signal: null
12 info lifecycle deezer-plus@0.0.0~test: Failed to exec test script
13 verbose stack Error: deezer-plus@0.0.0 test: `ng test --watch=false --browsers=ChromeHeadless`
13 verbose stack Exit status 126
13 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack at EventEmitter.emit (events.js:400:28)
13 verbose stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:400:28)
13 verbose stack at maybeClose (internal/child_process.js:1055:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
14 verbose pkgid deezer-plus@0.0.0
15 verbose cwd /opt/atlassian/pipelines/agent/build
16 verbose Linux 5.4.92-flatcar
17 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "test"
18 verbose node v14.17.5
19 verbose npm v6.14.14
20 error code ELIFECYCLE
21 error errno 126
22 error deezer-plus@0.0.0 test: `ng test --watch=false --browsers=ChromeHeadless`
22 error Exit status 126
23 error Failed at the deezer-plus@0.0.0 test script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 126, true ]

```

**Any ideas why I am getting this error and possible fixes - any help would be appreciated, Thank you**


Answers:
author: Mark C
body: Hi [@Ruben Ferreira](/t5/user/viewprofilepage/user-id/4520903)

Welcome to the community.

Based on the error message, it seems the build is erroring with a permission denied issue.

```
sh: 1: ng: Permission denied
```

For this, would it be possible for you to add the **chown** command below before your test command to make sure you have permission to the **/build** folder?

```
chown -R root:root $BITBUCKET_CLONE_DIR
```

Apart from that, I'd also recommend [testing your Pipelines build locally in a Docker container](https://support.atlassian.com/bitbucket-cloud/docs/debug-pipelines-locally-with-docker/).  
Ideally, if it works locally in a Docker container, it should also work on Pipelines.

Let me know how it goes.

Regards,  
Mark C


Tags: bitbucket-pipelines

angular

bitbucket-cloud

cloud