Saad
Saad

Reputation: 53879

mkdir not working in npm script on linux, but works on mac

I have a "prebuild" script that does the following:

mkdir -p dist/{server,shared,client/{css,js,fonts,img}}

I'd like for this to create a structure like so:

dist
    server
    shared
    client
        css
        js
        fonts
        img

When I run the mkdir command right from the terminal, it has the right output. However, if I put this command as an npm script like so:

package.json

{
    "scripts": {
        "prebuild": "mkdir -p dist/{server,shared,client/{css,js,fonts,img}}"
    }
}

And then when I do npm run prebuild, it only creates a single folder with a weird name: {server,shared,client.

So on Ubuntu, it only works when you type the command directly, but if you put it in an npm script you get this issue. On Mac, it works either way.

Does anyone know why this is happening?

Upvotes: 1

Views: 430

Answers (2)

VtoCorleone
VtoCorleone

Reputation: 17203

Great explanation from chepner. I have added to the answer to give a solution. Creating a link to the bash shell will allow you to run bash commands from the npm scripts.

$ sudo ln -sf bash /bin/sh

Hope this helps others running into this issue in Ubuntu.

Credit to the link solution.

Upvotes: 0

chepner
chepner

Reputation: 531808

npm uses /bin/sh to execute scripts, but Ubuntu uses dash as its POSIX shell, while Mac OS X uses bash. Brace expansion is a bash feature, which dash does not have.

bash (incorrectly, I would argue) still processes brace expansion when invoked as sh.

Upvotes: 7

Related Questions