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

tsconfig-paths rewrite to be broken when import with file extension (.js) #1437

Open
2 of 4 tasks
msimon opened this issue Dec 2, 2021 · 7 comments
Open
2 of 4 tasks

Comments

@msimon
Copy link

msimon commented Dec 2, 2021

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

With a project organised that way:

src:
  - main.ts
  - app.module.ts
  ....
tsconfig.json
...

Defining an import in main.ts the following way:

import { AppModule } from 'src/app.module.js';

result in it being compiled to:

const app_module_1 = require("src/app.module.js");

ignoring the non-relative path to relative path rewrite that should be done by tsconfig-paths.

Removing the .js from the import, result in the correct compilation:

import { AppModule } from 'src/app.module';
const app_module_1 = require("./app.module");

I took a quick look into nest-cli/lib/compiler/hooks/tsconfig-paths.hook.ts to see if I could spot the issues.
My guess is that the match on l.56 does not return anything if the extension is set.

I'll try to ding more into it soon, but I figure I would ask in case there is an obvious fix for this.

Minimum reproduction code

https://github.com/HomeskilletHealthInc/nestJs_import_rewrite_bug

Steps to reproduce

Run:

nest build
cat ./dist/main.js

This code:

import { AppModule } from 'src/app.module';
import { AppService } from 'src/app.service.js';

is compiled to:

const app_module_1 = require("./app.module");
const app_service_js_1 = require("src/app.service.js");

Failing to rewrite src when .js is precise.

Expected behavior

Defining an import in main.ts the following way:

import { AppModule } from 'src/app.module.js';

Should compile to:

const app_module_1 = require("./app.module.js");

Package version

8.1.5

NestJS version

8.2.3

Node.js version

v14.17.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

The reason I would to get the .js is because it is required for ESM

@jmcdo29
Copy link
Member

jmcdo29 commented Dec 2, 2021

Can you please provide a minimum reproduction repository?

@kamilmysliwiec
Copy link
Member

BTW using absolute paths (using "src" in your import paths) is considered as a bad practice

@msimon
Copy link
Author

msimon commented Dec 3, 2021

@jmcdo29 https://github.com/HomeskilletHealthInc/nestJs_import_rewrite_bug

@kamilmysliwiec Using absolute path is, but I don't believe that applies to non-relative path. The code will always compile to the same relative path, independently of the service/computer is being compiled on.
The gain of using non-relative import is so much higher imo. It's even supported out of the box by NestJs.

Let me know if I misses something, but I haven't seen any large projects using fully relative path, nor a good argument to use them.

@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Dec 3, 2021

Yeah just to clarify: it applies only to paths that contain src directory (so using rootDir + absolute paths)

@Val-istar-Guo
Copy link

Val-istar-Guo commented Jan 20, 2022

I had the same problem and couldn't import an esm package. Do I have to change all absolute paths to relative paths.
Is there any solution to solve this problem temporarily.

@amit78523
Copy link

I had the same problem and couldn't import an esm package. Do I have to change all absolute paths to relative paths. Is there any solution to solve this problem temporarily.

I am using this command temporarily nest build && tsc-alias tsc-alias is a node module.

@Ttou
Copy link

Ttou commented Nov 9, 2023

same problem, microsoft/TypeScript#56350

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants