swagger-api/swagger-node

Error: Cannot find module swagger_router

TomlDev opened this issue ยท 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

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.

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

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

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....

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

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.

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

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);

Happens to me as well on node 12

Ethns commented

Same issue
env: node 12.13.1 npm 6.12.1

gohmc commented

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 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.

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

it's work for me

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.

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

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

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

I solved the problem downgrading my Node version to 10.23.0

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

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

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?

  • onError: json_error_handler
    • cors
    • swagger_params_parser

Thank you! It`s works for me

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