[SOLVED] Node.js command line tool opens in notepad instead of being executed

Issue

This Content is from Stack Overflow. Question asked by slowikowskiarkadiusz

I have a problem very similar to this one, but instead of a command line application, I have a ASP.NET Web API project with an Angular project inside of it, created using a dotnet template ‘angular’ (dotnet new angular --name something. .NET 6.0.401).

When I run the application with dotnet run and open localhost:5097 I get a blank page with a message “Launching the SPA proxy…
This page will automatically redirect to https://localhost:44415 when the SPA proxy is ready.” The console constantly prints

info: Microsoft.AspNetCore.SpaProxy.SpaProxyMiddleware[0]
SPA proxy is not ready. Returning temporary landing page.

over and over again.

When I enter localhost:44415 I get an error in the console that says

fail: Microsoft.AspNetCore.SpaProxy.SpaProxyLaunchManager[0]
Couldn't start the SPA development server with command 'npm start'.

and a notepad window is opened with content

:: Created by npm, please don't edit manually.
@ECHO OFF

SETLOCAL

SET "NODE_EXE=%~dp0node.exe"
IF NOT EXIST "%NODE_EXE%" (
  SET "NODE_EXE=node"
)

SET "NPM_CLI_JS=%~dp0node_modulesnpmbinnpm-cli.js"
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix -g') DO (
  SET "NPM_PREFIX_NPM_CLI_JS=%%Fnode_modulesnpmbinnpm-cli.js"
)
IF EXIST "%NPM_PREFIX_NPM_CLI_JS%" (
  SET "NPM_CLI_JS=%NPM_PREFIX_NPM_CLI_JS%"
)

"%NODE_EXE%" "%NPM_CLI_JS%" %*

which is understandably the same as the npm.cmd’s content in my node.js directory. The window name is also “npm.cmd”.

As mentioned in the thread I attached previously, I removed a file association for .js in windows settings, but it hasn’t changed anything. The last comment there says something about changing the contents of “lb-discover.cmd” file. I don’t think an ASP.NET + Angular project has an analogical file to it.

I suspect it might have something to do with file association in windows, but I can’t change it for .cmd files.

Thanks in advance.



Solution

Add #!/usr/bin/env node to the first line of lb-model-discovery/index.js. Then install the package again.

This script will be generated

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe"  "%~dp0\node_modules\lb-model-discovery\index.js" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node  "%~dp0\node_modules\lb-model-discovery\index.js" %*
)

instead of

@"%~dp0\node_modules\lb-model-discovery\index.js"   %*

Source: https://blog.npmjs.org/post/118810260230/building-a-simple-command-line-tool-with-npm


This Question was asked in StackOverflow by TheSoul and Answered by Marcos It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?