Mocha: "sh: 1: mocha: Permission denied" - Running mocha test via "npm test" fails

Created on 4 Jan 2015  路  5Comments  路  Source: mochajs/mocha

I am attempting to use Mocha for testing in a project. I've followed the steps in this tutorial: https://www.joyent.com/blog/risingstack-writing-testable-apis-the-basics

My package.json looks like this:

{
  "name": "cyoab",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "test": "mocha test"
  },
  "dependencies": {
    "express": "~4.2.0",
    "static-favicon": "~1.0.0",
    "morgan": "~1.0.0",
    "mongoose": "*",
    "cookie-parser": "~1.0.1",
    "body-parser": "~1.0.0",
    "debug": "~0.7.4"
  },
  "devDependencies": {
    "mocha": "~2.1.0",
    "chai": "~1.10.0",
    "sinon": "~1.12.2"
  }
}

When I run "npm test" or "sudo npm test" I get the following output:

matt@matt-Inspiron-N5040:/media/matt/Storage1/Dev/cyoab$ sudo npm test

> [email protected] test /media/matt/Storage1/Dev/cyoab
> mocha test

sh: 1: mocha: Permission denied
npm ERR! weird error 126
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian

npm ERR! not ok code 0

I have tried making mocha executable via chmod, but that didn't work.

I am using node v0.10.25 on Ubuntu 14.04 LTS.

Any ideas? Or am I posting this in the wrong place? I have a feeling it isn't necessarily a Mocha issue, but I'm not sure.

Thank you,

Matt

Most helpful comment

hi,

using ubuntu i never had to use sudo to setup mocha.
My steps are similar to

npm i -g mocha
mocha tests/*

First one create a new bin file in ~/bin/, it is executable, and ready to go.

This said, i also manually installed node, download archive, extract on desktop, link node-xxx/node/bin/{node,npm} to ~/bin, and it works fine.

Getting your problems, i d be cleaning up my system of node, then doing a cleaner setup manually.

My 2 cents!

All 5 comments

Here is the verbose output:

matt@matt-Inspiron-N5040:/media/matt/Storage1/Dev/cyoab$ sudo npm --loglevel verbose test
npm info it worked if it ends with ok
npm verb cli [ '/usr/bin/nodejs',
npm verb cli   '/usr/bin/npm',
npm verb cli   '--loglevel',
npm verb cli   'verbose',
npm verb cli   'test' ]
npm info using [email protected]
npm info using [email protected]
npm verb run-script [ 'pretest', 'test', 'posttest' ]
npm info pretest [email protected]
npm info test [email protected]
npm verb unsafe-perm in lifecycle true

> [email protected] test /media/matt/Storage1/Dev/cyoab
> mocha test

sh: 1: mocha: Permission denied
npm info [email protected] Failed to exec test script
npm ERR! weird error 126
npm verb exit [ 1, true ]
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian

npm ERR! not ok code 0

hi,

using ubuntu i never had to use sudo to setup mocha.
My steps are similar to

npm i -g mocha
mocha tests/*

First one create a new bin file in ~/bin/, it is executable, and ready to go.

This said, i also manually installed node, download archive, extract on desktop, link node-xxx/node/bin/{node,npm} to ~/bin, and it works fine.

Getting your problems, i d be cleaning up my system of node, then doing a cleaner setup manually.

My 2 cents!

Definitely not an issue related to mocha. Did you perhaps install any project-local dependencies through sudo? Give running sudo chown -R $(whoami) . from your project dir a try, and remember for the future never to install local npm dependencies as root.

@jbnicolai OK, I ran sudo chown -R $(whoami) . No luck. Then I ran sudo npm cache clean then sudo rm -rf node_modules then npm install. Still having the error.

@maboiteaspam I tried your steps and that fixed my problem. Thanks, guys. Sorry I raised this issue in the wrong place!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

luoxi001713 picture luoxi001713  路  3Comments

jamietre picture jamietre  路  3Comments

adamhooper picture adamhooper  路  3Comments

EdvinOlofsson picture EdvinOlofsson  路  3Comments

eschwartz picture eschwartz  路  3Comments