34

I'm trying to have a script that looks like this:

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

And it fails, at least on Windows, even if I run it from a Git Bash prompt. Even trying just mkdir ./my-dir doesn't work. I can't figure out any reason it should fail.

The error it gives is a "syntax incorrect" error:

> 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

If this won't work, is there a cross-platform friendly way to initialize an empty directory?

samanime
  • 25,408
  • 15
  • 90
  • 139

3 Answers3

21

This module possibly will solve your problem: https://www.npmjs.com/package/mkdirp

Stavros Zavrakas
  • 3,045
  • 1
  • 17
  • 30
  • 1
    Had to install the package globally (npm i -g mkdirp) to be able to use 'mkdirp dist/js -p' on Windows. But yeah it works :) – Hamish Rouse Oct 24 '16 at 19:37
  • 3
    Just a heads up, it's possible to install it locally, you just have to use `./node_modules/.bin/mkdirp` instead of just `mkdirp` from the locally installed folder. Holds true for pretty much all NPM commands. – samanime Dec 07 '16 at 22:50
  • 2
    You don't have to prefix `./node_modules/.bin/`because `npm` already adds this path to PATH. Reference: [NPM](https://docs.npmjs.com/cli/run-script) – xtu Jan 08 '18 at 21:54
  • 1
    I just noticed the last comment and wanted to add some clarification since it still gets a fair number of views. If you are using `mkdirp` (or any bin command) from an npm script (in your `package.json`, you don't need anything but the command. If you want to call it directly in your terminal, you either need `./node_modules/.bin/mkdirp` or install it with the `-g` flag. This was a case where everyone was right. =) – samanime Jan 21 '20 at 17:18
  • To note, now you can just do `npx mkdirp` instead of the directory path if you want to run it directly. If you run it as apart of an NPM script, it's still the same as my above comment. – samanime May 07 '20 at 03:31
18

You can use npx plus mkdirp for a relatively lightweight, portable solution without global installs

Add this to package.json:

"scripts": {
  "create-dir": "npx mkdirp _site/assets",
}

And then use like this:

$ npm run create-dir
KyleMit
  • 30,350
  • 66
  • 462
  • 664
12

Npm is using the windows mkdir command, not the linux. Use

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

Use backslashes.

You can also install cygwin and run:

/cygwin64/bin/mkdir -p mydir
abentan
  • 518
  • 5
  • 6