Nest.Js: import files from outside project folder

Viewed 4854

I started using Nest.Js and I created a Full Stack App with this structure:

app structure

api: nestjs app
client: frontend app
models: shared models (interfaces only) between back and front

So I set alias path in tsconfig.json inside api folder to let it import shortly: import { User } from 'models/user.model'

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "incremental": true, 
    "outDir": "./dist",
    "baseUrl": "./",
    "paths": {
      "models/*": ["../models/*"]
    }
  }
}

The problem is that typescript is compiling and changing the root structure under dist folder and nest cli is not finding main.js file to start up the application.

dist folder

Is there a way to move models folder and preserve nestjs structure? Or maybe change nestjs config to start the app on api/src/main.js?

1 Answers

This is something typescript does when there is code outside of the root src directory, to make sure that after compilation the same paths can be used. You can modify the nest-cli.json and add an entryFile property to tell Nest where the main file is. Also, consider something like Nx to help with creating fullstack monorepos.

Related