class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

feature: Requesting support for node16/nodenext module resolution

Open kf6kjg opened this issue 2 years ago • 4 comments

Description

When using this library in a project that has the TSConfig setting moduleResolution set to node16 the TS compiler throws errors on every import and export ... from call.

Example:

 ../../node_modules/class-validator/types/index.d.ts(1,33): error TS2834: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.

Proposed solution

  1. Change the tsconfig.json#/compilerOptions/moduleResolution setting to node16.
  2. Bulk alter the codebase to correct the import and export paths to have the full path to the target file with the extension.
  3. Manually fix the outliers in the code: converting implicit index.js into explicit being one such.

This should have no deleterious effect since, as far as I am aware, no version of TS or Node cared about having the filename and extension until ES6 came along.

kf6kjg avatar Apr 05 '23 21:04 kf6kjg

I just ran across this issue myself but doesn't seem like it is a problem with class-validator. This is the issue I see:

` node_modules/class-validator/types/decorator/string/IsPhoneNumber.d.ts:2:29 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("libphonenumber-js")' call instead.

2 import { CountryCode } from 'libphonenumber-js'; ~~~~~~~~~~~~~~~~~~~

node_modules/libphonenumber-js/index.d.ts:18:8 - error TS2834: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.

18 } from './types'; ~~~~~~~~~`

tet-lenovo avatar Apr 20 '23 16:04 tet-lenovo

The libphonenumber-js problem is a separate issue. Fundamentally that problem is due to libphonenumber-js not handling its ESM exports correctly, which is causing problems both in this project and downstream projects.

kf6kjg avatar Apr 20 '23 16:04 kf6kjg

Any suggestions on how to fix it?

The current file is a CommonJS module whose imports will produce 'require' calls; 
however, the referenced file is an ECMAScript module and cannot be imported with 'require'. 
Consider writing a dynamic 'import("libphonenumber-js")' call instead.

witem avatar Aug 07 '23 13:08 witem

Please converse about the libphonenumber-js problem in https://github.com/typestack/class-validator/issues/1947, thank you. :)

kf6kjg avatar Aug 08 '23 22:08 kf6kjg