meh_programmer meh_programmer - 2 months ago 13
Linux Question

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?

Answer

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.

Comments