Running node.js code just displays a node identifier

前端 未结 9 2120
爱一瞬间的悲伤
爱一瞬间的悲伤 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:56

    I faced the same problem and simply changed the the name of node.exe file from Harvest. This hasn't broken anything from Harvest and I can keep working with it.

    Change the Harvest's command name to node_.exe:

    ren "C:\Program Files (x86)\CA\SharedComponents\PEC\bin\node.exe" "C:\Program Files (x86)\CA\SharedComponents\PEC\bin\node_.exe"
    
    0 讨论(0)
  • 2021-02-19 19:56

    I think you're running the wrong node command. Try locating or re-downloading your nodejs installation and add it to your path as the first directory. If you're running linux or unix you can try 'which node' to see what is being run. Note that in some cases, the node.js executable is called nodejs so you may want to try nodejs server.js as well

    0 讨论(0)
  • 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 <Program Files (x86)>\CA\SharedComponents\PEC\bin (where <Program Files (x86)> 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 <Program Files (x86)>\CA\SharedComponents\PEC\bin from PATH environment variable.
    2. Add/move <Program Files (x86)>\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.

    0 讨论(0)
提交回复
热议问题