Open mackojar opened 4 years ago
Hey how are you running the app ? are you using any sort of loaders ? (ts-node ... ) ? because nodejs will not load any .ts files by default and will always only load .js,.json,.node https://nodejs.org/api/modules.html#modules_all_together
I created TypeScript application, and as part of it: controller/Test.ts
controller. I compiled it to regular JavaScript (using tsc - output went to dist
folder) and noticed Typescript definition file (dist/controller/Test.d.ts
) was generated by the compiler (besides Test.js
).
The dist
folder was part of node.js application. And then swagger-tools
tries to load every file matching regular expression mentioned in https://github.com/apigee-127/swagger-tools/issues/608#issue-499942010 using require
. Hence it also tries to load Test.d.ts
using require
, which obviously fails in the following way:
Error: Cannot find module '/Users/mackoj/progs/swagger-typescript-server/app/dist/controllers/Test.d'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at /Users/mackoj/progs/swagger-typescript-server/app/node_modules/swagger-tools/middleware/swagger-router.js:81:22
at arrayEach (/Users/mackoj/progs/swagger-typescript-server/app/node_modules/lodash/lodash.js:516:11)
at Function.forEach (/Users/mackoj/progs/swagger-typescript-server/app/node_modules/lodash/lodash.js:9342:14)
at /Users/mackoj/progs/swagger-typescript-server/app/node_modules/swagger-tools/middleware/swagger-router.js:76:7
at arrayEach (/Users/mackoj/progs/swagger-typescript-server/app/node_modules/lodash/lodash.js:516:11)
at Function.forEach (/Users/mackoj/progs/swagger-typescript-server/app/node_modules/lodash/lodash.js:9342:14)
As a workaround I requested tsc
(TypeScript compiler) to not generate definition file (so the Test.d.ts
file is not generated any more).
However the question here is what is an idea behind trying to load using require
also *.ts
files, which is what the swagger-tools/middleware
currently does. I think that's an error.
I'm using swagger-tools in my typescript/nodejs based REST API server.
Please take a look at swagger-tools module https://github.com/apigee-127/swagger-tools/blob/master/middleware/swagger-router.js, function
handlerCacheFromDir
excerpt:If you compile your typescript application there might be
*.d.ts
files created in the controllers folder. Thus, swagger-tools tries to callrequire
over the*.d
module name (no file name extension) which causes error (require
will not load*.ts
file).Obviously we can instruct typescript to stop generating corresponding
*.d.ts
files, however what is the idea behind thehandlerCacheFromDir
code mentioned above? Is this an error and maybe only.js
files should be processed by the function?