microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.61k stars 12.44k forks source link

Module resolution: Exception during run: error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'. #60197

Open khteh opened 12 hours ago

khteh commented 12 hours ago

Demo Repo

https://github.com/khteh/Node.JSRestAPI

Which of the following problems are you reporting?

The module specifier resolves at build time, but shouldn't because it doesn't at runtime

Demonstrate the defect described above with a code sample.

$ n run ts_test

> node.jsrestapi@1.0.0 ts_test
> export NODE_ENV=development && env TS_NODE_COMPILER_OPTIONS='{"module": "commonjs" }' ./node_modules/.bin/nyc --timeout 10000 --recursive --check-leaks --reporter=lcov --reporter=text --reporter=text-summary --reporter=html ./node_modules/.bin/mocha --loader=ts-node/esm -r ts-node/register tests/**/*.ts --reporter mocha-junit-reporter --reporter-options mochaFile=test_reports/mocha/test-results.xml --exit

(node:118119) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)
(node:118119) [DEP0180] DeprecationWarning: fs.Stats constructor is deprecated.
(Use `node --trace-deprecation ...` to show where the warning was created)

 Exception during run: error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.

Run tsc --showConfig and paste its output here

{
    "compilerOptions": {
        "incremental": true,
        "composite": true,
        "target": "es2022",
        "lib": [
            "es2022"
        ],
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "module": "nodenext",
        "rootDir": "./",
        "moduleResolution": "nodenext",
        "paths": {
            "webapi.core/*": [
                "./src/webapi.core/*"
            ],
            "infrastructure/*": [
                "./src/infrastructure/*"
            ],
            "webapi/*": [
                "./src/webapi/*"
            ]
        },
        "typeRoots": [
            "/usr/src/Node.JSRestAPI/node_modules/@types"
        ],
        "types": [
            "mocha"
        ],
        "resolveJsonModule": true,
        "declaration": true,
        "declarationMap": true,
        "sourceMap": true,
        "outDir": "./build",
        "importHelpers": true,
        "noEmitOnError": true,
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "strictPropertyInitialization": false,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "noImplicitOverride": true,
        "noPropertyAccessFromIndexSignature": false,
        "skipLibCheck": true
    },
    "files": [
        "./src/infrastructure/LoggerImpl.ts",
        "./src/infrastructure/db.ts",
        "./src/infrastructure/index.ts",
        "./src/infrastructure/types.ts",
        "./src/infrastructure/Data/Repositories/RepositoryBase.ts",
        "./src/infrastructure/Data/Repositories/StudentRepository.ts",
        "./src/infrastructure/Data/Repositories/TeacherRepository.ts",
        "./src/infrastructure/build/LoggerImpl.d.ts",
        "./src/infrastructure/build/db.d.ts",
        "./src/infrastructure/build/index.d.ts",
        "./src/infrastructure/build/types.d.ts",
        "./src/infrastructure/build/Data/Repositories/RepositoryBase.d.ts",
        "./src/infrastructure/build/Data/Repositories/StudentRepository.d.ts",
        "./src/infrastructure/build/Data/Repositories/TeacherRepository.d.ts",
        "./src/infrastructure/build/migrations/1661072499534-initial.d.ts",
        "./src/infrastructure/migrations/1661072499534-initial.ts",
        "./src/webapi/index.ts",
        "./src/webapi/server.ts",
        "./src/webapi/Controllers/AddStudentsToTeacherController.ts",
        "./src/webapi/Controllers/CommonStudentsController.ts",
        "./src/webapi/Controllers/FibonacciController.ts",
        "./src/webapi/Controllers/GreetingsController.ts",
        "./src/webapi/Controllers/RegistrationController.ts",
        "./src/webapi/Controllers/StudentNotificationsController.ts",
        "./src/webapi/Controllers/SuspendStudentController.ts",
        "./src/webapi/Models/Request/RegisterStudentModel.ts",
        "./src/webapi/Models/Request/RegisterTeacherModel.ts",
        "./src/webapi/Models/Response/ResponseBase.ts",
        "./src/webapi/Presenters/CommonStudentsPresenter.ts",
        "./src/webapi/Presenters/PresenterBase.ts",
        "./src/webapi/Presenters/RegisterUserPresenter.ts",
        "./src/webapi/Presenters/StudentNotificationsPresenter.ts",
        "./src/webapi/routes/api.ts",
        "./src/webapi/routes/healthchecks.ts",
        "./src/webapi/routes/index.ts",
        "./src/webapi.core/Fibonacci.ts",
        "./src/webapi.core/index.ts",
        "./src/webapi.core/types.ts",
        "./src/webapi.core/DTO/Error.ts",
        "./src/webapi.core/DTO/StudentDTO.ts",
        "./src/webapi.core/DTO/TeacherDTO.ts",
        "./src/webapi.core/DTO/UseCaseRequests/AddStudentsToTeacherRequest.ts",
        "./src/webapi.core/DTO/UseCaseRequests/CommonStudentsRequest.ts",
        "./src/webapi.core/DTO/UseCaseRequests/RegisterStudentRequest.ts",
        "./src/webapi.core/DTO/UseCaseRequests/RegisterTeacherRequest.ts",
        "./src/webapi.core/DTO/UseCaseRequests/RegistrationRequest.ts",
        "./src/webapi.core/DTO/UseCaseRequests/StudentNotificationsRequest.ts",
        "./src/webapi.core/DTO/UseCaseRequests/SuspendStudentRequest.ts",
        "./src/webapi.core/DTO/UseCaseResponse/CommonStudentsResponse.ts",
        "./src/webapi.core/DTO/UseCaseResponse/StudentNotificationsResponse.ts",
        "./src/webapi.core/DTO/UseCaseResponse/UseCaseResponseMessage.ts",
        "./src/webapi.core/Domain/Entities/EntityBase.ts",
        "./src/webapi.core/Domain/Entities/Student.ts",
        "./src/webapi.core/Domain/Entities/Teacher.ts",
        "./src/webapi.core/Domain/States/ApprovedState.ts",
        "./src/webapi.core/Domain/States/NewState.ts",
        "./src/webapi.core/Domain/States/QuotedState.ts",
        "./src/webapi.core/Domain/States/RejectedState.ts",
        "./src/webapi.core/Domain/States/State.ts",
        "./src/webapi.core/Domain/States/StateContext.ts",
        "./src/webapi.core/Domain/States/StateEnums.ts",
        "./src/webapi.core/Interfaces/ILogger.ts",
        "./src/webapi.core/Interfaces/IOutputPort.ts",
        "./src/webapi.core/Interfaces/IRepository.ts",
        "./src/webapi.core/Interfaces/IStudentRepository.ts",
        "./src/webapi.core/Interfaces/ITeacherRepository.ts",
        "./src/webapi.core/Interfaces/IUseCaseRequest.ts",
        "./src/webapi.core/Interfaces/IUseCaseRequestHandler.ts",
        "./src/webapi.core/Interfaces/UseCases/IAddStudentsToTeacherUseCase.ts",
        "./src/webapi.core/Interfaces/UseCases/ICommonStudentsUseCase.ts",
        "./src/webapi.core/Interfaces/UseCases/IRegisterStudentUseCase.ts",
        "./src/webapi.core/Interfaces/UseCases/IRegisterTeacherUseCase.ts",
        "./src/webapi.core/Interfaces/UseCases/IStudentNotificationsUseCase.ts",
        "./src/webapi.core/Interfaces/UseCases/ISuspendStudentUseCase.ts",
        "./src/webapi.core/UseCases/AddStudentsToTeacherUseCase.ts",
        "./src/webapi.core/UseCases/CommonStudentsUseCase.ts",
        "./src/webapi.core/UseCases/RegisterStudentUseCase.ts",
        "./src/webapi.core/UseCases/RegisterTeacherUseCase.ts",
        "./src/webapi.core/UseCases/StudentNotificationsUseCase.ts",
        "./src/webapi.core/UseCases/SuspendStudentUseCase.ts",
        "./src/webapi.core/build/Fibonacci.d.ts",
        "./src/webapi.core/build/index.d.ts",
        "./src/webapi.core/build/types.d.ts",
        "./src/webapi.core/build/DTO/Error.d.ts",
        "./src/webapi.core/build/DTO/StudentDTO.d.ts",
        "./src/webapi.core/build/DTO/TeacherDTO.d.ts",
        "./src/webapi.core/build/DTO/UseCaseRequests/AddStudentsToTeacherRequest.d.ts",
        "./src/webapi.core/build/DTO/UseCaseRequests/CommonStudentsRequest.d.ts",
        "./src/webapi.core/build/DTO/UseCaseRequests/RegisterStudentRequest.d.ts",
        "./src/webapi.core/build/DTO/UseCaseRequests/RegisterTeacherRequest.d.ts",
        "./src/webapi.core/build/DTO/UseCaseRequests/RegistrationRequest.d.ts",
        "./src/webapi.core/build/DTO/UseCaseRequests/StudentNotificationsRequest.d.ts",
        "./src/webapi.core/build/DTO/UseCaseRequests/SuspendStudentRequest.d.ts",
        "./src/webapi.core/build/DTO/UseCaseResponse/CommonStudentsResponse.d.ts",
        "./src/webapi.core/build/DTO/UseCaseResponse/StudentNotificationsResponse.d.ts",
        "./src/webapi.core/build/DTO/UseCaseResponse/UseCaseResponseMessage.d.ts",
        "./src/webapi.core/build/Domain/Entities/EntityBase.d.ts",
        "./src/webapi.core/build/Domain/Entities/Student.d.ts",
        "./src/webapi.core/build/Domain/Entities/Teacher.d.ts",
        "./src/webapi.core/build/Domain/States/ApprovedState.d.ts",
        "./src/webapi.core/build/Domain/States/NewState.d.ts",
        "./src/webapi.core/build/Domain/States/QuotedState.d.ts",
        "./src/webapi.core/build/Domain/States/RejectedState.d.ts",
        "./src/webapi.core/build/Domain/States/State.d.ts",
        "./src/webapi.core/build/Domain/States/StateContext.d.ts",
        "./src/webapi.core/build/Domain/States/StateEnums.d.ts",
        "./src/webapi.core/build/Interfaces/ILogger.d.ts",
        "./src/webapi.core/build/Interfaces/IOutputPort.d.ts",
        "./src/webapi.core/build/Interfaces/IRepository.d.ts",
        "./src/webapi.core/build/Interfaces/IStudentRepository.d.ts",
        "./src/webapi.core/build/Interfaces/ITeacherRepository.d.ts",
        "./src/webapi.core/build/Interfaces/IUseCaseRequest.d.ts",
        "./src/webapi.core/build/Interfaces/IUseCaseRequestHandler.d.ts",
        "./src/webapi.core/build/Interfaces/UseCases/IAddStudentsToTeacherUseCase.d.ts",
        "./src/webapi.core/build/Interfaces/UseCases/ICommonStudentsUseCase.d.ts",
        "./src/webapi.core/build/Interfaces/UseCases/IRegisterStudentUseCase.d.ts",
        "./src/webapi.core/build/Interfaces/UseCases/IRegisterTeacherUseCase.d.ts",
        "./src/webapi.core/build/Interfaces/UseCases/IStudentNotificationsUseCase.d.ts",
        "./src/webapi.core/build/Interfaces/UseCases/ISuspendStudentUseCase.d.ts",
        "./src/webapi.core/build/UseCases/AddStudentsToTeacherUseCase.d.ts",
        "./src/webapi.core/build/UseCases/CommonStudentsUseCase.d.ts",
        "./src/webapi.core/build/UseCases/RegisterStudentUseCase.d.ts",
        "./src/webapi.core/build/UseCases/RegisterTeacherUseCase.d.ts",
        "./src/webapi.core/build/UseCases/StudentNotificationsUseCase.d.ts",
        "./src/webapi.core/build/UseCases/SuspendStudentUseCase.d.ts"
    ],
    "include": [
        "src/**/*",
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules",
        "build"
    ]
}

Run tsc --traceResolution and paste its output here

output too long.

Paste the package.json of the importing module, if it exists

Not sure what you mean with importing module.

Paste the package.json of the target module, if it exists

Not sure what you mean with target module.

Any other comments can go here

https://github.com/mochajs/mocha/issues/5171#issuecomment-2400435684 I can run the application without any error. This error only appears when I run the mocha test

RyanCavanaugh commented 2 hours ago

export NODE_ENV=development && env TS_NODE_COMPILER_OPTIONS='{"module": "commonjs" }' ./node_modules/.bin/nyc --timeout 10000 --recursive --check-leaks --reporter=lcov --reporter=text --reporter=text-summary --reporter=html ./node_modules/.bin/mocha --loader=ts-node/esm -r ts-node/register tests/*/.ts --reporter mocha-junit-reporter --reporter-options mochaFile=test_reports/mocha/test-results.xml --exit

None of these are tsc. We don't control how external tools (mis)configure something.