问题 不能在Windows的NPM脚本中使用mkdir


我正在尝试使用如下所示的脚本:

{
    "scripts":
        "setup": "mkdir -p ./my-dir"
}

它失败了,至少在Windows上,即使我从Git Bash提示符运行它也是如此。即使只是尝试 mkdir ./my-dir 不起作用。我无法弄清楚它应该失败的原因。

它给出的错误是“语法不正确”错误:

> my-app@0.0.1 stage C:\my-app
> mkdir ./my-dir

The syntax of the command is incorrect.

npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "stage"
npm ERR! node v6.4.0
npm ERR! npm  v3.10.3
npm ERR! code ELIFECYCLE
npm ERR! my-app@0.0.1 stage: `mkdir ./my-dir`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the my-app@0.0.1 stage script 'mkdir ./my-dir'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the my-app package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     mkdir ./my-dir
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs my-app
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls my-app
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\my-app\npm-debug.log

如果这不起作用,是否有一种跨平台友好的方式来初始化一个空目录?


5131
2017-09-27 14:55


起源

你能粘贴你的错误信息吗? - stavros.zavrakas
你是怎么称这个剧本的?应该 npm setup - Maria Ines Parnisari
添加了错误消息。我在用着 npm run setup。我的实际命令不是命名设置,这只是一个例子。 - samanime


答案:


这个模块可能会解决你的问题: https://www.npmjs.com/package/mkdirp


8
2017-09-27 15:42



这就是我最终使用的。它做了诀窍,谢谢。 - samanime
不得不全局安装软件包(npm i -g mkdirp)才能在Windows上使用'mkdirp dist / js -p'。但是它的确有效:) - Hamish Rouse
只需要抬头,就可以在本地安装,只需使用即可 ./node_modules/.bin/mkdirp 而不仅仅是 mkdirp 从本地安装的文件夹。几乎所有NPM命令都适用。 - samanime
你不必加前缀 ./node_modules/.bin/因为 npm 已经将此路径添加到PATH。参考: NPM - xtu


Npm使用的是windows mkdir命令,而不是linux。使用

"setup": "mkdir .\\my-dir"

使用反斜杠。

你也可以安装cygwin并运行:

/cygwin64/bin/mkdir -p mydir

1
2018-02-14 20:12



这适用于在bash终端上运行npm命令的Windows计算机,但不适用于 -p - Alberto Rechy