Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

typescript file loaded by 'require' by handlerCacheFromDir #608

Open
mackojar opened this issue Sep 29, 2019 · 2 comments
Open

typescript file loaded by 'require' by handlerCacheFromDir #608

mackojar opened this issue Sep 29, 2019 · 2 comments

Comments

@mackojar
Copy link

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:

var jsFileRegex = /\.(coffee|js|ts)$/;
[... iterate through all files in the controller folder... ]
var controllerName = file.replace(jsFileRegex, '');   <- remove `coffee|js|ts` extension from the file name
controller = require(path.resolve(path.join(dir, controllerName)));   <- require module - no file name extension

If you compile your typescript application there might be *.d.ts files created in the controllers folder. Thus, swagger-tools tries to call require 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 the handlerCacheFromDir code mentioned above? Is this an error and maybe only .js files should be processed by the function?

@3mard
Copy link

3mard commented Oct 6, 2019

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

@mackojar
Copy link
Author

mackojar commented Oct 6, 2019

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 #608 (comment) 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.

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

No branches or pull requests

2 participants