Home > Software design >  problem in building typescript application with imports and type module
problem in building typescript application with imports and type module

Time:11-19

I've actually read every stackoverflow post related to my problem, but I can't solve it, every attempt brings me a new problem.

This is the structure: enter image description here

package.json:

{
  ..
  "type": "module",
  "main": "index.ts",
  "scripts": {
    "dev": "ts-node-esm ./src/index.ts",
    "start": "ts-node-esm ./src/index.ts",
    "build": "tsc --build",
    "clean": "tsc --build --clean"
  },
...
}

tsconfig.json:

{
  "compilerOptions": {
      "module": "esnext",
       "noImplicitAny": false,
       "sourceMap": true,
       "resolveJsonModule": true,
       "moduleResolution": "Node",
       "allowSyntheticDefaultImports": true,
       "esModuleInterop": true,
       "outDir": "dist",
   },
   "include": [
    "src/*"
  ]
}

index.ts imports myModule.ts like this:

...
import request from 'request';
import {connection} from "../config/db.js";
import { MyModule } from '../assets/ts/myModule.js';

I start the development app with this command and everything works fine::

npm run dev

> [email protected] dev
> ts-node-esm ./src/index.ts

now i want to build the application for production. i wanted to use pm2 but i had several problems.

what is the best approach and the most performing solution to put my application into production?

CodePudding user response:

I already had an experience with rolling up a project and found out about this fantastic library you can use in this case! RollupJS

CodePudding user response:

It appears that it is currently possible to use the ts-node-em command with the --transpile-only flag without compromising performance. So it can be used in production:

Solution:

ts-node-esm --transpile-only ./src/index.ts
  • Related