Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
brandon-leapyear opened this issue Dec 10, 2020 · 1 comment
Closed
Labels
needs triage This issue has not been looked into

Comments

@brandon-leapyear
Copy link

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:

@brandon-leapyear brandon-leapyear added the needs triage This issue has not been looked into label Dec 10, 2020
@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Dec 10, 2020

I don't think this is related to NestJS. Here's the setViewEngine method reference: https://github.com/nestjs/nest/blob/master/packages/platform-express/adapters/express-adapter.ts#L95 If you open up this link <, you'll notice that this method contains a single line of code, simply delegating the entire "view-engine" wiring up logic to Express. If you can't reproduce it using just express, I'd recommend reporting this issue in Yarn repo.

leeklife pushed a commit to leeklife/nest that referenced this issue Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

2 participants