How to run mocha using sudo on Travis - node.js

I'm trying to create raw sockets in node, but I'm having trouble running mocha with sudo.
Calling mocha in either package.json or .travis.yml works (with the permissions error for the socket)
Trying to call sudo mocha in either file gets me a file not found error.
$ sudo mocha
sudo: mocha: command not found
The command "sudo mocha" exited with 1.
I do have sudo: true

Try adding the mocha call to your package.json scripts like this:
"scripts": {
"start": // do something
"test": "mocha"
}
Then set it up on travis.yml with:
script: "sudo $(which npm) test"
More help here: https://github.com/travis-ci/travis-ci/issues/1305

Related

Concatenating command line arguments and flags in NPM package.json scripts

I am looking for a way to do something like this:
package.json
[...]
"scripts": {
"test": "cd ./apps/my-awesome-app && npx cypress run",
"test:watch": "npm run test && --headed",
[...]
so then at the command line, running
npm run test:watch
will result in the following commands being executed:
cd ./apps/my-awesome-app
npx cypress run --headed
However, this is not working as expected. Is there a way to achieve this without repeating the whole "test"-string?

Start node.js with bash script?

What to do when I have to start a node.js application with a script on the raspberry pi?
Normally I go to the command line move to the map with "cd projectMap" and do this command "npm projectName"
I thought this would work but it does not.
#!/bin/bash
cd projectMap
npm projectName
to run a nodejs script you generally run
node index.js
Replace index.js with the name of the file you want to run.
To run it with npm you have to add it as a script in the package.json like so
"scripts": {
"start": "node index.js"
}
And to run it:
npm start
Create a file:
sudo touch script.sh
Give permission:
sudo chmod 400 script.sh
Inside your script.sh
#!/bin/bash
cd project
npm -i
node -v // not necessary just to check the node version .
node file.js
run script.sh
./script.sh

NPM script under cygwin/windows: the syntax of the command is incorrect

I am running Node 6.9.5 and NPM 3.10.10 on a Windows 7 machine. My terminal is Cygwin 2.877.
If I try to run the following in Cygwin, it works fine:
mkdir mydir/mysubdir;
However, if I put it into a package.json file instead, e.g.:
"scripts": {
"test": "mkdir mydir/mysubdir"
},
and run:
npm run test
It fails with:
The syntax of the command is incorrect.
After Googling the above, it seems to be a Windows Command Prompt error, not a Cygwin one. As such, it seems that NPM is trying to run the script using the Command Prompt rather than the existing Cygwin environment.
How can I fix this? Or rather, how can I make sure NPM runs scripts in the terminal environment it is being invoked from?
The script are always run in the default windows shell, not cygwin.
If you want it to run in bash then put this in package.json:
"scripts": {
"test": "bash test.sh"
},
and put this in test.sh:
#!/bin/bash
mkdir mydir/mysubdir
Or, as csvan pointed out in the comment, you can use Node scripts instead of shell scripts:
"scripts": {
"test": "node test.js"
},
This approach is even better for cross-platform compatibility.
See also:
Why does `DEBUG=foo node index.js` fails in `scripts` section of `package.json`

npm start to run some scripts in silent mode

I have multiple scripts in package.json, most of them call some .js e.g. "copyResources" bellow. If I will run "npm run -s copyResources" in shell, it will execute silently. So far so good. But I would like to run "npm start" which will execute copyResource silently, example bellow, but this is not working (it's not silent). :(
Later I will have more scripts in "start" and I want to run some of them silently and some of them not. Thats the case why I can't just do npm start -s ...
"scripts": {
"copyResources": "echo 'Copy resources =>' && node ./bin/copyResources.js",
"start": "npm run -s copyResources"
}
Thank you very much!

mocha running with NPM test but not regular mocha CLI command

I am trying to understand what I am doing wrong in this instance. I have a Node.js project with the following in my package.json
"scripts": {
"test": "mocha --recursive ./src/setup/*.js ./test/**/*.js"
},
"dependencies": {
"mocha": "^2.2.5"
}
When I run 'npm test' the mocha tests are run correctly:
$ npm test (successful run)
However when I try to just run the mocha command I have there in my package.json
$ mocha --recursive ./src/setup/*.js ./test/**/*.js"
This errors with:
-sh: mocha: command not found
I do not have mocha globally installed, I only have installed it via npm to this specific project.
If I install mocha globally then it works. Why doesn't it work when I have simply have mocha installed in the current directory's node_modules, yet it does with 'npm test'?
npm scripts automatically add mocha to the PATH:
If you depend on modules that define executable scripts, like test suites, then those executables will be added to the PATH for executing the scripts.
https://docs.npmjs.com/misc/scripts#path

Resources