shelljs / shx

Portable Shell Commands for Node
MIT License
1.72k stars 44 forks source link

Issues running NPM script on Windows 10 #164

Closed stevendesu closed 5 years ago

stevendesu commented 5 years ago

Further research may be necessary to determine the exact cause of the issue. If you have trouble reproducing, I'm willing to provide any additional information to help out.

My environment:

My package.json looks like so:

{
    "name": "...",
    "version": "0.1.0",
    "description": "...",
    "main": "src/index.js",
    "scripts": {
        "build": "webpack --mode production",
        "clean": "shx rm -rf ./dist",
        "dev": "webpack --mode development",
        "lint": "eslint --ignore-pattern node_modules ./src ./plugins ./utilities ./scripts",
        "prebuild": "npm run lint && npm run clean",
        "predev": "npm run lint && npm run clean"
    },
    "repository": {...},
    "keywords": [...],
    "author": "..."
    "license": "...",
    "devDependencies": {
        "@babel/core": "^7.3.4",
        ... removed for brevity ...
        "shx": "^0.3.2",
        ... removed for brevity ...
        "webpack": "^4.29.6",
        "webpack-cli": "^3.2.3"
    },
    "dependencies": {
        "@babel/runtime": "^7.4.2",
        "video.js": "^7.4.1"
    }
}

They two key lines here are that shx is installed (and can be found in the node_modules directory) and the clean script, which utilizes shx. On my Mac this worked perfectly. On Windows I'm getting the following error:

C:\Users\Steven Barnett\Source\xxx> npm run clean

> xxx@0.1.0 clean C:\Users\Steven Barnett\Source\xxx
> shx rm -rf ./dist

'shx' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! xxx@0.1.0 clean: `shx rm -rf ./dist`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the xxx@0.1.0 clean script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Steven Barnett\AppData\Roaming\npm-cache\_logs\2019-07-29T18_07_41_648Z-debug.log

If I install shx globally then I can use it just fine:

C:\Users\Steven Barnett\Source\xxx> npm install -g shx
C:\Users\Steven Barnett\AppData\Roaming\npm\shx -> C:\Users\Steven Barnett\AppData\Roaming\npm\node_modules\shx\lib\cli.js
+ shx@0.3.2
added 19 packages from 15 contributors in 1.245s

C:\Users\Steven Barnett\Source\xxx> shx rm -rf dist

C:\Users\Steven Barnett\Source\xxx>

However attempting to use npx to run shx simply yields more errors:

C:\Users\Steven Barnett\Source\xxx>npx shx rm -rf dist
Error: EPERM: operation not permitted, mkdir 'C:\Users\Steven'
TypeError: Cannot read property 'get' of undefined
    at errorHandler (C:\Program Files\nodejs\node_modules\npm\lib\utils\error-handler.js:205:18)
    at C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js:76:20
    at cb (C:\Program Files\nodejs\node_modules\npm\lib\npm.js:228:22)
    at C:\Program Files\nodejs\node_modules\npm\lib\npm.js:266:24
    at C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:83:7
    at Array.forEach (<anonymous>)
    at C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:82:13
    at f (C:\Program Files\nodejs\node_modules\npm\node_modules\once\once.js:25:25)
    at afterExtras (C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:173:20)
    at C:\Program Files\nodejs\node_modules\npm\node_modules\mkdirp\index.js:47:53
C:\Program Files\nodejs\node_modules\npm\lib\utils\error-handler.js:205
  if (npm.config.get('json')) {
                 ^

TypeError: Cannot read property 'get' of undefined
    at process.errorHandler (C:\Program Files\nodejs\node_modules\npm\lib\utils\error-handler.js:205:18)
    at process.emit (events.js:182:13)
    at process._fatalException (internal/bootstrap/node.js:472:27)
Install for shx@latest failed with code 7

This and many others errors has made cross-platform development a real nightmare.

nfischer commented 5 years ago

Sorry you're having cross-platform headaches :(

I'm not sure why you're having problems with package.json scripts (this is a supported use case)--at a glance, your package.json looks fine. On Windows, npm modifies the %PATH% to include node_modules/.bin/, which should contain a script named shx.bat (a small wrapper which invokes node, passing our declared "binary" file as an argument). Running shx would invoke shx.bat in cmd.exe, and it seems you're not reaching this step. If you can identify which piece of the puzzle is missing on your system, I may be able to advise further.

I've never tried npx, so I can't remark on your results there.

stevendesu commented 5 years ago

Sorry for the delayed response. It will be some time before I can test this for you. Tuesday morning my wife and I went to the hospital to give birth to our new baby. Until Saturday I won’t be back home.

nfischer commented 5 years ago

Congrats! :tada: :balloon:

stevendesu commented 5 years ago

For some reason I'm not seeing a comment GitHub e-mailed me where someone reported they saw issues with shx rm but not shx mkdir. Perhaps they deleted the comment.

I'm working from home today so I sat down to look at this again (at the office I have a Mac, so I only really have issues when working from home on my Windows computer)

I took a peak in C:\Users\Steven Barnett\Source\xxx\node_modules\.bin and there is no shx.bat file there. The only files are webpack and webpack.cmd

Now here's the thing: I deleted node_modules and re-installed (via npm install). This time the folder was full of files. Not just webpack and shx, but also acorn, atob, cake, jsdoc, node-sass, etc.

My guess is that for some reason the NPM install process wasn't completing - perhaps node-sass was failing to compile or Webpack was attempting to copy a file into the .bin directory that did not exist. I suspect that the "copy things to the .bin directory" step is one of the last steps performed by NPM, and so everything else appeared to install correctly despite it error'ing out. I further suspect that a minor or patch update to one of my dependencies fixed the issues, because without making any changes to the package.json file it's magically working now.

nfischer commented 5 years ago

Ah, sounds like an npm bug in this case. I'll consider this resolved unless someone can identify shx-specific problems.

iamevansobeng commented 3 years ago

This is what resolved mine. I installed this shx package as dev dependency myself yarn add -D shx or npm i shx --dev

https://www.npmjs.com/package/shx

nikolay-tzonev commented 2 years ago

Hi all, I just tackled a similar issue on my environment.

My case is that I am using WSL console (Ubuntu terminal) in tandem with Windows PowerShell which turns out is not best practice. If you npm install the packages with WSL terminal the *.bat files are not installed in .bin folder and obviously running any shx ... commands in PowerShell (or anywhere outside WSL) would fail. So my fix is to stick to one terminal really.

Hope this would help someone. Cheers!