Home>

I created the following npm script, but it doesn't work as expected.

"scripts": {
      "hoge": "concurrently \" nodemon --watch postcss.config.js --exec \ "postcss src/style/base.css -o public/style/base.css -w \" \ ""
}

Execution result ↓

PS C: \ Users \ akihiro \ work \ project \ diary>yarn hoge
yarn run v1.22.5
$concurrently "nodemon --watch postcss.config.js --exec" postcss src/style/base.css -o public/style/base.css -w ""
[1]'src' � ́A ����� R �} ���h � ͂��͊O ���R �} ���h �A
[1] ���� \ �ȃv���O �����͂��̓o �b�`�t�@�C���Ƃ��ĔF������Ă�� B
[1] src/style/base.css exited with code 1
[0] [nodemon] 2.0.6
[0] [nodemon] to restart at any time, enter `rs`
[0] [nodemon] watching path (s): postcss.config.js
[0] [nodemon] watching extensions: js, mjs, json
[0] [nodemon] starting `postcss`

This npm script uses double quotes in double quotes in double quotes. And maybe the double quote escape is wrong. But I don't know how to fix it.

It would be helpful if you could tell me how it works.

  • Answer # 1

    I'm not so confident, but how about this?

    "concurrently \" nodemon --watch postcss.config.js --exec \\\ "postcss src/style/base.css -o public/style/base.css -w \\\" ""

    The rules are simple,

    Escape characters to escape before enclosing them in double quotes

    The "character to escape" is (in this case)"When\ \

    "Escape" means before the character\ \Refers to attaching

    is.

    Innermost string"postcss src/style/base.css -o public/style/base.css -w"Starting from
    ↓ It changes like this.

    "postcss src/style/base.css -o public/style/base.css -w"
    ↓ Escape
    \ "postcss src/style/base.css -o public/style/base.css -w \"
    ↓ Surround with double quotes
    "\" postcss src/style/base.css -o public/style/base.css -w \ ""
    ↓ Add other words
    "nodemon --watch postcss.config.js --exec \" postcss src/style/base.css -o public/style/base.css -w \ ""
    ↓ Escape
    \ "nodemon --watch postcss.config.js --exec \\\" postcss src/style/base.css -o public/style/base.css -w \\\ ""
    ↓ Surround with double quotes
    "\" nodemon --watch postcss.config.js --exec \\\ "postcss src/style/base.css -o public/style/base.css -w \\\" ""
    ↓ Add other words
    "concurrently \" nodemon --watch postcss.config.js --exec \\\ "postcss src/style/base.css -o public/style/base.css -w \\\" ""

    I'm not so confident.