SyntaxError: Invalid or unexpected token when run nestjs

Viewed 32

When I run this nestjs code, I encounter an error:

SyntaxError: Invalid or unexpected token

what is the reason?

import {Controller, Get, Bind, Req, Post} from '@nestjs/common';

@Controller('cats')
export class catsController {
    @Post()
    create() {
        return "this is a action 1ss"
    }
    @Get()
    @Bind(Req())
    findAll(request) {
        return "this is a action";
    }
}
1 Answers

If you are just using JavaScript, not Typescript, you need to have a babel config like the following

// .babelrc
{
  "presets": ["@babel/preset-env"],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    [
      "@babel/plugin-transform-runtime",
      {
        "regenerator": true
      }
    ]
  ]
}

And then use babel-node instead of node so that babel can properly interpret the decorators.

The docs even mention this on the getting started page

Nest takes advantage of the latest language features, so to use it with vanilla JavaScript we need a Babel compiler.

Related