fastify / fastify/fastify-cli

generate-swagger doesn't work with typescript code

Open
#608 6 comments 2 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
733
Forks
177
Avg merge
1d 20h
Merged PRs (30d)
3

Description

### Prerequisites

- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the bug has not already been reported

### Fastify version

4.15.0

### Plugin version

_No response_

### Node.js version

v19.7.0

### Operating system

macOS

### Operating system version (i.e. 20.04, 11.3, 10)

Monterey (12.5.1)

### Description

I'm trying to generate swagger using fastify-cli for a Typescript project and getting an error `SyntaxError: Cannot use import statement outside a module`.

### Steps to Reproduce

1. Create a new Fastify project from template:
```
$ fastify generate --lang=ts test-project
```

2. Run `npm install`

4. Add `@fastify/swagger` - `npm install @fastify/swagger`

5. In the `src/app.ts` add:
```ts
+ import swagger from '@fastify/swagger';

// [skipped]

const app: FastifyPluginAsync = async (
fastify,
opts
): Promise => {
// Place here your custom code!
+ fastify.register(swagger, {});

// [skipped remaining]
```

6. Try generating swagger:
```
$ fastify generate-swagger src/app.ts
```

7. Observe the error
```
import { join } from 'path';
^^^^^^

SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1195:20)
at Module._compile (node:internal/modules/cjs/loader:1239:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
at Module.load (node:internal/modules/cjs/loader:1133:32)
at Module._load (node:internal/modules/cjs/loader:972:12)
at Module.require (node:internal/modules/cjs/loader:1157:19)
at require (node:internal/modules/helpers:119:18)
at requireServerPluginFromPath (/Users/anton/.nvm/versions/node/v19.7.0/lib/node_modules/fastify-cli/util.js:79:20)
at async runFastify (/Users/anton/.nvm/versions/node/v19.7.0/lib/node_modules/fastify-cli/generate-swagger.js:60:12)
```

### Expected Behavior

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.