Skip to content

Rendering with express view engine fails with Yarn v2 workspaces #5909

Closed
@brandon-leapyear

Description

@brandon-leapyear

Bug Report

We're trying to upgrade to Yarn v2 in our project, which uses NestJS in a Yarn workspace. Yarn v2 adds Plug-n-Play which enforces dependencies explicitly list their dependencies. When running our server and calling an endpoint that renders a pug template, we get:

express tried to access pug, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Required package: pug (via "pug")
Required by: express@npm:4.17.1 (via /Users/bchinn/Desktop/yarn2/express-pug-error/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/)

Require stack:
- /Users/bchinn/Desktop/yarn2/express-pug-error/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/view.js
- /Users/bchinn/Desktop/yarn2/express-pug-error/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/application.js
- /Users/bchinn/Desktop/yarn2/express-pug-error/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/express.js
- /Users/bchinn/Desktop/yarn2/express-pug-error/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/index.js
- /Users/bchinn/Desktop/yarn2/express-pug-error/.yarn/$$virtual/@nestjs-platform-express-virtual-71cd35a84f/0/cache/@nestjs-platform-express-npm-7.5.5-1f245a935a-8af8bba8dc.zip/node_modules/@nestjs/platform-express/adapters/express-adapter.js
- /Users/bchinn/Desktop/yarn2/express-pug-error/.yarn/$$virtual/@nestjs-platform-express-virtual-71cd35a84f/0/cache/@nestjs-platform-express-npm-7.5.5-1f245a935a-8af8bba8dc.zip/node_modules/@nestjs/platform-express/adapters/index.js
- /Users/bchinn/Desktop/yarn2/express-pug-error/.yarn/$$virtual/@nestjs-platform-express-virtual-71cd35a84f/0/cache/@nestjs-platform-express-npm-7.5.5-1f245a935a-8af8bba8dc.zip/node_modules/@nestjs/platform-express/index.js
- /Users/bchinn/Desktop/yarn2/express-pug-error/.yarn/unplugged/@nestjs-core-virtual-74f5478e67/node_modules/@nestjs/core/nest-factory.js
- /Users/bchinn/Desktop/yarn2/express-pug-error/.yarn/unplugged/@nestjs-core-virtual-74f5478e67/node_modules/@nestjs/core/index.js
- /Users/bchinn/Desktop/yarn2/express-pug-error/foo/dist/main.js +9467ms

I couldn't repro this in a repo using just express, which is why I'm writing this in the NestJS project.

Repro instructions

mkdir express-pug-error && cd express-pug-error
yarn set version berry
yarn init

# having trouble bootstrapping with yarn v2. just use npm for now
npm install @nestjs/cli
node_modules/.bin/nest new foo
# "Which package manager would you <3 to use?" => yarn

Update package.json

 {
   "name": "express-pug-error",
+  "private": true,
+  "workspaces": ["foo"]
 }
yarn workspace foo add @nestjs/platform-express

Add to .yarnrc.yml (since #5477 isn't uploaded to npm yet)

packageExtensions:
  '@nestjs/testing@*':
    peerDependencies:
      '@nestjs/platform-express': '*'
  '@nestjs/core@*':
    peerDependencies:
      '@nestjs/microservices': '*'
      '@nestjs/platform-express': '*'
      '@nestjs/websockets': '*'

Make the following additions:

diff --git a/foo/src/app.controller.ts b/foo/src/app.controller.ts
index cce879e..23ffd92 100644
--- a/foo/src/app.controller.ts
+++ b/foo/src/app.controller.ts
@@ -1,4 +1,4 @@
-import { Controller, Get } from '@nestjs/common';
+import { Controller, Get, Render } from '@nestjs/common';
 import { AppService } from './app.service';
 
 @Controller()
@@ -6,7 +6,8 @@ export class AppController {
   constructor(private readonly appService: AppService) {}
 
   @Get()
+  @Render('index')
-  getHello(): string {
-    return this.appService.getHello();
+  getHello() {
+    return {}
   }
 }
diff --git a/foo/src/main.ts b/foo/src/main.ts
index 13cad38..9c28104 100644
--- a/foo/src/main.ts
+++ b/foo/src/main.ts
@@ -1,8 +1,10 @@
 import { NestFactory } from '@nestjs/core';
+import { NestExpressApplication } from '@nestjs/platform-express'
 import { AppModule } from './app.module';
 
 async function bootstrap() {
-  const app = await NestFactory.create(AppModule);
+  const app = await NestFactory.create<NestExpressApplication>(AppModule);
+  app.setViewEngine('pug')
   await app.listen(3000);
 }
 bootstrap();
yarn
yarn workspace foo start
curl localhost:3000

Input Code

const your = (code) => here;

Expected behavior

Possible Solution

Environment


Nest version: X.Y.Z

 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue has not been looked into

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions