Search code examples
node.jsdeploymentserversystemdsystemctl

NodeJS server deploy working directory error


I have my NestJS application on my Ubuntu server.

Here is the .service file:

[Unit]
Description=My app

[Service]
ExecStart=/usr/bin/node /opt/apps/treden/dist/main.js
WorkingDirectory=/opt/apps/treden/dist
Restart=always
User=nobody
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production


[Install]
WantedBy=multi-user.target

the app launches itself, but the problem is, that it cannot find the files in subfolders

my app has the following structure:

/opt/apps/treden
├── dist
│   ├── app.module.d.ts
│   ├── app.module.js
│   ├── app.module.js.map
│   ├── auth
│   │   ├── auth.module.d.ts
│   │   ├── auth.module.js
│   │   ├── auth.module.js.map
│   │   ├── auth.service.d.ts
│   │   ├── auth.service.js
│   │   ├── auth.service.js.map
│   │   ├── local-auth.guard.d.ts
│   │   ├── local-auth.guard.js
│   │   ├── local-auth.guard.js.map
│   │   ├── local.strategy.d.ts
│   │   ├── local.strategy.js
│   │   └── local.strategy.js.map
│   ├── config
│   │   ├── application.config.d.ts
│   │   ├── application.config.js
│   │   ├── application.config.js.map
│   │   ├── database.config.d.ts
│   │   ├── database.config.js
│   │   └── database.config.js.map
│   ├── main.d.ts
│   ├── main.js
│   ├── main.js.map
│   ├── tsconfig.build.tsbuildinfo
│   └── users
│       ├── entities
│       │   ├── user.entity.d.ts
│       │   ├── user.entity.js
│       │   └── user.entity.js.map
│       ├── users.controller.d.ts
│       ├── users.controller.js
│       ├── users.controller.js.map
│       ├── users.module.d.ts
│       ├── users.module.js
│       ├── users.module.js.map
│       ├── users.service.d.ts
│       ├── users.service.js
│       └── users.service.js.map
├── nest-cli.json
├── package.json
├── package-lock.json
├── README.md
├── src
│   ├── app.module.ts
│   ├── auth
│   │   ├── auth.module.ts
│   │   ├── auth.service.spec.ts
│   │   ├── auth.service.ts
│   │   ├── local-auth.guard.ts
│   │   └── local.strategy.ts
│   ├── config
│   │   ├── application.config.ts
│   │   └── database.config.ts
│   ├── main.ts
│   └── users
│       ├── entities
│       │   └── user.entity.ts
│       ├── users.controller.spec.ts
│       ├── users.controller.ts
│       ├── users.module.ts
│       ├── users.service.spec.ts
│       └── users.service.ts
├── test
│   ├── app.e2e-spec.ts
│   └── jest-e2e.json
├── tsconfig.build.json
├── tsconfig.json
└── yarn.lock

11 directories, 61 files

the error from server is:

Started My app.
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [NestFactory] Starting Nest application...
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [InstanceLoader] TypeOrmModule dependencies initialized +30ms     [Nest] 9900   - 11/16/2020, 11:03:53 PM   [InstanceLoader] ConfigHostModule dependencies initialized +2ms   [Nest] 9900   - 11/16/2020, 11:03:53 PM   [InstanceLoader] AppModule dependencies initialized +0ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [InstanceLoader] TypeOrmCoreModule dependencies initialized +128ms[Nest] 9900   - 11/16/2020, 11:03:53 PM   [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [InstanceLoader] UsersModule dependencies initialized +2ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [RoutesResolver] AppController {}: +166ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [RouterExplorer] Mapped {, GET} route +5ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [RoutesResolver] UsersController {/users}: +0ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [RouterExplorer] Mapped {/users/:id, GET} route +2ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [RouterExplorer] Mapped {/users, GET} route +1ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [RouterExplorer] Mapped {/users, POST} route +1ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [RouterExplorer] Mapped {/users/bulk, POST} route +0ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [RouterExplorer] Mapped {/users/:id, PATCH} route +1ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [RouterExplorer] Mapped {/users/:id, PUT} route +1ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [RouterExplorer] Mapped {/users/:id, DELETE} route +1ms
[Nest] 9900   - 11/16/2020, 11:03:53 PM   [NestApplication] Nest application successfully started +3ms
Stopping My app...
Stopped My app.
Started My app.
 internal/modules/cjs/loader.js:883
    throw err;
       ^
        Error: Cannot find module './auth.controller'
         Require stack:
          - /opt/apps/treden/dist/auth/auth.module.js
          -  - /opt/apps/treden/dist/app.module.js
 - /opt/apps/treden/dist/main.js
 -      at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
 -           at Function.Module._load (internal/modules/cjs/loader.js:725:27)
 -                at Module.require (internal/modules/cjs/loader.js:952:19)
 -                     at require (internal/modules/cjs/helpers.js:88:18)
 -                          at Object.<anonymous> (/opt/apps/treden/dist/auth/auth.module.js:15:27)
 -                               at Module._compile (internal/modules/cjs/loader.js:1063:30)
 -                                    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
 -                                         at Module.load (internal/modules/cjs/loader.js:928:32)
 -                                              at Function.Module._load (internal/modules/cjs/loader.js:769:14)
 -                                                   at Module.require (internal/modules/cjs/loader.js:952:19) {
 -                                                      code: 'MODULE_NOT_FOUND',
 -                                                         requireStack: [
 -                                                              '/opt/apps/treden/dist/auth/auth.module.js',
 -                                                                   '/opt/apps/treden/dist/app.module.js',
 -                                                                        '/opt/apps/treden/dist/main.js'
 -                                                                           ]
 -                                                                            }
 -                                                                            treden.service: Main process exited, code=exited, status=1/FAILURE
 -                                                                            treden.service: Failed with result 'exit-code'.

I understand what the problem is, but I need a proper solving of this problem. I tried to remove WorkingDirectory parameter is .service file, but that didn't help

Much thanks.


Solution

  • There were no auth.controller.ts file in folder (I accidentally deleted it)