mkdir在linux上的npm脚本中不起作用,但在mac

时间:2016-09-23 20:27:32

标签: node.js linux bash ubuntu npm

我有一个"prebuild"脚本执行以下操作:

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

我想为此创建一个这样的结构:

dist
    server
    shared
    client
        css
        js
        fonts
        img

当我从终端运行mkdir命令时,它具有正确的输出。但是,如果我把这个命令作为像这样的npm脚本:

的package.json

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

然后当我执行npm run prebuild时,它只创建一个名称奇怪的文件夹:{server,shared,client

所以在Ubuntu上,它只能在你直接输入命令时起作用,但是如果你把它放在npm脚本中就会出现这个问题。在Mac上,无论哪种方式都可以。

有谁知道为什么会这样?

2 个答案:

答案 0 :(得分:7)

npm使用/bin/sh执行脚本,但Ubuntu使用dash作为其POSIX shell,而Mac OS X使用bash。大括号展开是bash功能,dash没有。

bash(错误地,我认为)在调用sh时仍会处理大括号扩展。

答案 1 :(得分:0)

来自chepner的很好的解释。我已经添加了答案来提供解决方案。创建指向bash shell的链接将允许您从npm脚本运行bash命令。

$ sudo ln -sf bash /bin/sh

希望这有助于其他人在Ubuntu中遇到此问题。

Credit to the link solution.