olekskop
olekskop

Reputation: 63

Nest js failed to execute command: with 'node'

I'm trying to init my first NestJS project but met this fail:

-----------------------------------------
$ nest new testproj
āš”  We will scaffold your app in a few seconds..

'node' is not recognized as an internal or external command,
operable program or batch file.

Failed to execute command: node @nestjs/schematics:application --name=testproj --directory=undefined --no-dry-run --no-skip-git --no-strict --package-manager=undefined --language="ts" --collection="@nestjs/schematics"
------------------------------------------

Tryed to reinstall NodeJS, but no luck. 

$ node -v
v16.13.1

$ nest -v
8.1.6

$ npm -v
8.3.0

Any help will be appreciated.

System variables set like that

Upvotes: 2

Views: 1697

Answers (1)

CherryDT
CherryDT

Reputation: 29012

tl;dr:

If your PATH somewhere has a file in it and not a folder (can also be in the middle of a path, with some \other\stuff appended like C:\stuff\somefile.txt\stuff), then this can happen due to an error when Git bash is translating PATH before calling cmd.exe, resulting in part of the PATH not being forwarded and making binaries in that part "not found".


Details:

After some investigation via chat, it turned out that the root cause was a bad GRADLE_HOME environment variable.

Yes, Gradle has nothing to do with node.js or nest, but bear with me, this is one of those moments where a TV episode starts with a totally crazy scene and you wonder what the heck happened that led to this, and then you get "6 hours earlier..." šŸ˜


So, Git bash obviously succeeded in finding node, because it ran the nest CLI (which is a node script). But then, somehow, cmd (which is called by node when executing shell commands) did not find node. This normally should not happen.

Tracing the events with Process Monitor revealed that bash (sh.exe) passed a truncated PATH variable to node.exe. It just ended abruptly somewhere in the middle, and C:\Program Files\nodejs (which was towards the end of it) was not passed along.

The reason for this turned out to be an entry in the PATH that looked like this: C:\foobar\file.zip\bin. The transition into bash worked, as the full path (including this bad entry as /c/foobar/file.zip/bin) could be seen in bash's $PATH, and /c/Program Files/nodejs was there too.

But the transition from bash to node.exe failed. In the process of converting the Linux-style paths to Windows-style paths before passing the variable on to node.exe, bash silently failed in the middle of the string and stopped processing it - as soon as this /c/foobar/file.zip/bin entry was encountered. C:\foobar\file.zip did exist, and it turns out Git bash behaves like this when it unexpectedly encounters a "not a directory" error from the OS when querying the path segment ("file not found" is fine) - as a result of attempting to access a "subdirectory" of a file. Removing this entry from the PATH made everything work normally.

The source of this entry was actually %GRADLE_PATH%\bin in the Windows PATH, and the reason this caused the problem was that GRADLE_PATH itself was incorrectly set to a file (C:\foobar\file.zip) instead of a directory.


There are three ways to resolve this:

  1. Remove %GRADLE_PATH%\bin from the PATH.
  2. Fix GRADLE_PATH to point to a directory.
  3. Delete or rename the C:\foobar\file.zip file.

Upvotes: 3

Related Questions