Nx: NX workspace NestJS 6.0 with Middleware causes controllers to not respond.

Created on 12 Apr 2019  路  3Comments  路  Source: nrwl/nx

Expected Behavior

Can run NestJS 6.0 as part of Nx workspace, and use Middleware functionality

Current Behavior

When upgrading NestJS to 6.0 in NX workspace (7.6 or 7.8), when running in development mode adding Middleware to NestJS app either does not work (one controller) or causes controllers not to respond (multiple controllers)

Failure Information (for bugs)

Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Created NX Workspace (using 7.6 NX CLI After creation of workspace, created a Nest app via 'ng g'
  2. Upgrated the versions of NestJS to 6.0.0 in package.json
  3. Added a middleware logger implementation to the appmodule in NestJs app.
  4. Run 'ng serve' at command line.
  5. Tested endpoint (localhost:3333/api) - middleware logger doesn't work, but the default api endpoint still returns response.
  6. Stop application.
  7. Added another controller to NestJS app, added to appmodule.
  8. Run 'ng serve' at command line.
  9. Tested endpoints (localhost:3333/api and localhost:3333/api/test) and neither respond. If you remove the registration of middleware and re-test both endpoints will work.

Repo used for these steps (it is in step 7-8 config) is here: https://github.com/kmkatsma/nx-nestjs6-middleware-issue

Context

Please provide any relevant information about your setup:

A minimal reproduce scenario using allows us to quickly confirm a bug (or point out coding problem) as well as confirm that we are fixing the right problem.

Failure Logs

Please include any relevant log snippets or files here.

Other

I know currently default Nx workspace uses NestJS 5.5, just hoping to be able to use 6.0 version of Nest in Nx workspace (really wanting the different injection scopes). I'm assuming this will need to wait for an update for Nx to use NestJS 6.0, just wanted to make sure this was out there to make sure this middleware scenario works after update.

Most helpful comment

Ok, this is best kind of issue - one that originator closes! I guess I just needed more time.

The solution was in fact to override the tsconfig for the NestJS app. The final version I am using looks like this below. It was also important to set the _baseUrl_ to the base of the monorepo, so that @ barrel files can be found. App is up and running, with middleware.

{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"],
"sourceMap": true,
"declaration": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": false,
"target": "es6",
"module": "commonjs",
"typeRoots": ["node_modules/@types"],
"lib": ["es6", "es2015", "dom"],
"noImplicitAny": false,
"noLib": false,
"removeComments": true,
"allowSyntheticDefaultImports": true,
_"baseUrl": "./../../",_
},
"include": ["*/.ts"]
}

All 3 comments

I actually reverted to 5.5 version of NestJS and still had middleware issue. So not sure if people using NX with Nest are just running into and solving on their own.

I found that I was able to get around this particular issue by changing the global tsconfig compiler options to match those used by default in NestJS projects. A number of these settings are different than the default tsconfig options for NX workspace.

These are here:

"compilerOptions": {
"sourceMap": true,
"declaration": true,
"noImplicitAny": false,
"noLib": false,
"removeComments": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es6",
"module": "commonjs",
"typeRoots": ["node_modules/@types"],
"lib": ["es6", "es2015", "dom"],
"baseUrl": ".",

I'm thinking I will see if it's possible to override these for just the NestJS app itself, and will resolve issue...

Ok, this is best kind of issue - one that originator closes! I guess I just needed more time.

The solution was in fact to override the tsconfig for the NestJS app. The final version I am using looks like this below. It was also important to set the _baseUrl_ to the base of the monorepo, so that @ barrel files can be found. App is up and running, with middleware.

{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"],
"sourceMap": true,
"declaration": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": false,
"target": "es6",
"module": "commonjs",
"typeRoots": ["node_modules/@types"],
"lib": ["es6", "es2015", "dom"],
"noImplicitAny": false,
"noLib": false,
"removeComments": true,
"allowSyntheticDefaultImports": true,
_"baseUrl": "./../../",_
},
"include": ["*/.ts"]
}

Thanks, faced exactly the same problem, changing the tsconfig.json in the nest folder helped.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SWGeekPD picture SWGeekPD  路  3Comments

vimalraj-a picture vimalraj-a  路  3Comments

Svancara picture Svancara  路  3Comments

dereklin picture dereklin  路  3Comments

danieldanielecki picture danieldanielecki  路  3Comments