DEV Community

No Longer Coder
No Longer Coder

Posted on

ML in JavaScript

Learning machine learning in Python might be reasonable if you are first in machine learning. Python's eco-environment is plentiful, and the community is widespread.

Building machine learning models in JavaScript is beneficial, especially in mono-language application systems like React.js and Node.js run in JavaScript; no need to make another backend server in Python.

"TensorFlow.js" is a popular library for machine learning in JavaScript, able to use ML directly in the browser or Node.js.

Step 1 - Set Up a Node

Initialize the Project

$ npm init -y
Enter fullscreen mode Exit fullscreen mode

Configure the TypeScript Compiler

npm install --save-dev typescript
Enter fullscreen mode Exit fullscreen mode

Install ambient Node.js types for TypeScript

npm install @types/node --save-dev
Enter fullscreen mode Exit fullscreen mode

Install the pure JavaScript version

npm install @tensorflow/tfjs
Enter fullscreen mode Exit fullscreen mode

Create a tsconfig.json file

touch tsconfig.json
Enter fullscreen mode Exit fullscreen mode

Add following JSON to tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es5",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "sourceMap": true,
    "rootDir": "src",
    "outDir": "dist",
    "allowJs": true,
    "strict": true,
    "noImplicitAny": true,
    "skipLibCheck": true,
  },
  "lib": ["es6"]
}
Enter fullscreen mode Exit fullscreen mode

Create a Minimal TypeScript Express Server

npm install --save express
npm install -save-dev @types/express
Enter fullscreen mode Exit fullscreen mode

Install Environment Variables

npm install dotenv --save
Enter fullscreen mode Exit fullscreen mode

Create a file called .env

NODE_ENV = development
PORT = 3000
Enter fullscreen mode Exit fullscreen mode

create an src folder in the root directory

mkdir src
Enter fullscreen mode Exit fullscreen mode

create a TypeScript file named index.ts

touch src/index.ts
Enter fullscreen mode Exit fullscreen mode

Open up the index.ts file and paste in the following code snippet

import express from 'express';
const app = express();

if (process.env.NODE_ENV !== 'production') {
  require('dotenv').config();
}

const port = parseInt(process.env.PORT) || 3000;

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  return console.log(`Express is listening at http://localhost:${port}`);
});
Enter fullscreen mode Exit fullscreen mode

Install nodemon

npm install --save-dev ts-node nodemon
Enter fullscreen mode Exit fullscreen mode

Add a nodemon.json config

{
  "watch": ["src"],
  "ext": ".ts,.js",
  "ignore": [],
  "exec": "ts-node ./src/index.ts"
}
Enter fullscreen mode Exit fullscreen mode

Add a script to package.json

"start:dev": "nodemon",
Enter fullscreen mode Exit fullscreen mode

Run the server

npm run start:dev
Enter fullscreen mode Exit fullscreen mode

Install rimraf, a cross-platform tool

npm install --save-dev rimraf
Enter fullscreen mode Exit fullscreen mode

Add a script to package.json

"build": "rimraf ./build && tsc",
"start": "npm run build && node build/index.js"
Enter fullscreen mode Exit fullscreen mode

Install ESLint

npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
Enter fullscreen mode Exit fullscreen mode

Create a .eslintrc file

touch .eslintrc
Enter fullscreen mode Exit fullscreen mode

Add a JSON to .eslintrc

  "root": true,
  "parser": "@typescript-eslint/parser",
  "plugins": [
    "@typescript-eslint"
  ],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended"
  ],
  "rules": { 
    "no-console": 0
  }
}
Enter fullscreen mode Exit fullscreen mode

Create an .eslintignore

touch .eslintignore
Enter fullscreen mode Exit fullscreen mode

Add the thing to be ignored to .eslintignore

node_modules
dist
Enter fullscreen mode Exit fullscreen mode

Add a script to the package.json

"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix"
Enter fullscreen mode Exit fullscreen mode

Run lint

npm run lint
Enter fullscreen mode Exit fullscreen mode

Top comments (0)