How to run node app with sublime text

2019-03-11 14:33发布

问题:

How would you run node app with sublime text? Like this, open the file app.js in sublime, go to menu->tools->build, and it just runs. Simple like that

回答1:

Cmd+Shift+P , search for "Nodejs::Default File Settings" ,it will open file "Node.js.sublime-settings". you'll see:

{
  // save before running commands
  "save_first": true,
  // if present, use this command instead of plain "node"
  // e.g. "/usr/bin/node" or "C:\bin\node.exe"
  "node_command": false,
  // Same for NPM command
  "npm_command": false,

  "expert_mode": false,

  "ouput_to_new_tab": false
}

modify

"node_command": false,

to

"node_command": "/usr/local/bin/node",

if the node path is not the same with above, find it and change to yours.



回答2:

If you want to fix the plugin's path yourself. One option is changing Nodejs.sublime-build. It's located in the packages directory of sublime:

Mac: ~/Library/Application Support/Sublime Text 2/Packages/Nodejs/Nodejs.sublime-build 

Linux: ~/.config/sublime-text-2/Packages/Nodejs/Nodejs.sublime-build

Note: On latest OS X versions the Library folder is hidden. If that's the case, from the menu select Go > Go to Folder... and type ~/Library.

Change "cmd": ["node", "$file"] to "cmd": ["/usr/local/bin/node", "$file"]

{
  "cmd": ["/usr/local/bin/node", "$file"],
  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
  "selector": "source.js",
  "shell":true,
  "encoding": "cp1252",
  "windows":
    {
        "cmd": ["taskkill /F /IM node.exe & node", "$file"]
    },
  "linux":
    {
        "cmd": ["killall node; node", "$file"]
    }
}

Lastly, open your *.js file and press command + b. Everything should just work fine now.

Linux Users: This file is identical across all operating systems. Finding the path to Nodejs.sublime-build may require running a search. In most cases it's located in ~/.config/sublime-text-2/Packages/Nodejs/Nodejs.sublime-build



回答3:

To run nodejs on sublime text, install node package "node dev" then create a sublime text build, the code should look like this

{
  "cmd": ["node-dev", "$file"],
  "selector" : "source.js",
  "path" : "/usr/local/bin"
}

Now to run a nodejs app, go to menu->tools->build.



回答4:

What is going on is that you don't have the right PATH setup for your terminal.

try this command in a regular terminal:

> which node

I peronaly get this:

/usr/local/bin/node

As you can see this path is not in you environement path to add it in a regular terminal you would edit .bashrc or .bash_profile and add this line

export PATH=/usr/local/bin:$PATH

Here well you just have to look at the doc and find out that you need to modify the configuration file.

If you have a JavaScript file open, by selecting selecting Tools -> Build Systems -> Nodejs and then hitting Ctrl + B, you will activate the node build system on your file and node will try to run it. You may need to add a path variable to the settings object for this if your node executable is not found

Look at this.



回答5:

On xubuntu, I made the build command in Nodejs.sublime-build explicity use the terminal:

"cmd": ["xfce4-terminal", "--command", "node $file"]