问题
I installed Swagger CodeGen using the command
npm install swagger-codegen -g
So I should have it installed, but there is some error/glitch going on. Everytime I enter
swagger-codegen -v
I get the following message
'swagger-codegen' is not recognized as an internal or external command, operable program or batch file.
I have no idea what to do. How should I fix this? I am trying to follow a tutorial, but the instructor nevers shows how he installs programs.
Under GitHub, the instructions don't make sense
Prerequisites If you're looking for the latest stable version, you can grab it directly from Maven.org (Java 7 runtime at a minimum):
wget http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.3.1/swagger-codegen-cli-2.3.1.jar -O swagger-codegen-cli.jar
java -jar swagger-codegen-cli.jar help
For Windows users, you will need to install wget or you can use Invoke-WebRequest in PowerShell (3.0+), e.g.
Invoke-WebRequest -OutFile swagger-codegen-cli.jar http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.3.1/swagger-codegen-cli-2.3.1.jar
Am I supposed to put those in the command line? tried that, didn't work
回答1:
Swagger Codegen is a Java app, so you must have Java installed.
There are two versions of Swagger Codegen - 2.x and 3.x. Use 3.x for openapi: 3.0.0
definitions and 2.x for swagger: '2.0'
.
Installing Swagger Codegen CLI (Command-Line Version)
Windows
Download the compiled JAR file:
Codegen 3.x JARs are here: https://oss.sonatype.org/content/repositories/releases/io/swagger/codegen/v3/swagger-codegen-cli/
Codegen 2.x JARs are here: http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/
Look for the file named swagger-codegen-cli-<version>.jar
, e.g. if you need v. 2.3.1:
http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.3.1/swagger-codegen-cli-2.3.1.jar
To run the codegen, use:
java -jar c:\path\to\swagger-codegen-cli-<version>.jar <args>
(You can rename swagger-codegen-cli-<version>.jar
to swagger-codegen-cli.jar
for simplicity.)
Mac
Install using Homebrew:
# Codegen 3.x
brew install swagger-codegen
# Codegen 2.x
brew install swagger-codegen@2
Then you can run the codegen as:
swagger-codegen <args>
swagger-codegen@2 <args>
If you don't have Homebrew, download the JAR file from Maven and run it as explained above for Windows.
Alternatives to CLI Version
You can also:
- generate the server and client code from Swagger Editor (it has an integrated codegen).
- call the online codegen using its REST API. (Example)
- use the Maven plugin: swagger-codegen-maven-plugin. The group ID is
io.swagger
for Codegen 2.x andio.swagger.codegen.v3
for Codegen 3.x.
回答2:
this module doesn't create an executable file when being installed via npm install swagger-codegen -g
, that's why you see the error. Please see https://github.com/swagger-api/swagger-codegen/blob/master/README.md#prerequisites for installation instructions
来源:https://stackoverflow.com/questions/51713856/trouble-installing-swagger-codegen