Tag: nodejs

NodeJS Best Practices – Resources

nodejs-best-practices-1-728

This is a list of resources from another websites and blogs where you can find really useful information how to write better code in NodeJS including NodeJS design patterns.

I would also mention here paid video classes on CodeSchool which I have a have attended and found very useful:

How to setup StandardJS for Visual Studio Code – NodeJS and TypeScript

About

          visual_studio_code_icon_1x

This is a tutorial how to set StandardJS with Visual Studio Code to work with JavaScript and TypeScript.

StandardJS for JavaScript/NodeJS

  1. Open VS Code and install StandardJS extension. Press CTRL+P and type:
    ext install standardjs
  2. Disable default JavaScript validation in VS Code so standard can take that role. Go to menu File/Preferences/Settings or just press CTRL+, and add to settings.json following line:
    "javascript.validate.enable": false
  3. Open a terminal and install standard with the command:
    $ npm i -g --dev-save standard
  4. If you want to ignore specific files you can do that like in the following example where we added ignore for checking and formatting Angular2 bundle files. Add the following to package.json:
    "standard": {
      "ignore": [
        "*.bundle.js"
      ]
    }
  5. Restart VS Code
  6. Now you probably have a lot of unformatted code in a project, you can fix it by typing following command. After that, all .js files will be fixed against the standard you will need some parts to fix manually since this will fix the only indentation and remove semicolons.
    $ standard --fix

Additional – Add snazzy

  1. For better error reporting you can install snazzy:
    $ npm i -g --dev-save snazzy
  2. Usage in terminal:
    $ standard | snazzy
  3. Or you can create test task in the package.json with adding the following:
    "scripts": {
        ...
        "test": "standard | snazzy"
      }
  4. Like all other task scripts, you can run it with:
    $ npm run test

Standard for TypeScript

  1. Open VS Code and install TSLint extension. Press CTRL+P and type:
    ext install tslint
  2. Open a terminal and install TSLint configuration for standard rules with:
    $ npm i --dev-save tslint tslint-config-standard
  3. Create tslint.json file in a root of your project and insert following:
    {
      "extends": "tslint-config-standard"
    }
  4. To fix automatic formatting in VS Code for .ts files you need to add three more lines in settings.json:
    "editor.tabSize": 2,
    "typescript.format.insertSpaceBeforeFunctionParenthesis": true,
    "editor.detectIndentation": false
  5. Restart VS Code
  6. To fix existing files to follow standard rules you need to install global tslint and typescript:
    $ npm i -g tslint typescript
  7. And after that to run the command:
    $ tslint -c tslint.json assets/**/*.ts

Known issues

  • TypeScript constructor keyword doesn’t have space before opening brackets after code formating, which will report error by standard. Workaround is to put above constructor following line:
    // tslint:disable-next-line:space-before-function-paren
  • Automatic Imports with light bulp or CTRL+. will not follow standard rules. It will import with double quotes and semicolon at the end. Don’t know workaround for this if someone find solution please add it to comments.