{"id":13785,"date":"2017-10-30T17:07:07","date_gmt":"2017-10-30T16:07:07","guid":{"rendered":"https:\/\/supporttest.hemsida.eu\/artikel\/hur-kommer-jag-igang-med-node-js\/"},"modified":"2023-05-16T13:39:55","modified_gmt":"2023-05-16T12:39:55","slug":"how-do-i-get-started-with-node-js","status":"publish","type":"ht_kb","link":"https:\/\/supporttest.hemsida.eu\/en\/artikel\/how-do-i-get-started-with-node-js\/","title":{"rendered":"How do I get started with Node.js?"},"content":{"rendered":"\n<p class=\"wp-block-ht-blocks-messages wp-block-hb-message wp-block-hb-message--withicon is-style-alert\">This guide is written for advanced users.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"via-cpanel\">Via cPanel<\/h2>\n\n\n\n<p>To get started with Node.js and creating a new application, begin by <a rel=\"noreferrer noopener\" href=\"https:\/\/supporttest.hemsida.eu\/en\/artikel\/how-do-i-login-to-cpanel\/\" data-type=\"ht_kb\" data-id=\"4236\" target=\"_blank\">logging on to cPanel<\/a>. Then, click the icon <code>Setup Node.js App<\/code> under <code>Software<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2017\/10\/cpanel_icon_nodejs.png\"><img loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"256\" src=\"https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2017\/10\/cpanel_icon_nodejs.png\" alt=\"\" class=\"wp-image-18787\" srcset=\"https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2017\/10\/cpanel_icon_nodejs.png 716w, https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2017\/10\/cpanel_icon_nodejs-300x107.png 300w, https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2017\/10\/cpanel_icon_nodejs-50x18.png 50w, https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2017\/10\/cpanel_icon_nodejs-60x21.png 60w, https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2017\/10\/cpanel_icon_nodejs-100x36.png 100w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/a><\/figure>\n\n\n\n<p>Once there, you can see all your existing Node.js applications, and also create a new one:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2017\/10\/skapa_nodejs_app-1.png\"><img decoding=\"async\" src=\"https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2017\/10\/skapa_nodejs_app-1.png\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<p>After clicking the<code> Create application<\/code> button, you get to adjust some settings for your app:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2019\/10\/settings_for_node.js-app.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1668\" height=\"1692\" src=\"https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2019\/10\/settings_for_node.js-app.png\" alt=\"\" class=\"wp-image-7656\"\/><\/a><\/figure>\n\n\n\n<p>The six available options imply the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Node.js version<\/code> &#8211; the version of Node.js you want to use.<\/li>\n\n\n\n<li><code>Application mode<\/code> &#8211; here you select if the app should be run in development or production mode.<\/li>\n\n\n\n<li><code>Application root<\/code> &#8211; the folder on the server where you&#8217;re going to upload the files for the Node.js application. Keep in mind that the application root shouldn&#8217;t be the same folder as the document root for the domain you use (below).<\/li>\n\n\n\n<li><code>Application URL<\/code> \u2013 the domain and URI where the app should be accessible. Leave it blank to run the application directly on the domain. To use a specific directory, enter <code>\/directory<\/code>, <em>e.g.<\/em> <code>\/node-app<\/code> to make your application accessible via <code>yourdomain.com\/node-app<\/code>.<\/li>\n\n\n\n<li><code>Application startup file<\/code> \u2013 unless you have a specific startup file for your app, you can leave this field empty. If the field is left blank, cPanel will create an <code>app.js<\/code> file for you.<\/li>\n\n\n\n<li><code>Passenger log file<\/code> &#8211; here you set a directory where you wish logs from the application to be saved.<\/li>\n<\/ul>\n\n\n\n<p>Once you&#8217;re done adjusting the settings, click <code>Create<\/code>. Now a <code><a rel=\"noreferrer noopener\" href=\"http:\/\/docs.python-guide.org\/en\/latest\/dev\/virtualenvs\/\" target=\"_blank\">virtualenv<\/a><\/code> will be created where you can install Node.js packages for your application. A <code>virtualenv<\/code> in the Node.js world can be described as a virtual environment where you can install packages that will not interfere with the system or with other <code>virtualenv<\/code>s. Thus, it&#8217;s a way to separate your different Node.js environments. You can access a specific <code>virtualenv<\/code> after logging on to your web hosting account via SSH. More information about connecting via SSH can be found <a rel=\"noreferrer noopener\" href=\"https:\/\/supporttest.hemsida.eu\/en\/artikel\/how-do-i-connect-to-my-web-hosting-account-using-ssh\/\" data-type=\"ht_kb\" data-id=\"4832\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p>After initial setup, you can see your new installation in the list of created Node.js applications.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2017\/10\/lista_over_node-appar.png\"><img decoding=\"async\" src=\"https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2017\/10\/lista_over_node-appar.png\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<p>If you click the pen icon, you will get access to the settings for the application:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2017\/10\/installningar_uppsatt_nodejs_app.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1664\" height=\"2536\" src=\"https:\/\/supporttest.hemsida.eu\/wp-content\/uploads\/2017\/10\/installningar_uppsatt_nodejs_app.png\" alt=\"Inst\u00e4llningar f\u00f6r en uppsatt Node.js-applikation\" class=\"wp-image-7663\"\/><\/a><\/figure>\n\n\n\n<p>Here, you will see information about the Node.js app that is set up, and you can also edit the settings.<\/p>\n\n\n\n<p>On top, you have a few buttons to manage your application:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Destroy<\/code> \u2013 delete your application.<\/li>\n\n\n\n<li><code>Cancel<\/code> \u2013 discard changes and return to the list of applications.<\/li>\n\n\n\n<li><code>Save<\/code> \u2013 save changes made to the application.<\/li>\n<\/ul>\n\n\n\n<p>Under the buttons, you&#8217;ll find <code>Enter to the virtual environment<\/code>. This command can be copied and run via SSH to activate the virtual Node.js environment (<code>virtualenv<\/code>) for your application. Please refer to the section above regarding <code>virtualenv<\/code> and SSH access.<\/p>\n\n\n\n<p>A bit further down, you&#8217;ll find more buttons:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Stop app<\/code> \u2013 stop the Node.js application. Will change to <code>Start app<\/code> if the application is currently stopped.<\/li>\n\n\n\n<li><code>Restart<\/code> \u2013 restart the application.<\/li>\n<\/ul>\n\n\n\n<p>You can also manage settings for your application:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Node.js Version<\/code> \u2013 the chosen Node.js version.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Application mode<\/code>,<code>Application root,<\/code> <code>Application URL<\/code>, <code>Application startup file<\/code> samt <code>Passenger log file<\/code> \u2013 please refer to the section above regarding these.<\/li>\n\n\n\n<li><code>Detected configurations files<\/code> \u2013 here you may install various node packages via npm.<\/li>\n\n\n\n<li><code>Environment variables<\/code> \u2013 here you may set various environment variables for your application.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"via-ssh\">Via SSH<\/h2>\n\n\n\n<p class=\"wp-block-ht-blocks-messages wp-block-hb-message wp-block-hb-message--withicon is-style-danger\">You need to have full knowledge regarding how to use SSH to follow these instructions.<\/p>\n\n\n\n<p>You may also manage certain aspects of your Node.js application via SSH.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"restart-a-node-js-application\">Restart a Node.js application<\/h3>\n\n\n\n<p>To restart a Node.js application via SSH, begin by connecting to your web hosting account <a href=\"https:\/\/supporttest.hemsida.eu\/en\/artikel\/how-do-i-connect-to-my-web-hosting-account-using-ssh\/\" data-type=\"ht_kb\" data-id=\"4832\" target=\"_blank\" rel=\"noreferrer noopener\">via SSH<\/a>.<\/p>\n\n\n\n<p>Go to the directory that you&#8217;ve chosen as application root for the app when you created it (according to the <a href=\"#via-cpanel\">instructions above<\/a>).<\/p>\n\n\n\n<p>Then create the folder <code>tmp<\/code> in your application root if it doesn&#8217;t already exist. Then create or modify the file <code>restart.txt<\/code> in that folder to force your application to restart. For example, you may use the following command after <code>cd<\/code>&#8216;ing to the correct folder:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">touch tmp\/restart.txt<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"select-node-js-version\">Select Node.js version<\/h3>\n\n\n\n<p>If you require Node.js to run certain cli scripts\/programs, and want to select what version of Node to run, you can use <code>nvm<\/code> (Node Version Manager).<\/p>\n\n\n\n<p>To get started, you need to log on to your web hosting account <a href=\"https:\/\/supporttest.hemsida.eu\/en\/artikel\/how-do-i-connect-to-my-web-hosting-account-using-ssh\/\" data-type=\"ht_kb\" data-id=\"4832\" target=\"_blank\" rel=\"noreferrer noopener\">via SSH<\/a>.<\/p>\n\n\n\n<p>You&#8217;ll find nvm here: <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/creationix\/nvm\" target=\"_blank\">https:\/\/github.com\/creationix\/nvm<\/a><\/p>\n\n\n\n<p>To install it on your account, connect to the account via SSH and run their install script (please refer to the URL above for instructions). Then run the following command to enable nvm:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">. .\/.bashrc<\/pre>\n\n\n\n<p>If you for example want to install the latest version of Node 10, run:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">nvm install 10<\/pre>\n\n\n\n<p>Once the installation is completed, you can set version 10 as default by running:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">nvm alias default 10<\/pre>\n\n\n\n<p>If you need the exact path to the node binary, you can use this command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">nvm which VERSION<\/pre>\n\n\n\n<p>(swap out <code>VERSION<\/code> for the version you wish to check, for example <code>10<\/code>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Via cPanel To get started with Node.js and creating a new application, begin by logging on to cPanel. Then, click the icon Setup Node.js App under Software. Once there, you can see all your existing Node.js applications, and also create a new one: After clicking the Create application button, you&#8230;<\/p>\n","protected":false},"author":12,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"ht-kb-category":[213],"ht-kb-tag":[],"class_list":["post-13785","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-developer-tools"],"acf":[],"_links":{"self":[{"href":"https:\/\/supporttest.hemsida.eu\/en\/wp-json\/wp\/v2\/ht-kb\/13785","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/supporttest.hemsida.eu\/en\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/supporttest.hemsida.eu\/en\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/supporttest.hemsida.eu\/en\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/supporttest.hemsida.eu\/en\/wp-json\/wp\/v2\/comments?post=13785"}],"version-history":[{"count":32,"href":"https:\/\/supporttest.hemsida.eu\/en\/wp-json\/wp\/v2\/ht-kb\/13785\/revisions"}],"predecessor-version":[{"id":18789,"href":"https:\/\/supporttest.hemsida.eu\/en\/wp-json\/wp\/v2\/ht-kb\/13785\/revisions\/18789"}],"wp:attachment":[{"href":"https:\/\/supporttest.hemsida.eu\/en\/wp-json\/wp\/v2\/media?parent=13785"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/supporttest.hemsida.eu\/en\/wp-json\/wp\/v2\/ht-kb-category?post=13785"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/supporttest.hemsida.eu\/en\/wp-json\/wp\/v2\/ht-kb-tag?post=13785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}