Zubashev Stepan's blog

How to migrate Javascript webpack configuration to Typescript

It turned out that the newest versions (at least 5+) of webpack support Typescript out of the box. So the algorithm is next:

  • Create tsconfig.json at the root level. Content:
    {
      "compilerOptions": {
        "module": "CommonJS",
        "target": "ES5",
        "esModuleInterop": true,
        "checkJs": false,
        "strict": true,
      }
    }
    
  • Rename all *.js files to *.ts
  • Type all of them:
    • no more ugly require, use import.
    • webpack package has typings out of the box.
      • You may find these types useful: ConfigurationRuleSetRule
      • To enable devServer write this: 
        interface Configuration extends WebpackConfiguration {
          devServer?: WebpackDevServerConfiguration;
        }
        
    • Some of the popular plugins have types too.
    • Some of them don’t have types at all:
      • Create a *.d.ts file
      • Put there something like this:
        declare module 'postcss-assets' {
          export default function postcssAssets(opts: {
            basePath: string;
            relative: boolean;
          }): unknown;
        }
        
  • Make sure your webpack.config.ts file is placed at the root level. I mean exactly at the same spot where node_modules is. Otherwise, you won’t be able to build it. No compilerOptions helped me. 
  • Run webpack. It should work.

How to make a simple system.d service for a node.js server

Put this content:

[Unit]
Description={name}

[Service]
Type=simple
User={user}
ExecStart=/usr/bin/node {full-path-to-script}.js

[Install]
WantedBy=multi-user.target

… somewhere as {name}.service, where:

  • {name} is the name of the service
  • {user} is the name of the user to run the script (optional)

… then:

  • run: this sudo ln -s /{full_path}/{name}.service /lib/systemd/system/{name}.service 
  • then this: sudo systemctl daemon-reload
  • then this: sudo systemctl enable {name}.service
  • and finally this: sudo systemctl start {name}

How does it work?

  • It starts the service on boot (see WantedBy section).
  • Using ExecStart command. Important: we specify the full path to node
  • SystemD remembers the PID of the new process and considers the service is ongoing until the process is died.
  • So any subsequent systemctrl start {name} won’t do anything if the previous process is alive.
  • This behavior is determined by Type=Simple