Silk Web Hosting: Node.js

Modified

Long-term support (LTS) versions of Node.js and utilities are available. By convention, these are the even-numbered Node.js releases.

Choosing a version

You can choose among multiple versions of Node.js:

Node.js version Command-line path Sunset Date
16.x (default) /opt/nodejs/v16/bin/node September 11, 2023

If you need to switch Node.js versions, you can do so by creating or editing your site’s .silk.ini file (for example ~/www-root/.silk.ini) and adding the version to the [nodejs] section:

    [nodejs]
    version = 16

Valid version choices are default, any of the versions listed in the table above, or newest (which provides the newest available on the system). From the command-line, changes will take effect when you next log in. Web applications will need to be restarted.

Packages

yarn and npm are available for installing packages.

If you need to install something globally rather than inside your application’s directory, you can change the default global directory to a location you own.

For yarn, edit your ~/.yarnrc.yml file to include a line similar to:

    globalFolder: "./.yarn/global"

For npm, you can follow these directions.

Web applications

Web applications use NGINX Unit as an application server. This will automatically take care of starting up your code when someone tries to access it with a web browser; you will not launch it directly with node. For this to work, do the following:

  1. Edit the .silk.ini file in your site root. Add a section similar to the following.

    [app]
    type = nodejs
    document-root = public
    startup-script = app.js
    cmdline-args = --arg1 --arg2=blah
    env.MYVAR = some value

    The .silk.ini section name should be either “app” or any descriptive name you wish for your application that starts with “app: “. This could be useful if you wish to configure multiple apps.

    root is an optional directory under your site root containing all your application files.

    document-root is the directory under your app root in which static files will be located.

    startup-script is the name of the script launching your application, which should have executable permissions (chmod u+x app.js).

    cmdline-args is an optional list of command-line arguments passed to the startup script.

    Any line beginning with env. will set an environment variable available within your application.

  2. If this is the first web application in your site, a new app server will be started for your account within ten minutes.

  3. Make the following tweaks to your application:

    1. From inside your site root directory, run npm link unit-http.

    2. Since your app is launched as a script by the app server, make sure your startup script is executable (chmod u+x app.js) and begins with a script “shebang” line pointing to your desired version of Node.js, such as #!/opt/nodejs/v10/bin/node.

    3. Your app will use the unit-http module instead of http for receiving and responding to requests. Replace the line that requires http with one for unit-http, such as:

      var http = require('unit-http');
    4. Your app will not directly listen on a network port, so make sure your listen() line does not referene one. You can just say something such as:

      server.listen();
  4. Load your application with silk app <hostname> load. If you have multiple sites and/or multiple apps, <hostname> is followed by the URI path of your app, such as dev.mynetid.w3.uvm.edu/app2. If you see an error, check the app server log for details.

Reloading applications

An application needs to be restarted for code changes to take effect. You can do this the same way you originally loaded your application:

[myusername@silk21 ~]$ silk app <hostname> load

Console logs

App server logs are available at /usr/lib/unit-user-<mynetid>/unit.log.