thisismydesign / nestjs-starter

NestJS MVC boilerplate for rapid development with battle-tested standards.
473 stars 80 forks source link

Numerous issues out of box: (0 , next_1.default) is not a function, Cannot read properties of undefined (reading 'render') #430

Closed stevenlafl closed 1 year ago

stevenlafl commented 1 year ago

Followed exact "usage" instructions. Can't get it to run.

This was docker-compose up

web-1  | [2/4] Fetching packages...
web-1  | error @angular-devkit/schematics@16.0.1: The engine "node" is incompatible with this module. Expected version "^16.14.0 || >=18.10.0". Got "18.8.0"
web-1  | error Found incompatible module.
web-1  | info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

So I ran

yarn install

on the host machine. Uh, no problems. Weird, very weird. Tried docker-compose up again, got:

web-1  | TypeError: (0 , next_1.default) is not a function
web-1  |     at ViewService.onModuleInit (/app/src/server/view/view.service.ts:14:33)
web-1  |     at MapIterator.iteratee (/app/node_modules/@nestjs/core/hooks/on-module-init.hook.js:22:43)
web-1  |     at MapIterator.next (/app/node_modules/iterare/src/map.ts:9:39)
web-1  |     at IteratorWithOperators.next (/app/node_modules/iterare/src/iterate.ts:19:28)
web-1  |     at Function.from (<anonymous>)
web-1  |     at IteratorWithOperators.toArray (/app/node_modules/iterare/src/iterate.ts:227:22)
web-1  |     at callOperator (/app/node_modules/@nestjs/core/hooks/on-module-init.hook.js:23:10)
web-1  |     at callModuleInitHook (/app/node_modules/@nestjs/core/hooks/on-module-init.hook.js:43:23)
web-1  |     at NestApplication.callInitHook (/app/node_modules/@nestjs/core/nest-application-context.js:224:50)
web-1  |     at NestApplication.init (/app/node_modules/@nestjs/core/nest-application.js:98:9)

Okay, obviously it can't use the server method mentioned here, which is what you're doing in the code: https://nextjs.org/docs/pages/building-your-application/configuring/custom-server

Nonetheless, I go visit /home and I see:

web-1  | TypeError: Cannot read properties of undefined (reading 'render')
web-1  |     at ViewController.showHome (/app/dist/src/server/view/view.controller.js:35:13)
web-1  |     at /app/node_modules/@nestjs/core/router/router-execution-context.js:38:29
web-1  |     at processTicksAndRejections (node:internal/process/task_queues:95:5)
web-1  |     at /app/node_modules/@nestjs/core/router/router-execution-context.js:46:28
web-1  |     at /app/node_modules/@nestjs/core/router/router-proxy.js:9:17
stevenlafl commented 1 year ago

Okay, it's a typescript issue. I fixed it with this:

view.service.ts

import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as createServer from 'next';
import { NextServer } from 'next/dist/server/next';

@Injectable()
export class ViewService implements OnModuleInit {
  private server: NextServer;

  constructor(private configService: ConfigService) {}

  async onModuleInit(): Promise<void> {
    try {
      // @ts-ignore
      this.server = createServer({
        dev: this.configService.get<string>('NODE_ENV') !== 'production',
        dir: './src/client',
      });
      await this.server.prepare();
    } catch (error) {
      console.error(error);
    }
  }

  getNextServer(): NextServer {
    return this.server;
  }
}
thisismydesign commented 1 year ago

You shouldn't mix docker and native setup.

A clean docker-compose up works fine on my system and worked in the CI last time as well (although logs are no longer available)). What is your native OS?

stevenlafl commented 1 year ago

Debian Linux

I cloned, and ran docker-compose up. No node_modules, etc. That's how I got the yarn problem at the start.

thisismydesign commented 1 year ago

Thanks! I fixed the docker issue by updating the node version in the dockerfiles: https://github.com/thisismydesign/nestjs-starter/pull/431 You can git pull the fix.

The other issue I think is related to mixing native and docker setup. I recommend the docker setup but you can also do yarn install && yarn start:dev natively, it should work. Just don't mix the two (e.g. yarn install && docker-compose up).

I'm closing for now but feel free to ping me if you run into further issues.