4

I have the following NPM script in my package.json:

{
  "scripts": {
     "lint": "tslint -c tslint.json src/**/**?(.test).ts?(x)"
  }
}

And after I run npm run lint I get the following error:

> tslint -c tslint.json src/**/**?(.test).ts?(x)

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `tslint -c tslint.json src/**/**?(.test).ts?(x)'

It seems that I cannot use the character ( in my NPM script. How can I come around this? The script is a valid bash script.

I tried searching the issue but I couldn't find anything helpful.

Any help is appreciated.

Kind thanks!

Update:

It seems that running this command in my terminal (macOS) like so:

bash -c "tslint -c tslint.json src/**/**?(.test).ts?(x)"

I get the exact same error.

however if I run it like this:

bash -c "tslint -c tslint.json src/**/**?\(.test\).ts?\(x\)"

it seems to work in the terminal. But not in a NPM script.

Got it to work like so:

{
   "scripts": {
      "lint": "bash -c \"tslint -c tslint.json 'src/**/**?(.test).ts?(x)'\"",
   }
}

or the more simpler version

{
   "scripts": {
      "lint": "tslint -c tslint.json 'src/**/**?(.test).ts?(x)'",
    }
}

Credits go to @shellter.

6
  • Maybe try to escape it like so: tslint -c tslint.json src/**/**?\(.test\).ts?\(x\) Commented Dec 18, 2017 at 15:01
  • tried it... still getting the error. I also tried with bash -c "tslint -c tslint.json src/**/**?(.test).ts?(x)" with and without escaping. Commented Dec 18, 2017 at 15:03
  • 3
    how about bash -c "tslint -c tslint.json 'src/**/**?(.test).ts?(x)' " ? (note the single-quote surrounding the problematic pattern (you may want/need to remove the space between ' and " that I included for visibility) ). Good luck. Commented Dec 18, 2017 at 15:21
  • yes, thanks @shellter, that was it!. I posted an update with the final fix. Commented Dec 19, 2017 at 6:10
  • I couldnt get my bash scripts to work with npm, using the bash -c prefix helped. Is it because npm run is using node runtime? Commented Jun 17, 2018 at 18:41

1 Answer 1

2

I once wrote this script to make a basic syntax check on my javascript files:

#!/usr/bin/env bash
if [ -x "$(command -v parallel)" ]; then
    find src/js -name "*.js" | parallel --no-notice --jobs 4 --gnu nodejs -c
else
    find src/js -name "*.js" | xargs -L1 -d '\n' nodejs -c
fi

This will lint every javascript file within src/js and utilize parallel if available. If you have a cpu with more threads, you might want to increase the jobs parameter to match your threads-count.

I hope this is of some help to you

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.