oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
71.84k stars 2.56k forks source link

Optional peer dependency resolution (Nest.js) #9936

Open cosigyn opened 3 months ago

cosigyn commented 3 months ago

What version of Bun is running?

1.1.0+5903a6141

What platform is your computer?

Microsoft Windows NT 10.0.22621.0 x64

What steps can reproduce the bug?

I am in a monorepo and previously using pnpm and node. I deleted pnpm lock file and all node_modules folders and ran ‘bun i’, then went into a workspace and ran the ‘bun start’ command, I received the following error which was not present when using pnpm:

PS C:\Users\unkno\Documents\RaidRequestHubMonorepo\apps\server> bun run start
$ nest start
ERROR in ../../node_modules/@nestjs/core/nest-application.js 19:107-150
Module not found: Error: Can't resolve '@nestjs/websockets/socket-module' in 'C:\Users\unkno\Documents\RaidRequestHubMonorepo\node_modules\@nestjs\core'
 @ ../../node_modules/@nestjs/core/index.js 26:21-50
 @ ./src/main.ts 3:15-38

ERROR in ../../node_modules/@nestjs/core/nest-application.js 20:124-177
Module not found: Error: Can't resolve '@nestjs/microservices/microservices-module' in 'C:\Users\unkno\Documents\RaidRequestHubMonorepo\node_modules\@nestjs\core'
 @ ../../node_modules/@nestjs/core/index.js 26:21-50
 @ ./src/main.ts 3:15-38

ERROR in ../../node_modules/@nestjs/mapped-types/dist/type-helpers.utils.js 80:27-63
Module not found: Error: Can't resolve 'class-transformer/storage' in 'C:\Users\unkno\Documents\RaidRequestHubMonorepo\node_modules\@nestjs\mapped-types\dist'
 @ ../../node_modules/@nestjs/mapped-types/dist/index.js 23:27-58
 @ ../../node_modules/@nestjs/mapped-types/index.js 6:9-26
 @ ../../node_modules/@nestjs/swagger/dist/type-helpers/intersection-type.helper.js 4:23-54
 @ ../../node_modules/@nestjs/swagger/dist/type-helpers/index.js 17:13-50
 @ ../../node_modules/@nestjs/swagger/dist/index.js 22:13-38
 @ ../../node_modules/@nestjs/swagger/index.js 6:9-26
 @ ./src/main.ts 5:18-44

I am not using any of these packages in my app. When I installed them, I got 22 errors from modules not being resolved that were required in those 3 modules.

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

According to previous reports, this is a webpack issue and related to @nestjs/swagger package nestjscore module resolving error o.txt

cosigyn commented 3 months ago

In my case. it works with pnpm but not bun 'Additional information' case it does not work with yarn In https://github.com/NiGhTTraX/ts-monorepo/issues/61 case, it does not work with npm but it works with yarn

cosigyn commented 3 months ago

The same issue appears to occur using bun build instead of webpack:

PS C:\Users\unkno\Documents\RaidRequestHubMonorepo\apps\server> bun build ./src/main.ts --outdir ./dist
57 |         const { NestMicroservice } = (0, load_package_util_1.loadPackage)('@nestjs/microservices', 'NestFactory', () => require('@nestjs/microservices'));
                                                                                                                                     ^
error: Could not resolve: "@nestjs/microservices". Maybe you need to "bun install"?
    at C:\Users\unkno\Documents\RaidRequestHubMonorepo\node_modules\@nestjs\core\nest-factory.js:57:129

19 | const { SocketModule } = (0, optional_require_1.optionalRequire)('@nestjs/websockets/socket-module', () => require('@nestjs/websockets/socket-module'));       
                                                                                                                        ^
error: Could not resolve: "@nestjs/websockets/socket-module". Maybe you need to "bun install"?
    at C:\Users\unkno\Documents\RaidRequestHubMonorepo\node_modules\@nestjs\core\nest-application.js:19:116

20 | const { MicroservicesModule } = (0, optional_require_1.optionalRequire)('@nestjs/microservices/microservices-module', () => require('@nestjs/microservices/microservices-module'));
                                                                                                                                         ^
error: Could not resolve: "@nestjs/microservices/microservices-module". Maybe you need to "bun install"?
    at C:\Users\unkno\Documents\RaidRequestHubMonorepo\node_modules\@nestjs\core\nest-application.js:20:133

123 |         const { NestMicroservice } = (0, load_package_util_1.loadPackage)('@nestjs/microservices', 'NestFactory', () => require('@nestjs/microservices'));    
                                                                                                                                      ^
error: Could not resolve: "@nestjs/microservices". Maybe you need to "bun install"?
    at C:\Users\unkno\Documents\RaidRequestHubMonorepo\node_modules\@nestjs\core\nest-application.js:123:129

It appears to be associated with this part of the nest source, these are optional peer dependencies so it may be an edge case. I'm not using any of these features in my code I'll also add.

The class-validator is similarly an optional peer dep of @nestjs/mapped-types.

cosigyn commented 3 months ago

Related to https://github.com/oven-sh/bun/issues/4803