Closed KirianCaumes closed 1 year ago
I think this is a duplicate
@kdy1 can you link the original issue in this case? I can't find it
I see you put the bug in milestone v1.3.68 @kdy1 I just tested version 1.3.68, and the bug is still present. The repository in my initial message is up to date on this.
It's not fixed. It's duplicate
I told you.
Okay, that's a misinterpretation on my part, sorry. I thought the bug was fixed in version 1.3.68, hence my feedback! 😅 Could you share the link of the issue to follow then? I can't find it
@KirianCaumes I don't find the duplicate neither
Duplicate of what... @kdy1 you keep closing similar issues every time because "duplicate" but you haven't yet shared a link to the original issue, so again - DUPLICATE OF WHICH EXACTLY ISSUE?
I have the same issue.
@jovicailievski https://kdy1.dev/posts/2023/1/plus-1-is-spam
same issue here
@KirianCaumes have you managed to make it work?
@gabrielcipriano In this NestJS 10 boilerplate the SWC compiler works, so I'd suggest to look what they did there. Only on Windows it fails to serve due to a bug in SWC: https://github.com/nestjs/nest/issues/12214
@wp-harm If it fails only on windows, is it related to https://github.com/swc-project/swc/issues/7806 ?
@kdy1 I'm not entirely sure as the errors or setups vary slightly.
It basically is this (the rest of the thread should give the necessary context): https://github.com/nestjs/nest/issues/11891#issuecomment-1610682778 The person above says on Unix it works, on MacOS it works as well I can confirm and it seems this person as well: https://github.com/nestjs/nest/issues/11891#issuecomment-1622699444. So therefore the path alias only fails on Windows.
Reading the thread a bit more closely, I do notice this comment which is pretty much the same error/conclusion as the issue you refer to: https://github.com/nestjs/nest/issues/11891#issuecomment-1611117505. It would be interesting to see if the resulting compilation is different on MacOS or Unix. I could try this if you want me to 👍
@kdy1 I did a bit more research on my Windows PC. Switching to the non-SWC compiler I got a relative path that allowed my app to work. Going back to SWC I got an absolute path which did not work. Manually making the path relative by changing the compiled JS file (just like it was in the non-SWC dist) and my app worked again.
So the tl:dr, if the path can be become relative on Windows at least some of the issues will be resolved, maybe all of them as think many people don't realise their OS is the problem! 💪
I'm not sure my orignal problem is Windows related because I'm working with WSL (Ubuntu 20.04) on a Windows 11 computer. I think the problem must be same on a real Linux machine, even if I'm not 100% sure.
I updated the "Playground link repo" in my first message to SWC 1.3.77 version, and the problem is a bit different:
> test-nestjs@0.0.1 start
> nest start
✔ TSC Initializing type checker...
> TSC Found 0 issues.
> SWC Running...
thread '<unnamed>' panicked at 'base_dir(./src) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly.', /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_module-0.173.31/src/path.rs:104:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread '<unnamed>' panicked at 'base_dir(./src) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly.', /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_module-0.173.31/src/path.rs:104:13
thread '<unnamed>' panicked at 'base_dir(./src) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly.', /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_module-0.173.31/src/path.rs:104:13
failed to handle: base_dir(./src) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly.
thread 'thread '<unnamed><unnamed>' panicked at '' panicked at 'base_dir(./src) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly.base_dir(./src) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly.', ', /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_module-0.173.31/src/path.rs/usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_module-0.173.31/src/path.rs::104104::1313
failed to handle: base_dir(./src) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly.
failed to handle: base_dir(./src) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly.
failed to handle: base_dir(./src) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly.
failed to handle: base_dir(./src) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly.
Failed to compile 5 files with swc.
Error Failed to compile:
src/app.controller.spec.ts
src/app.controller.ts
src/app.module.ts
src/app.service.ts
src/main.ts
Then if I do one of this action on my .swcrc
, I have the following error:
baseUrl
to an absolute path like /home/xxx/swc-nestjs/src
> test-nestjs@0.0.1 start
> nest start
✔ TSC Initializing type checker...
> TSC Found 0 issues.
> SWC Running...
Successfully compiled: 5 files with swc (22.08ms)
Error: Cannot find module 'app.module'
Require stack:
- /home/kirian/Projects/Perso/test-nestjs/dist/main.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
at Function.Module._load (node:internal/modules/cjs/loader:841:27)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/home/kirian/Projects/Perso/test-nestjs/dist/main.js:6:20)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Function.Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
@KirianCaumes I see. I've not come across a setup like that. I think it's a separate issue from what I'm talking about.
Having said that, I'll continue my issue in https://github.com/swc-project/swc/issues/7806
@KirianCaumes https://github.com/nestjs/nest-cli/pull/2245
same error here "Error: Cannot find module 'file...' Require stack:..."
but what's the solution? i can't find it anywhere
https://github.com/nestjs/nest-cli/releases/tag/10.1.15 should fix it
Thanks a lot for your time @kdy1! But sadly, the problem does not seems to be fixed. I tried with @swc/core 1.3.78 and @nestjs/cli 10.1.15, but I still have a problem (https://github.com/KirianCaumes/nestjs-bug-swc-import):
> test-nestjs@0.0.1 start
> nest start
✔ TSC Initializing type checker...
> TSC Found 0 issues.
> SWC Running...
Successfully compiled: 5 files with swc (32.94ms)
Error: Cannot find module 'app.module'
Require stack:
- /home/kirian/Projects/Perso/test-nestjs/dist/main.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
at Function.Module._load (node:internal/modules/cjs/loader:841:27)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/home/kirian/Projects/Perso/test-nestjs/dist/main.js:6:20)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Function.Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
@KirianCaumes You don't have jsc.paths
I tested, and
{
"$schema": "https://json.schemastore.org/swcrc",
"sourceMaps": true,
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": true
},
"baseUrl": "./src",
"paths": {
"*": [
"*",
"src/*"
]
}
},
"minify": false
}
works
It's better! Now it can resolve app.module
!
But I have tried more things, and it seems to still not be perfectly working.
I created a simple CRUD, and now I have this error:
> test-nestjs@0.0.1 start
> nest start
✔ TSC Initializing type checker...
> TSC Found 0 issues.
> SWC Running...
Successfully compiled: 10 files with swc (31.16ms)
Error: Cannot find module 'users/users.service'
Require stack:
- /home/kirian/Projects/Perso/test-nestjs/dist/users/users.module.js
- /home/kirian/Projects/Perso/test-nestjs/dist/app.module.js
- /home/kirian/Projects/Perso/test-nestjs/dist/main.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
at Function.Module._load (node:internal/modules/cjs/loader:841:27)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/home/kirian/Projects/Perso/test-nestjs/dist/users/users.module.js:12:23)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Function.Module._load (node:internal/modules/cjs/loader:878:12)
at Module.require (node:internal/modules/cjs/loader:1061:19)
// app.module.ts
import { Module } from '@nestjs/common';
import { UsersModule } from 'users/users.module';
@Module({
imports: [UsersModule],
controllers: [],
providers: [],
})
export class AppModule {}
// users.module.ts
import { Module } from '@nestjs/common';
import { UsersService } from 'users/users.service';
import { UsersController } from 'users/users.controller';
@Module({
controllers: [UsersController],
providers: [UsersService],
})
export class UsersModule {}
// users.service.ts
import { Injectable } from '@nestjs/common';
import { CreateUserDto } from 'users/dto/create-user.dto';
import { UpdateUserDto } from 'users/dto/update-user.dto';
@Injectable()
export class UsersService {
create(createUserDto: CreateUserDto) {
return 'This action adds a new user';
}
findAll() {
return `This action returns all users`;
}
findOne(id: number) {
return `This action returns a #${id} user`;
}
update(id: number, updateUserDto: UpdateUserDto) {
return `This action updates a #${id} user`;
}
remove(id: number) {
return `This action removes a #${id} user`;
}
}
I have tried several "paths" config, but didn't manage to make it works 😕
I'm not sure if it's any help, but the dist/users/users.module.js
generated with the classic Nest compiler contains this:
const common_1 = require("@nestjs/common");
const users_service_1 = require("./users.service");
const users_controller_1 = require("./users.controller");
But with SWC:
const _common = require("@nestjs/common");
const _usersservice = require("users/users.service");
const _userscontroller = require("users/users.controller");
My repo is also up to date: https://github.com/KirianCaumes/nestjs-bug-swc-import
@KirianCaumes I'm working on a fix for it with https://github.com/swc-project/swc/pull/7852
I started working with SWC
for the first time in NEST JS and MongoDB
. And I've faced three different errors.
1- When I tried to give an enum as a type to a property in a schema ,which is imported from other file, mongoose failed to create a correct schema. which lead me to mongoose incorrect schema type error
. After searching I found that SWC
needs enums in the same file where you are using them. And it is annoying for me as I'm used to put my all enums into a separate folder under src
.
2-The Auto Import Issue
. Which lead me to this github page in the search of the solution.
3-I saw that I'm setting configs for SWC
only in my nest-json.cli
file, under
"compilerOptions": {
"builder": "swc",
"typeCheck": true
}
and not using .swcrc
file for the solution, so I tried to create a .swcrc
with following content
{
"$schema": "https://json.schemastore.org/swcrc",
"sourceMaps": true,
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": true
},
"baseUrl": "./src",
"paths": {
"*": ["*", "src/*"]
}
},
"minify": false
}
And it lead me to another error
panicked at 'base_dir(
./src
) must be absolute. Please ensure thatjsc.baseUrl
is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if.swcrc
is used, but if not, there are many candidates. e.g. the directory containingpackage.json
, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it...
I'll mark it as off-topic as 1 and 2 are not related to this issue at all and 3 is a duplicate
I don't think it's off-topic. As I've found that the first two issues are due to SWC
's import bug as well. When I put the same path ./src
in the tsconfig.json
, "baseUrl": "./src",
. Then the app started working fine in an odd manner. What I mean by Odd manner?. When I run the app with nest start --watch
, the same lines are printed in the terminal, but this time the app doesn't crash and all the functionalities works fine, but when I build
the project it fails due to same reasons. Second, now I don't have to type imports by my self. The auto imports are working fine,at least till now. Third I moved my enum back to where it was before,outside of schema file. And imported in with auto import and the 1st error was gone. No schema validation issues any more...
First, 1 is not a bug. And auto import has nothing to do with SWC
I mean absolute paths
. When an import is like src/etc/etc
, I face Can not find module
, and it only happens when using SWC
. with old ts compiler
it works fine. And about 1 , without SWC
, that also works fine.All those three issues i'm facing when I use SWC
.
Issue occurs only if I use SWC
does not mean it's a bug of SWC
I'm sorry If I wasn't able to make my self clear. I'm saying Error Only When Use Of SWC In NestJs V10
I think you were clear enough. Rather I think the problem is that I wasn't clear. I'm saying, that even if your problem occurs only if you enable SWC, it may not be a bug of SWC.
Sorry for inconvenience. But Nest Js sent the first person here to report. And I found my cases similar to him. So I also reported here. As I wasn't able to find the solution at stackoverflow as well.
@m-nouman-munir Can you try v1.3.79 once https://github.com/swc-project/swc/actions/runs/5972622833 is finished?
1-Sure I'll Try that.
2- The Auto Import
Issue. Let me just explain it a little bit.
2.1-I have an export enum X{}
in src/enum/enum.ts
2.2-I have a schema Y, in Y there is a property z : X , in src/components/Y.ts
.
2.3-Now I have to import X in Y. SO when I use ctl + .
, in vscode to import X. It imports like import {X} from 'src/enum.enum'
. And With SWC
it throws error TypeError: Can not found module X
, and if I disable SWC and use the other tsc or babel compiler which by default NEST JS uses, It does not throw error
.
2.4-After error. When I change the import to like import { X } from '../../../enum/enum'
, by my self ,It works fine with SWC
.
2.5- I'M ON UBUNTU
Actually, import {X} from 'src/enum.enum'
this part is 3 and it'll be fixed by the new version
Yes, by reading all the above messages, I totally understood that. But my mistake I wasn't able to explain things clearly and put you under confusion. And also that's why I explained the entire point 1. As it was happening due to import issues, and also solved by itself when I changed things related to imports in .swcrc
and tscnofig.json
.
Hey, thanks a lot!
I just tried it, but now it seems there's a problem with node_modules
imports:
> test-nestjs@0.0.1 start
> nest start
✔ TSC Initializing type checker...
> TSC Found 0 issues.
> SWC Running...
Successfully compiled: 10 files with swc (29.65ms)
Error: Cannot find module 'node_modules/@nestjs/core/index.js'
Require stack:
- /home/kirian/Projects/Perso/test-nestjs/dist/main.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
at Function.Module._load (node:internal/modules/cjs/loader:841:27)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/home/kirian/Projects/Perso/test-nestjs/dist/main.js:5:15)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Function.Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
The import of node_modules/@nestjs/core/index.js
in dist/main.js
(generated from src/main.ts
) looks like this:
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const _core = require("node_modules/@nestjs/core/index.js");
const _appmodule = require("./app.module");
async function bootstrap() {
const app = await _core.NestFactory.create(_appmodule.AppModule);
await app.listen(5002);
}
bootstrap();
//# sourceMappingURL=main.js.map
And I don't think it was prefixed with node_modules/
before.
I also tried to edit paths
from .swcrc
, but I didn't find a solution either 😕
My repo is up to date: https://github.com/KirianCaumes/nestjs-bug-swc-import
I changed version in my package.json to v1.3.79
. But still it fails when run npm run build
I'm also finding Error: Cannot find module 'node_modules/@nestjs/common/index.js'
with nest start --watch
So v.1.3.80 available now?
It now works perfectly with version 1.3.80! I also tested it with a larger NestJs project, and I did not find any issue at all. Thank you so much for your time an effort @kdy1! 👏
@KirianCaumes or @kdy1 can you help me I'm still facing issue. I Installed latest SWC version.But When I try to build my project It does not get's build.And I face
thread 'thread '<unnamed><unnamed>' panicked at 'thread '' panicked at 'base_dir(
./src) must be absolute. Please ensure that
jsc.baseUrlis specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if
.swcrcis used, but if not, there are many candidates. e.g. the directory containing
package.json, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.thread '<unnamed><unnamed>base_dir(
./src) must be absolute. Please ensure that
jsc.baseUrlis specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if
.swcrcis used, but if not, there are many candidates. e.g. the directory containing
package.json, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.
Error Failed to compile
And If I do nest start --watch
I face
⠋ TSC Initializing type checker...Error: Cannot find module 'src/app.module'
Settings
.swcrc
{
"$schema": "https://json.schemastore.org/swcrc",
"sourceMaps": true,
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": true
},
"baseUrl": "./src",
"paths": {
"*": ["*", "src/*"]
}
},
"minify": false
}
nest-cli.json
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"builder": "swc",
"typeCheck": true
}
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./src",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}
Describe the bug
When I run
npm run start
(nest start
) in my project with NestJs 10 andswc
enabled, an error is thrown:Input code
No response
Config
Playground link
https://github.com/KirianCaumes/nestjs-bug-swc-import
Expected behavior
It should work the same way as without
swc
enabled and withbaseUrl
being used.Actual behavior
Import with non-relative modules are not found
Version
1.3.67
Additional context
I already posted an issue on NestJs, but they redirected me here.