14

I'd like to get my package.json to be able to run using the command npm run test-watch on Windows 10 with npm 5.5.1. In my package.json:

  "scripts": {
    "test": "mocha server/**/*.test.js",
    "test-watch": "nodemon --exec 'npm test'"
  }

However, I this interpretes the code strangely to have a single quote in there. I'm actually following a Udemy course so it appears to work for the instructor. However, here is the output I get:

PS D:\courses\node-course\node-todo-api> npm run test-watch

> todo-api@1.0.0 test-watch D:\courses\node-course\node-todo-api
> nodemon --exec 'npm test'

[nodemon] 1.14.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `'npm test'`
''npm' is not recognized as an internal or external command,
operable program or batch file.
[nodemon] app crashed - waiting for file changes before starting...

What do I need to change to get this to work? It appears to be keeping the quotes on the string. I can't seem to get around it though. When I run the command directly, it works:

PS D:\courses\node-course\node-todo-api> nodemon --exec 'npm test'
[nodemon] 1.12.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `npm test`

> todo-api@1.0.0 test D:\courses\node-course\node-todo-api
> mocha server/**/*.test.js



started on port 3000
  Post /todos
    √ should create a new todo (50ms)


  1 passing (1s)
Rik
  • 1,870
  • 3
  • 22
  • 35

1 Answers1

19

Unfortunately, the operating system and shell can cause a massive headache when using npm. Some things work on one computer and some on another.

Both of these should work on Windows 10 though:

"test-watch": "nodemon --exec \"npm test\""
"test-watch": "nodemon --exec npm test"
Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
  • Thanks... Can't believe I couldn't figure that one out LOL now that it is so easy... – Rik Jan 05 '18 at 01:21
  • Is there a way to get the command to work regardless of what system and shell are being used? – cjsimon Feb 28 '18 at 07:24
  • 2
    @cjsimon I think not using single quotes is the way to go to get it working on both *nix and Windows. Use "". According to [this](https://www.npmjs.com/package/cross-env) page using three backslashes might be the best option (for some reason). – Mika Sundland Feb 28 '18 at 11:49