Volazh
Volazh

Reputation: 146

Run JavaScript code SublimeText

I tried to run javascript code from SUBLIME TEXT 3 but no works. I tried by setting the build system, once i open it shows me:

{
    "shell_cmd": "make"
}

i change it by:

{
  "shell_cmd": ["C:/Program Files/nodejs/node.exe", "$file"],
  "selector": "source.js"
}

but the package no appear in build system. How can i solve this problem?

I have already installed NodeJS in my pc.

Upvotes: 3

Views: 9670

Answers (6)

Tumin
Tumin

Reputation: 11

1. For Mac

This worked for me. I had all my files in the same dir.

{
  "cmd": ["sh", "-c", "node < input.txt $file > output.txt"],
  "selector": "source.js"
}

Upvotes: 0

Marrasty 8189
Marrasty 8189

Reputation: 31

When I tried it before I made a new build system and pasted this

{
    "cmd": ["C:/Program Files/nodejs/node.exe", "$file"],
    "selector": "source.js",
}

Upvotes: 3

Ste
Ste

Reputation: 2293

No answer has file_patterns in it which is required, well at least I had trouble getting it to work without it.

{
  "selector": "source.js",
  "file_patterns": ["*js"],
  "cmd": ["C:/Program Files/nodejs/node.exe", "$file"],
}

More info on Sublime Texts' page:

https://www.sublimetext.com/docs/build_systems.html

Upvotes: 0

Yashvardhan
Yashvardhan

Reputation: 1

  1. For Windows Users

first you have to install nodeJS. If it is already installed open command prompt and type where node it will show the directory where it is installed, it must be here -

C:\Program Files\nodejs\node.exe

copy this path and go to environment variables Select PATH then EDIT then NEW and then PASTE the copied address and then click OK

Now open sublime text goto TOOLS > BUILD SYSTEM >NEW BUILD SYSTEM Paste the code given below

{
  "shell_cmd": ["C:/Program Files/nodejs/node.exe", "$file"],
  "selector": "source.js"
}

Save it with name JavaScript.sublime-build in location -

C:\Users\Acer\AppData\Roaming\Sublime Text 3\Packages\User

NOTE that here "Acer" is username. Now restart Sublime text and you will able to see a new build system named as JavaScript,

Upvotes: 0

Muriithi Derrick
Muriithi Derrick

Reputation: 342

1. For linux based os.

first install nodejs

sudo apt-get install nodejs-legacy

then create a build system under tools panel on sublime text, save it as javascript.sublime-build or anyword.sublime-build

then paste this code and save,

{
    "cmd": ["node", "$file"],
    "selector": "source.js"
}

make sure that the saving directory is:

/home/username/.config/sublime-text-3/Packages/User/

then on build systems select which build system you want. in this case select javascript.

example. create a new file like index.js and add this code.

var add = function(x,y){
    return x+y;
}

console.log(add(2,3));

then press ctl+B key. A console terminal opens at the bottomm of the page.

5
[Finished in 0.0s]

Upvotes: 0

idleberg
idleberg

Reputation: 12882

I don't see shell_cmd mentioned in the official Build Systems documentation and it doesn't work for in Sublime Text (Build 3119).

Update: If I'm interpreting error message in the console correctly, shell_cmd does not accept any arguments, hence you cannot pass $file!

Anyway, the following works just fine for me:

{
  "cmd": ["node", "$file"],
  "selector": "source.js",
  "windows" : {
     "shell": true
  }
}

Make sure you save the as whatever.sublime-build in Packages/User. That's the default location when you're using the dialog to create a new build system (Tools > Build System > New Build System).

Also, I see no reason to put the full path to node, since the Node.js installer adds it to your PATH environmental variable. If you want use an absolute path, you should probably follow Windows conventions and use back-slashes – just make sure they're escaped, which is JSON convention (e.g. C:\\Program Files\\nodejs\\node.exe).

Upvotes: 2

Related Questions