Skip to content

SyntaxError: Illegal return statement #3995

@lalexdotcom

Description

@lalexdotcom

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.0.0

Plugin version

No response

Node.js version

16.x

Operating system

macOS

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

12.4

Description

It seems that the generated file error-serializer.jsis not valid Javascript. The last line of the file is a return statement outside of a function.

SyntaxError: Illegal return statement
    at Object.../../node_modules/fastify/lib/error-serializer.js
[...]

Manually commenting the last line allow to launch even with Webpack.

When I run my code using ts-node, no error is done. When the project is bundled with Webpack, one way or another, the whole code is evaluated, and the SyntaxError is thrown by node...

I definitly have no idea why Webpack is evaluating this code so soon (I'm not familiar enough with its "magic"), but anyway, returning outside of a function is no valid JS and this Error could be thrown later in the code I guess...

Steps to Reproduce

Main script

import fastify from 'fastify';

(async () => {
    console.log('Start CLI !');

    const fast = fastify({ logger: true });
    await fast.listen({ port: 15987 });
})();

Webpack config (both ts-loader or swc-loader throw the error)

const config: Configuration = {
    mode: 'development',
    target: 'node16',
    entry: './main.ts',
    context: FOLDER,
    module: {
        rules: [
            {
                test: /\.ts$/,
                exclude: /(node_modules)/,
                use: {
                    loader: 'swc-loader',
                    options: {
                        jsc: {
                            parser: {
                                syntax: 'typescript',
                            },
                            target: 'es2016',
                        },
                    },
                },
            },
            // {
            //     test: /\.ts$/,
            //     exclude: /(node_modules)/,
            //     use: {
            //         loader: 'ts-loader',
            //     },
            // },
        ],
    },
    resolve: {
        extensions: ['.ts', '.tsx', '.js', '.jsx'],
    },
};

Expected Behavior

No error 0:-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions