swagger-node icon indicating copy to clipboard operation
swagger-node copied to clipboard

Error: Cannot find module swagger_router

Open TomlDev opened this issue 6 years ago • 23 comments

Hello, I just tried the npm tutorial "your swagger API in five steps", linux mint 19.04, nodejs 12.2.0, npm 6.9.0

$ npm install -g swagger
$ swagger project create hello-world     # create with express
$ cd hello-world
$ swagger project edit

After that I finish editing with CTRL+C and try to start the project:

$ swagger project start

I get the follow error:

/home/tom/tutorials/hello-world$ swagger project start
Starting: /home/tom/tutorials/hello-world/app.js...
  project started here: http://localhost:10010/
  project will restart on changes.
  to restart at any time, enter `rs`
Error initializing middleware
Error: Cannot find module '/home/tom/tutorials/hello-world/api/fittings/swagger_router'
Require stack:
- /home/tom/tutorials/hello-world/node_modules/bagpipes/lib/fittingTypes/user.js
- /home/tom/tutorials/hello-world/node_modules/bagpipes/lib/bagpipes.js
- /home/tom/tutorials/hello-world/node_modules/bagpipes/lib/index.js
- /home/tom/tutorials/hello-world/node_modules/swagger-node-runner/index.js
- /home/tom/tutorials/hello-world/node_modules/swagger-express-mw/lib/index.js
- /home/tom/tutorials/hello-world/app.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:610:15)
    at Function.Module._load (internal/modules/cjs/loader.js:526:27)
    at Module.require (internal/modules/cjs/loader.js:666:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at createFitting (/home/tom/tutorials/hello-world/node_modules/bagpipes/lib/fittingTypes/user.js:18:20)
    at Bagpipes.newFitting (/home/tom/tutorials/hello-world/node_modules/bagpipes/lib/bagpipes.js:158:17)
    at Bagpipes.createFitting (/home/tom/tutorials/hello-world/node_modules/bagpipes/lib/bagpipes.js:147:22)
    at Bagpipes.createPipe (/home/tom/tutorials/hello-world/node_modules/bagpipes/lib/bagpipes.js:111:19)
    at Bagpipes.getPipe (/home/tom/tutorials/hello-world/node_modules/bagpipes/lib/bagpipes.js:50:38)
    at /home/tom/tutorials/hello-world/node_modules/bagpipes/lib/bagpipes.js:34:10

TomlDev avatar May 20 '19 11:05 TomlDev

I know how to fixed this issue.

Copy some files from project/node_modules/swagger-node-runner/fittings to project/api/fittings/swagger_router, from project/node_modules/bagpipes/lib/fittings to project/api/fittings/swagger_router

Finally, start is OK.

I think I have make a wrong path with module.

pz2147 avatar May 22 '19 15:05 pz2147

Is this the best approach to fix the issue? This seems like a new problem with Swagger. Please help with the right approach. Thanks.

ranrao avatar May 29 '19 00:05 ranrao

This is probably because you are running node v12.2.0! Try to downgrade your node to version 10, version 12 is not stable.

deventorum avatar Jun 04 '19 17:06 deventorum

I also had same problems. Tried the file copying from fittings-directory. This didn't work for me. I downgraded node to 10.16.0 (and npm 6.9.0) and all was solved....

racingfever avatar Jun 12 '19 08:06 racingfever

@deventorum Node v10.16.0 is work for me. Thank you!

binhapp avatar Jul 10 '19 07:07 binhapp

I have the exact same issue as the OP. Node v12 is becoming LTS in one month; I'd generally assume I should be able to follow the instructions in the README and have a working 'getting started' project. This doesn't appear to be the case at the moment.

crutlandTAG avatar Sep 23 '19 14:09 crutlandTAG

I have the exact same issue too. I was trying to upgrade to node v12.

Error initializing middleware Error: Cannot find module '/Users/nodejs/services/account/api/fittings/swagger_router' Require stack:

  • /Users/nodejs/services/account/node_modules/bagpipes/lib/fittingTypes/user.js
  • /Users/nodejs/services/account/node_modules/bagpipes/lib/bagpipes.js
  • /Users/nodejs/services/account/node_modules/bagpipes/lib/index.js
  • /Users/nodejs/services/account/node_modules/swagger-node-runner/index.js
  • /Users/nodejs/services/account/node_modules/swagger-express-mw/lib/index.js
  • /Users/nodejs/services/account/node_modules/cm-util/app-start.js
  • /Users/nodejs/services/account/node_modules/cm-util/index.js
  • /Users/nodejs/services/account/app.js

rajeshpudota avatar Sep 24 '19 18:09 rajeshpudota

Problem is that in Node 12 Module Not Found error differs from Node 10. bagpipes relies on error message text. This can tepmorary be fixed in this file: node_modules/bagpipes/lib/fittingTypes/user.js file: var split = err.message.split(path.sep); should be var split = err.message.split('\n')[0].split(path.sep);

s-kalaus avatar Oct 20 '19 11:10 s-kalaus

Happens to me as well on node 12

shiranyo avatar Nov 24 '19 16:11 shiranyo

Same issue env: node 12.13.1 npm 6.12.1

Ethns avatar Nov 29 '19 01:11 Ethns

Try this, tested on node version to date (12.13.1):

  1. Update your swagger-express-mw:

"swagger-express-mw": "^0.7.0"

  1. Add swagger_params_parser to swagger_controllers at config/default.yaml ...

pipe for all swagger-node controllers

swagger_controllers:
  - onError: json_error_handler
  - cors
  - swagger_params_parser
  ...
  1. Run npm install

  2. Run swagger project start

I wish swagger-node will receive more timely update.

gohmc avatar Dec 01 '19 08:12 gohmc

I also had to move to node 12 and since I felt this lib is no longer maintained nor supported I decided to shift to swagger-ui-express . the above solution could have saved me serveral hours of debugging.

taitelman avatar Dec 01 '19 11:12 taitelman

try to install swagger-router with command $npm install --save swagger-router

it's work for me

sonywibisono avatar Feb 25 '20 11:02 sonywibisono

This is probably because you are running node v12.2.0! Try to downgrade your node to version 10, version 12 is not stable.

This worked for me as well, downgraded to v10.15.0.

amrendraw3 avatar Mar 31 '20 11:03 amrendraw3

@gohmc - Thank you, your solution works splendidly with v12.16.1 and requires minimal configuration without touching the code.

smeckman avatar Apr 02 '20 14:04 smeckman

Try this, tested on node version to date (12.13.1):

1. Update your swagger-express-mw:

"swagger-express-mw": "^0.7.0"

1. Add swagger_params_parser to swagger_controllers at config/default.yaml
   ...

pipe for all swagger-node controllers

swagger_controllers:
  - onError: json_error_handler
  - cors
  - swagger_params_parser
  ...
1. Run npm install

2. Run swagger project start

I wish swagger-node will receive more timely update.

Thanks for the fix @gohmc.

Note: this fix also works for me with node v15.1.0

balain avatar Nov 09 '20 02:11 balain

Try this, tested on node version to date (12.13.1):

  1. Update your swagger-express-mw:

"swagger-express-mw": "^0.7.0"

  1. Add swagger_params_parser to swagger_controllers at config/default.yaml ...

pipe for all swagger-node controllers

swagger_controllers:
  - onError: json_error_handler
  - cors
  - swagger_params_parser
  ...
  1. Run npm install
  2. Run swagger project start

I wish swagger-node will receive more timely update.

I did the same but the issue wasnt resolved, its showing this error `Error initializing middleware Error: Cannot find module 'D:\swagggggger\hello\api\fittings\swagger_router' Require stack:

  • D:\swagggggger\hello\node_modules\bagpipes\lib\fittingTypes\user.js
  • D:\swagggggger\hello\node_modules\bagpipes\lib\bagpipes.js
  • D:\swagggggger\hello\node_modules\bagpipes\lib\index.js
  • D:\swagggggger\hello\node_modules\swagger-node-runner\index.js
  • D:\swagggggger\hello\node_modules\swagger-express-mw\lib\index.js
  • D:\swagggggger\hello\app.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1080:15) at Function.Module._load (internal/modules/cjs/loader.js:923:27) at Module.require (internal/modules/cjs/loader.js:1140:19) at require (internal/modules/cjs/helpers.js:75:18) at createFitting (D:\swagggggger\hello\node_modules\bagpipes\lib\fittingTypes\user.js:18:20) at Bagpipes.newFitting (D:\swagggggger\hello\node_modules\bagpipes\lib\bagpipes.js:158:17) at Bagpipes.createFitting (D:\swagggggger\hello\node_modules\bagpipes\lib\bagpipes.js:147:22) at Bagpipes.createPipe (D:\swagggggger\hello\node_modules\bagpipes\lib\bagpipes.js:111:19) at Bagpipes.getPipe (D:\swagggggger\hello\node_modules\bagpipes\lib\bagpipes.js:50:38) at D:\swagggggger\hello\node_modules\bagpipes\lib\bagpipes.js:34:10`

how should I sort it out? @gohmc

cp99says avatar Nov 15 '20 20:11 cp99says

I solved the problem downgrading my Node version to 10.23.0

JonanOribe avatar Jan 03 '21 18:01 JonanOribe

Try this, tested on node version to date (12.13.1):

  1. Update your swagger-express-mw:

"swagger-express-mw": "^0.7.0"

  1. Add swagger_params_parser to swagger_controllers at config/default.yaml ...

pipe for all swagger-node controllers

swagger_controllers:
  - onError: json_error_handler
  - cors
  - swagger_params_parser
  ...
  1. Run npm install
  2. Run swagger project start

I wish swagger-node will receive more timely update.

This fixes the error but routes are not working properly. For eg. 1) - GET /path/{id} 2) - POST /path/xyz

while making call to POST /path/xyz, it's throwing error method POST is not defined for /path/{id} It's treating xyz as some id

adarshsingh6162 avatar Nov 08 '21 15:11 adarshsingh6162

Wow this project is absolutely unmaintained. I experienced the same issue as all of the above.

Even the code that is auto-generated for the hello world controller is stale.

req.param('name') is the valid way to pull parameters from the request object now.

Now req.swagger.params.name.value

SteversIO avatar Nov 22 '21 21:11 SteversIO

I think I've figured out a way around this error with the following changes to the fittingsDir:

fittingsDirs: [ node_modules/swagger-node-runner/fittings ]

Then I had to remove this line under "swagger_controllers" to get it work, or else I get an error complaining about "onError" not being a fitting.

- onError: json_error_handler

I don't think, in my case, that I really need the json_error_handler. But tbh, I'm not 100% sure what it does. Does anyone know?

sscots avatar Jan 31 '22 15:01 sscots

  • onError: json_error_handler
    • cors
    • swagger_params_parser

Thank you! It`s works for me

mozhaev94 avatar Mar 27 '22 13:03 mozhaev94

Try this, tested on node version to date (12.13.1):

  1. Update your swagger-express-mw:

"swagger-express-mw": "^0.7.0"

  1. Add swagger_params_parser to swagger_controllers at config/default.yaml ...

pipe for all swagger-node controllers

swagger_controllers:
  - onError: json_error_handler
  - cors
  - swagger_params_parser
  ...
  1. Run npm install
  2. Run swagger project start

I wish swagger-node will receive more timely update.

Thanks alot, it's works for me.

I had the same issue with env:

Node -v: 16.16.0

npm -v: 8.11.0

CiriloGaspar avatar Jul 15 '22 10:07 CiriloGaspar