Skip to content

Warnings + error messages with hot reload + yarn v2 #5928

@brandon-leapyear

Description

@brandon-leapyear

Bug Report

Adding hot reload with yarn 2 in a new nest project runs the nest server successfully (i.e. curl localhost:3000 returns "Hello world"), but the output shows a deprecation warning and error messages related to requiring uninstalled packages.

Repro:

  1. nest new
  2. yarn set version berry && yarn
  3. Add hot reload
    1. Follow instructions per https://docs.nestjs.com/recipes/hot-reload
    2. Replace start-server-webpack-plugin with run-script-webpack-plugin (Please upgrade to Webpack 5 ericclemmons/start-server-webpack-plugin#40 (comment))
    3. Fix WatchIgnorePlugin (docs(hot-reload): update hot-reload.md docs.nestjs.com#1568)
    4. Run yarn add -D webpack@5.9.0 (Move webpack to peerDependency for Yarn v2 + hot reload nest-cli#992)
  4. yarn start:dev

Current behavior

You'll see one deprecation warning:

(node:35944) [DEP_WEBPACK_WATCH_WITHOUT_CALLBACK] DeprecationWarning: A 'callback' argument need to be provided to the 'webpack(options, callback)' function when the 'watch' option is set. There is no way to handle the 'watch' option without a callback.
(Use `node --trace-deprecation ...` to show where the warning was created)

and errors like

ERROR in ./.yarn/unplugged/@nestjs-core-virtual-330e9c6eb9/node_modules/@nestjs/core/nest-application.js 16:102-145
Module not found: Error: @nestjs/core tried to access @nestjs/websockets (a peer dependency) but it isn't provided by its ancestors; this makes the require call ambiguous and unsound.

Required package: @nestjs/websockets (via "@nestjs/websockets/socket-module")
Required by: @nestjs/core@virtual:6847a7e26c6f65e9a07a05ac6fd51a817f487ca65a3f4eb6074a6e87e9e5a9b8d604589ccca3616c3ade1ca80766e0dab48ed3499f9be6e35afa8028ddc2857f#npm:7.6.1 (via /Users/bchinn/Desktop/yarn2/nest-hrm-yarn2/.yarn/unplugged/@nestjs-core-virtual-330e9c6eb9/node_modules/@nestjs/core/)
Ancestor breaking the chain: @nestjs/platform-express@virtual:6847a7e26c6f65e9a07a05ac6fd51a817f487ca65a3f4eb6074a6e87e9e5a9b8d604589ccca3616c3ade1ca80766e0dab48ed3499f9be6e35afa8028ddc2857f#npm:7.6.1
Ancestor breaking the chain: @nestjs/testing@virtual:6847a7e26c6f65e9a07a05ac6fd51a817f487ca65a3f4eb6074a6e87e9e5a9b8d604589ccca3616c3ade1ca80766e0dab48ed3499f9be6e35afa8028ddc2857f#npm:7.6.1
Ancestor breaking the chain: foo@workspace:.


 @ ./.yarn/unplugged/@nestjs-core-virtual-330e9c6eb9/node_modules/@nestjs/core/index.js 25:21-50
 @ ./src/main.ts 3:15-38

Now #5477 correctly added these packages to peerDependencies and specified them as optional with peerDependenciesMeta, but I think the way NestJS tries to load optional dependencies (i.e. try-catch require) triggers an error message with Yarn v2.

const {
MicroservicesModule,
} = optionalRequire('@nestjs/microservices/microservices-module', () =>
require('@nestjs/microservices/microservices-module'),
);

export function optionalRequire(packageName: string, loaderFn?: Function) {
try {
return loaderFn ? loaderFn() : require(packageName);
} catch (e) {
return {};
}
}

Input Code

See repro above

Expected behavior

yarn start:dev should not show any deprecation warnings or error messages, the way it displays without the "Add hot reload" step in the repro.

Possible Solution

Some possible solutions:

  1. Only require optional dependencies when they're actually used
  2. Implement optionalRequire differently, possibly using the PnP API to check if a package is installed before trying to require it

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