Sei sulla pagina 1di 11

pawelgrzybek.

com about archive

Jun 12, 2015

JavaScript console in Sublime


Text
Learning JavaScript can be a long and diicult journey. You can read tons of books and articles,
attend reputable conferences and watch many videos but you are wasting your time if you
wont practice it. Google Chrome Javascript Console isnt the most comfortable tool for sure.
Tools like jsbin.com or jsfiddle.net are super cool but lets confirm that, nothing is more
convenient to write a piece of code than your favourite editor. Let me share with you how you
can test your JavaScript scripts and execute it via build systems feature in Sublime Text 3.

Build systems allows you to run your files through external programs without manually
switching between code editor and Terminal. Sublime Text comes with few predefined profiles.
Our mission is to add a new one for JavaScript. We are going to use node.js. Make sure it is
already installed on your device.

Create JavaScript build system in Sublime


Text
Every built system is described on JSON file with .sublime-build extension. To create new one
go to Tools > Build System > New Build System If you are mac OS user, copy and paste this bit
of code:

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

Windows users

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

In most cases single node command does the job instead of passing a full path to node. I found
this method more reliable though. If you are not sure where node is installed, just type which
node in Terminal (mac OS) or where node in command prompt (windows users) to get the path.
Save this file as a JavaScript.sublime-build in User directory inside Sublime Text settings
folder.

Time for fun part. Create and save a JavaScript file (to execute a node interpreter, file needs to
be saved), code some super amazing script and click Cmd + B (mac OS) or F7 (Windows). You
can run this task manually from Tools > Build. Sublime Text should automatically run your script
through appropriate build system. If something doesnt work correctly go to Tools > Build
System and make sure that Automatically or JavaScript is selected. Enjoy!
Tweet Share

57Comments pawelgrzybek.com
1 Login

Recommend 15 Share SortbyBest

Jointhediscussion

yana_dzhi3monthsago
Hithere!
Thisthingdoesn'tworkonmycomputer(Windows7).
I'vedoneeverythinglikeyousaid:downloadedNode,foundthewayforitincommandprompt,
createdaNewBuildSystem,pasted
{
"cmd":["C:/ProgramFiles/nodejs/node.exe","$file"],
"selector":"source.js"
}
}
thensaveditasaJavaScript.sublimebuildinUserdirectoryinsideSublimeTextsettings.
AfterthatIopenedmysaved.jsfile,pressedF7andnothinghappenes.
Noerrors.Nothing.
Please,helpme!
1 Reply Share

PaweGrzybek Mod >yana_dzhi 3monthsago

Hi,noworrieswewillmanagedtogetitwork@yana_dzhi

Firsthavealookathelpfulcommentby@VPak
http://disq.us/p/1f5m2jj

Isitanyhelpforyou?Makesurethatyouhaveacorrectpathtonode,fileissavedand
scriptactuallylogssomeoutput
Reply Share

DarkCappuccino7monthsago
Thankyouverymuch.Thisworkedlikeacharm.
1 Reply Share

PaweGrzybek Mod >DarkCappuccino 7monthsago

I'mgladthatithelpedyouout.Haveagreatday@DarkCappuccino
Reply Share

weezillaayearago
Windowsusers:SublimeTextneedstoknowwheretofindaprogramthatcanrunjavascript.Use
WinkeyR,type"cmd",andentertoopencommandprompt.Type"wherenode".Thepathitreturns
iswhatyoushouldtypeintotheconfigfileinSublimeText.Formeitwas"C:/Program
Files/nodejs/node.exe".If"wherenode"complainsaboutnotbeingfound,youshoulddownload
nodejsfromnodejs.org(thenusetheabovestepstopointsublimetotheinstalledexe).
1 Reply Share

PaweGrzybek Mod >weezilla ayearago

Iupdatethisarticlewithsnippetprovidedbyyou.Thanksatonforyourhelpagain.
1 Reply Share

PaweGrzybek Mod >weezilla ayearago

Thankyouforclarification.I'mnotaWindowsusersoitwashardformetoreplytosome
commenters.
1 Reply Share

KaMok>PaweGrzybekayearago
Pleaseupdateyourarticlewiththe"where"alternativeforWindowsuser.ihadto
googlefutheproblemuntilireachedtheendofthepostandsawthis.Woulda
savedmeagood10minutes.
savedmeagood10minutes.
1 Reply Share

PaweGrzybek Mod >KaMok ayearago

Ijustdidit.Thanksforsuggestion.Iwasplaningthatforsometimebecause
itsquitepopularpost.Thanksalot.
1 Reply Share

weezilla>PaweGrzybekayearago
Yourposthelpedmesogladtocontributetoyourblog.Cheers.
1 Reply Share

