How to run node app with sublime text - node.js

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

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.

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

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.

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.

On xubuntu, I made the build command in Nodejs.sublime-build explicity use the terminal:
"cmd": ["xfce4-terminal", "--command", "node $file"]

Create a build sytem with this code:
{
"cmd": ["node", "$file"],
"selector" : "source.js"
}

First make sure node is installed properly.
Create new build system in sublime.
Tools > Build System > New Build System
It will create new file. replace the content with below comman
{
"shell_cmd": "node $file"
}
save the file with extention .sublime-build
i.e. node.sublime-build
Now select build system in tools>build system>node (or whatever name you have set)
All set. open js file and hit ctrl+B or go Tools>build

Related

Node.js live debugging in VSCode doesn't work

I have a small project (but issue also appears on the one created by npx create-react-app my-app). I use VsCode and developing inside container (https://code.visualstudio.com/docs/remote/containers) . Dockerfile is very minimal:
ARG VARIANT="16-bullseye"
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT}
devcontainer.json has, almost, just defaults:
{
"name": "Node.js",
"build": {
"dockerfile": "Dockerfile",
"args": { "VARIANT": "16-bullseye" }
},
"settings": {},
"extensions": [
"dbaeumer.vscode-eslint"
],
"remoteUser": "node"
}
To debug, I run npm start , then I (for the 1st time) i click on Debug URl.
On macOS everything works, including file monitoring, so when I change js file, npm is recompiling instantly.
On Windows 11 however, the last part doesn't work - I need to stop and start npm manually, to have changes implement.
I've even tried to remove .vscode directory from my profile - no change here...
Any idea what is going on? Why it does work on macOS and doesn't work on Windows ?
I have the same extensions on both systems...I just can't find what is going on with Windows machine...
EDIT: the issue seems to be related to "Remote Development" extension for vscode . Issue is present only when using this extension on Windows. So I've opened a bug there: https://github.com/microsoft/vscode-remote-release/issues/6633

Sublime build for XML with different extension

I have a project that uses a XML file of the type .uvprojx.
I have a sublime build I have found and modified slightly and it works fine but I can't get the build system to auto-detect that this is the build it needs to use. I need to manually go to Tools->Build systems -> My build.
This is my .sublime-build file:
{
"cmd": ["python", "C:/Users/cag/Documents/Tasks/PythonMisc/UvisionWrapper.py", "UV4", "-b", "-j0", "${file}", "-o", "out.txt"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.uvprojx",
"variants":
[
{
"name": "Flash",
"shell":true,
"cmd": ["python", "C:/Users/cag/Documents/Tasks/PythonMisc/UvisionWrapper.py", "UV4", "-f", "-j0", "${file}", "-o", "out.txt"]
},
{
"name": "Clean",
"shell":true,
"cmd": ["python", "C:/Users/cag/Documents/Tasks/PythonMisc/UvisionWrapper.py", "UV4", "-c", "-j0", "${file}", "-o", "out.txt"]
},
]
}
I have the python wrapper as the .exe file for the build creates a new process so I need to save the output to a file and read it in Python but anyway it works well.
What I want is that when I open a file with the extension .uvprojx this build is the default. If it matters Sublime detects this file as XML (which it is).
Change your selector to text.xml and add "file_patterns": ["*.uvprojx"], as per the documentation at http://www.sublimetext.com/docs/3/build_systems.html#options, then ST will auto-select the build system correctly for you.

How to configure nodejs build plugin inside Sublime Text 3

I am trying to configure this plugin:
https://github.com/tanepiper/SublimeText-Nodejs
in Sublime Text 3.
I have this settings as a default ones:
{
// 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,
// as 'NODE_PATH' environment variable for node runtime
"node_path": true,
"expert_mode": false,
"output_to_new_tab": false
}
And then I have my user setting:
{
"node_path": "C:/Program Files/nodejs/"
}
I also checked and I have node in the Path, that can be accessed globally. I use Windows 10, and still, the output shows that this plugin cannot find node.exe:
ERROR: The process "node.exe" not found. module.js:557
throw err;
^
Can somebody help me with that? Thanks!
NODE_PATH is using for setting paths where Nodejs will search modules to import in your code (see: https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders)
If you want to set path to the Nodejs executable you should use node_command option instead of node_path. Also, on Windows you should you use backslash as path separator NOT forward slash.

Nodejs build sublime-text2

I am trying to use sublime-text 2, have installed Nodejs for Windows, the Nodejs plugin through package control and I get the following error:
ERROR: The process "node.exe" not found.
The filename, directory name, or volume label syntax is incorrect.
[Finished in 0.1s with exit code 1]
I have setup as my user environment variable a NODE_PATH: C:\Program Files\nodejs\node.exe
There is in my System variables PATH: C:\Program Files\nodejs\
My Nodejs.sublime-settings is set-up as follows:
{
// 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": "C:/Program Files/nodejs/node.exe",
// Same for NPM command
"npm_command": false,
// as 'NODE_PATH' environment variable for node runtime
"node_path": "C:/Program Files/nodejs/node.exe",
"expert_mode": false,
"ouput_to_new_tab": false
}
My Nodejs.sublime-build is set-up as follows:
{
"cmd": ["C:/Program Files/nodejs/node.exe", "$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"]
}
}
As a side note, I'm using JSHint which uses Nodejs using the same path (namely "C:/Program Files/nodejs/node.exe") and JSHint works!
Any idea why I can't use Nodejs build system?
Thx
Try setting your build system just to the following for the time being:
{
"cmd": ["C:/Program Files/nodejs/node.exe", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.js",
"shell":true,
}
Because you have a Windows-specific section, it's running that instead of the "cmd" on the first line of the build system. I suspect there's some sort of issue with the taskkill command.
If this does work, and you feel the need to have the taskkill section back in there, try restructuring it like so:
"windows":
{
"cmd": ["taskkill", "/F", "/IM", "node.exe", "&", "C:/Program Files/nodejs/node.exe", "$file"],
"shell": true
}
Obviously, you don't need the linux section in there at all. I'm not too sure about the syntax on windows, you may need to have two ampersands && there instead of one - I know that's the case on OS X and Linux systems.
Good luck!

Runnnig node.js as a windows service

How to configure WinSer to run a specific javascript file?
I understood that I have to add in package.json the lines:
{
"name": "MY_SERVICE",
"scripts": {
"install-windows-service": "node_modules\\.bin\\winser -i",
"uninstall-windows-service": "node_modules\\.bin\\winser -r"
}
I couldn't understand where to configure what javascript file I should run?
From a command line in the same folder as your package.json, run
winser -i
to install your node app (as defined in server.js in the same folder) as a service. Note that if you have not logged off and back on since you installed winser, your path to winser.cmd will not be initialized (it needs something like C:\Users\YourName\AppData\Roaming\npm\ in the path.

Resources