Running node.js code just displays a node identifier

前端 未结 9 2191
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-19 19:07

I have the following code in a file called server.js.

var http = require(\'http\');

http.createServer(function (request, response) {
  response.writeHead(200,          


        
9条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-19 19:57

    This happens when Harvest SCM is installed on your system. It has an executable with the name node.exe at \CA\SharedComponents\PEC\bin (where is your x86 program files folder). This path is present in your PATH variable before the path to Node.js's node.exe.

    Update: You don't need the elaborate scheme listed in the old answer. You just have to open the Command Prompt and run:

    C:\> nodevars
    

    nodevars.bat is a small script that does essentially the same thing described below (but in a safer way). If you have node installed, this script should be in path. (If not make sure to add C:\Program Files\nodejs to your path. But make sure to append it in the end so Harvest SCM does not break).


    Everything below is outdated, but I will leave it for the curious reader.

    You can do either of following two things you can do to overcome this problem:

    1. Remove \CA\SharedComponents\PEC\bin from PATH environment variable.
    2. Add/move \nodejs to the beginning of the PATH environment variable (This is the currently accepted answer from djrpascu).

    You can do better!

    There are two problems with the above approaches:

    1. You break Harvest SCM's functionality.
    2. If you do not have elevated privileges to change PATH, you are out of options. (Thanks @Glats)

    So I created this little batch file, and put it in a directory where I have several other personal scripts (this directory is in my PATH). Here's the gist for the script.

    nodecmd.bat

    @echo off
    
    set path=%path:C:\Program Files (x86)\CA\SharedComponents\PEC\bin;=%;C:\Program Files (x86)\nodejs;
    
    start %ComSpec%
    

    Then the next time you want to run Node.js, instead of Command Prompt, you open the new script with "Run..." command.

    Windows+R

    nodecmd

    A command prompt will appear. You can use this command prompt to run node without a hassle.

    Explanation

    This bit deletes the Harvest's executable's path from PATH variable:

    %path:C:\Program Files (x86)\CA\SharedComponents\PEC\bin;=%;
    

    And this adds the Node.js's path:

    set path=...;C:\Program Files (x86)\nodejs;
    

    The result is a string that contains the original PATH variable minus Harvest's path, plus Node's path. And it is set as PATH variable in the scope of current batch file.

    Note: You might have to change the path's in the script to suit software installation folders in your system).

    Next line, start %ComSpec% starts a Command Prompt. By this time, the PATH variabe is modified. With modified environment variables, you can run node within this new Command Prompt. The environment variable modification does not affect the rest of the system, making sure that Harvest SCM software runs without breaking.

提交回复
热议问题