npm / npx

npm package executor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] `findNodeScript()` cannot find correct node script path in win32 (at least webpack package))

joonhwan opened this issue · comments

What / Why

In my Windows 10 Box, I cannot run npx webpack produces following outputs

see following cmd log.

C:\prj\webpack-demo>type package.json
{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack --mode production && exit 0"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "http-server": "^0.12.1",
    "webpack": "^4.42.1",
    "webpack-cli": "^3.3.11"
  },
  "dependencies": {
    "lodash": "^4.17.15"
  }
}

C:\prj\webpack-demo>dir node_modules\webpack*
 C 드라이브의 볼륨: SSD
 볼륨 일련 번호: 1E07-5DF1

 C:\prj\webpack-demo\node_modules 디렉터리

2020-03-26  오후 02:54    <DIR>          webpack
2020-03-26  오후 02:54    <DIR>          webpack-cli
2020-03-26  오후 02:54    <DIR>          webpack-sources
               0개 파일                   0 바이트
               3개 디렉터리  40,466,583,552 바이트 남음

C:\prj\webpack-demo>npx webpack
Command failed: webpack spawn EPERM

As seen, npx webpack should produce some webpack commands.

On the other hand, when I run webpack directory, it just works.

C:\WorkSpace\prj\study\webpack\webpack-demo>node_modules\.bin\webpack
Hash: 029a3c105afe97d2be1b
Version: webpack 4.42.1
Time: 298ms
Built at: 2020-03-26 15:46:51
  Asset      Size  Chunks             Chunk Names
main.js  72.1 KiB       0  [emitted]  main
Entrypoint main = main.js
[1] d:/WorkSpace/prj/study/webpack/webpack-demo/src/index.js 242 bytes {0} [built]
[2] (webpack)/buildin/global.js 472 bytes {0} [built]
[3] (webpack)/buildin/module.js 497 bytes {0} [built]
    + 1 hidden module

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/

C:\WorkSpace\prj\study\webpack\webpack-demo>

When

See above

Where

I'm not node.js developer guy 😢 with just a handful amount of javascript knowledge, so I did kinda console.log debugging of this issue. and figured out followings code findNodeScript() in index.js does not work correctly

In that function,

         // findNodeScript() function body....

          const cmd = /"%~dp0\\node\.exe"\s+"%~dp0\\(.*)"\s+%\*/
          const mingw = /"\$basedir\/node"\s+"\$basedir\/(.*)"\s+"\$@"/i
          return str.match(cmd) || str.match(mingw)

actually cannot see the cmd pattern in batch script(c:\prj\webpack-demo\node_modules\.bin\webpack.cmd script in my case) of which contents is

@ECHO off
SETLOCAL
CALL :find_dp0

IF EXIST "%dp0%\node.exe" (
  SET "_prog=%dp0%\node.exe"
) ELSE (
  SET "_prog=node"
  SET PATHEXT=%PATHEXT:;.JS;=;%
)

"%_prog%"  "%dp0%\..\webpack\bin\webpack.js" %*
ENDLOCAL
EXIT /b %errorlevel%
:find_dp0
SET dp0=%~dp0
EXIT /b

With that script, the findNodeScript() cannot find actual node script's path which is supposed to be like C:\prj\webpack-demo\node_modules\webpack\bin\webpack.js in my case.

How

Current Behavior

See above

Steps to Reproduce

See above

Expected Behavior

findNodeScript() should parse the correct node script file path from script in win32 environment. See above.

Who

  • n/a

References

  • n/a

Try npx webpack-cli when you'd otherwise run ./node_modules/.bin/webpack.

Try npx webpack-cli when you'd otherwise run ./node_modules/.bin/webpack.

C:\prj\webpack-demo>npx webpack-cli
Command failed: webpack-cli spawn EPERM

😢 It does not help.

I have c:\prj\webpack-demo\node_modules\.bin\webpack-cli.cmd of which contents is

@ECHO off
SETLOCAL
CALL :find_dp0

IF EXIST "%dp0%\node.exe" (
  SET "_prog=%dp0%\node.exe"
) ELSE (
  SET "_prog=node"
  SET PATHEXT=%PATHEXT:;.JS;=;%
)

"%_prog%"  "%dp0%\..\webpack-cli\bin\cli.js" %*
ENDLOCAL
EXIT /b %errorlevel%
:find_dp0
SET dp0=%~dp0
EXIT /b

if i change the findNodeScript() in index.js

const cmd = /"%~dp0\\node\.exe"\s+"%~dp0\\(.*)"\s+%\*/

to

const cmd = /"%_prog%"\s+"%dp0%\\(.*)"\s+%\*/

npx webpack works

... though it may seem to be ad-hoc solution for win32 case

the parsed part of above regex is for file node_modules\.bin\webpack.cmd

@ECHO off
SETLOCAL
CALL :find_dp0

IF EXIST "%dp0%\node.exe" (
  SET "_prog=%dp0%\node.exe"
) ELSE (
  SET "_prog=node"
  SET PATHEXT=%PATHEXT:;.JS;=;%
)

"%_prog%"  "%dp0%\..\webpack\bin\webpack.js" %*
ENDLOCAL
EXIT /b %errorlevel%
:find_dp0
SET dp0=%~dp0
EXIT /b

with the exact line of

"%_prog%"  "%dp0%\..\webpack\bin\webpack.js" %*

part.

Found this issue already reported in #5 ,