Pabloamonthago
Thanksforsharing!
Reply Share

RichardBatson2monthsago
Thankyou...thanx:)
Reply Share

PaweGrzybek Mod >RichardBatson 2monthsago

Noworries,gladithelps:)
Reply Share

Nikhil4monthsago
Awesome..Workslikecharm!
Reply Share

VPak4monthsago
ThesearestepsforaWindows10Home:
1.Downloadsublimetexthttps://www.sublimetext.com/3(itusuallyshowswhichoneyouneedby
blackdot)
2.Downloadnode.jsathttps://nodejs.org/en/(Recommendedformostusers)
3.gotoTools>BuildSystem>NewBuildSystem
4.saveasJavascriptintheUserdirectoryinsideSublimeTextsettingsfolder

{
"cmd":["C:/ProgramFiles/nodejs/node.exe","$file"],
"selector":"source.js"
}5.tocheckit,followthelastparagraphofthisarticlefrom
Timeforfunpart.Createandsave.....
GOODLUCK!
Reply Share

PaweGrzybek Mod >VPak 4monthsago

Thanksalotfordetailedinstruction@VPak.Haveagreatday
Thanksalotfordetailedinstruction@VPak.Haveagreatday
Reply Share

AhmadGinanjar5monthsago
WorkedatSublimeText3,thanksforsharing.:)
Reply Share

PaweGrzybek Mod >AhmadGinanjar 5monthsago

I'mgladithelpedyouout.Enjoy:)
Reply Share

knosis8monthsago
Iwaswonderinghowtoprint/savetheoutputofthebuildthatshowedupintheconsole.Modifieda
largebitofjsoninmyfileandwonderinghowtogetitout.
Reply Share

TonyLopes9monthsago
sweet!,thanks!
Reply Share

Zak9monthsago
Hi,completenoobherewiththiskindofstuff...FollowedeverythingupuntilafterIsaveit.I'mnot
reallysurewhattodoaftersavingitorhowtoproceed.I'musingwindows10bytheway.Ialso
downloadedthe"Current"versionofnode.js,Idon'tknowifthataffectsanything.
Reply Share

PaweGrzybek Mod >Zak 9monthsago

HiZak.Aslongasyoudidallthestepscorrectlyandsavedthebuildconfigfileyoushould
beabletoexecuteyourjsfileinsideSublimeText.Makesurethatyourfileextensionis.js.
ThenmakesurethatinmenuTools>Buildsystemis"Automatic"or"JavaScript".Then
insideyourfilehitCtrl+b.Itshouldexecuteyourfilewithnoissue.Fileneedstobesaved
onharddrivebeforehand.
Reply Share

Zak>PaweGrzybek9monthsago
Hi,thanksforyourquickreply,yourlastsentenceiswhatIdidn'tdo.Ijustopeneda
newtabandsaveditasxyz.jsandretriedanditworked.Thankyousomuchforthe
tutorial.
Reply Share

PaweGrzybek Mod >Zak 9monthsago

Noworries,I'mgladImanagedtohelp:)
Reply Share

DelanteLeeBess9monthsago
Thanks!WorksonbothmyMacandWindowslaptops.
Reply Share

PaweGrzybek Mod >DelanteLeeBess 9monthsago

I'mreallygladthatyoufoundituseful.Enjoy:)
Reply Share

RickEnciso9monthsago
Thanks!!

IhadnodeinstalledthroughNVMsoitdidn'tworkformeatfirst,soIjustswitchedthepathtonode
likethis:

firstofallyoucangetthecorrectpathbytyping$whichnodeinyourterminal

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

//nvmpath

{
"cmd":["/Users/user/.nvm/versions/node/v4.4.7/bin/node","$file"],
"selector":"source.js"
}
Reply Share

PaweGrzybek Mod >RickEnciso 9monthsago

Exactly.Aslongasthenodepathiscorrectitsgonnawork.ThanksforcontributionRick!
Reply Share

DmitriyShesterkin10monthsago
Thankyouverymuch!
Reply Share

PaweGrzybek Mod >DmitriyShesterkin 10monthsago

I'mgladyoufoundthistutorialhelpful!
1 Reply Share

MuhammadBinQasimayearago
Hello.....yousaidthatweneedtofindtherightpath.whatdoyoumeanbythat??
theproblemisthis.andyousaidfindtherightpath....canyoupleasedirectus

[WinError2]Thesystemcannotfindthefilespecified
[cmd:['/usr/local/bin/node','C:\\Users\\Muhammad\\Desktop\\1.js']]
[dir:C:\Users\Muhammad\Desktop]
[path:
C:\Windows\system32C:\WindowsC:\Windows\System32\WbemC:\Windows\System32\WindowsPowerS
Files\Intel\WiFi\bin\C:\ProgramFiles\CommonFiles\Intel\WirelessCommon\C:\ProgramFiles
(x86)\ATITechnologies\ATI.ACE\CoreStaticC:\ProgramFiles\Intel\WiFi\bin\C:\Program
Files\CommonFiles\Intel\WirelessCommon\]
[Finished]
Reply Share

weezilla>MuhammadBinQasimayearago
Seemycommentabove.
Reply Share

phamcongayearago
Canyoutellmewhichkindofthemeyouused?Itsverynice.Thank!
Reply Share

PaweGrzybek Mod >phamcong ayearago

Hello.Itisa"Spacegray".Feelfreetograbmywholeconfigurationfilethatisavailablehere:
https://github.com/pawelgrz...
Reply Share

phamcong>PaweGrzybekayearago
Thanksomuch.I'lltryit))
1 Reply Share

Adeyemiayearago
WorksonMacperfectly!
Reply Share

PaweGrzybek Mod >Adeyemi ayearago

I'mgladithelped.Haveagreatday!
Reply Share

Avatar Thiscommentwasdeleted.

PaweGrzybek Mod >Guest ayearago

I'mgladitworksforyou.Snippetabovehasdoublequotes:)
Reply Share

Avatar Thiscommentwasdeleted.

PaweGrzybek Mod >Guest ayearago


Yessir.Nowyoucanusebacktickasandputsomeexpressioninsideas
well.

letsomeString=`2+2=${2+2}`;

Reply Share

amireghbalianayearago
[WinError2]Thesystemcannotfindthefilespecified

[cmd:['C:\\Users\\username\\node.exe','']]

[dir:C:\ProgramFiles(x86)\SublimeText3]

[path:C:\ProgramFiles(x86)\NVIDIA
Corporation\PhysX\CommonC:\Windows\system32C:\WindowsC:\Windows\System32\WbemC:\Windows
Files(x86)\CommonFiles\Acronis\SnapAPI\C:\ProgramFiles(x86)\MicrosoftSQL
Server\100\Tools\Binn\C:\ProgramFiles\MicrosoftSQLServer\100\Tools\Binn\C:\Program
Files\MicrosoftSQLServer\100\DTS\Binn\C:\Program
Files\MATLAB\R2014b\runtime\win64C:\ProgramFiles\MATLAB\R2014b\binC:\Program
Files\MATLAB\R2014b\polyspace\binC:\Program
Files\nodejs\C:\Users\Amir\AppData\Roaming\npm]

[Finished]

ihavethesameproblemhere
cananybodyhelpmeplz?
Reply Share

weezilla>amireghbalianayearago
Seemycommentaboveaspossiblesolution.
Reply Share

minayearago
heythere,itriedyourinstructionsforsublimetext2onmac,anditworked:Dbutnowi'mtryingit
forsublimetext3onwindows7,i'mgettingthiserrorwhenisavemyjsfile:

[WinError2]Thesystemcannotfindthefilespecified

[cmd:['/usr/local/bin/node','C:\\Users\\user\\Desktop\\js\\recursivefunction.js']]

[dir:C:\Users\user\Desktop\js]

[path:
C:\Windows\system32C:\WindowsC:\Windows\System32\WbemC:\Windows\System32\WindowsPowerS
Files(x86)\Skype\Phone\C:\ProgramFiles\nodejs\C:\Users\user\AppData\Roaming\npm]

[Finished]

i'mnotsurewhatswrong..isitafaultyinstallofnodejsthatiscausingthis?:(
Reply Share

VNamHng>minayearago
itshouldbesomethinglike'C:/ProgramFiles/nodejs/node'insteadof'/usr/local/bin/node'
1 Reply Share

PaweGrzybek Mod >min ayearago

Hello.Itlookslikeyouhaveawrongpathtonodeinsideyourbuildsystem.Youneedtofind
acorrectpathwhereyournodeisinstalledandreplacethisstring"/usr/local/bin/node"with
yourpath.
Reply Share

DCDudeayearago
Thankyou!Ilearnedhowtouse"which",too!
Reply Share

PaweGrzybek Mod >DCDude ayearago

I'msupergladthatIhelped.Enjoy!
Reply Share

billyzhangayearago
Itworksforme.Thankyou.StarttousenodeinSublimeText3now.Iamabletorunasingle
node.jsfileforsomefunctionvalidation.
Reply Share

PaweGrzybek Mod >billyzhang ayearago

I'mgladithelped.Enjoy!
Reply Share

Loadmorecomments

Subscribe d AddDisqustoyoursiteAddDisqusAdd Privacy

Copyright 2017 Pawel Grzybek | Powered by Jekyll

Potrebbero piacerti anche