question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected token 'var'

See original GitHub issue

I am trying to comment on a PR via a workflow and keep getting this exception:

SyntaxError: Unexpected token 'var'
    at new AsyncFunction (<anonymous>)
    at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4942:56)
    at main (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4997:26)
    at Module.272 (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4981:1)
Error: Unhandled error: SyntaxError: Unexpected token 'var'
    at __webpack_require__ (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:24:31)
    at startup (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:43:19)
    at /home/runner/work/_actions/actions/github-script/v5/dist/index.js:49:18
    at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:52:10)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)

The code is really simple

      - name: Comment on PR
        uses: actions/github-script@v5
        if: failure()
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: "foo"
            })

Github support asked me to log a bug report here as well.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
joshmgrosscommented, Jan 7, 2022

Ok, I think the actual problem is with using a multiline output from a previous step’s stdout as the body in createComment.

If you need to pass into multi-line outputs into the script block, you’ll likely need to pass those values in as environment variables. Otherwise, newlines will be interpreted literally in the script and won’t be valid JavaScript.

Example:

- uses: actions/github-script@v5
  env: 
    MULTILINE_VALUE: ${{ steps.my_step.outputs.value }}
  with:
    script: |
        github.issues.createComment({
          issue_number: context.issue.number,
          owner: context.repo.owner,
          repo: context.repo.repo,
          body: process.env.MULTILINE_VALUE
        })
1reaction
salecharohitcommented, Feb 19, 2022

So I encountered this same issue and was able to resolve it using below as per @joshmgross’s recommendation

  - name: Plan Output
    uses: actions/github-script@v6
    if: github.event_name == 'pull_request'
    env:
      PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
    with:
      github-token: ${{ secrets.GITHUB_TOKEN }}
      script: |
        const output = `## Terraform Prod Infra Plan
        #### Terraform Initialization \`${{ steps.init.outcome }}\`
        #### Terraform Validation \`${{ steps.validate.outcome }}\`
        #### Terraform Plan \`${{ steps.plan.outcome }}\`
        
        <details><summary>Show Plan</summary>
        ${process.env.PLAN}
        </details>

        *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;

        github.rest.issues.createComment ({
          issue_number: context.issue.number,
          owner: context.repo.owner,
          repo: context.repo.repo,
          body: output
        })
Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught SyntaxError: Unexpected token var - Stack Overflow
I have an error Uncaught SyntaxError: Unexpected token var displayed between (1) and (2) its a very odd error and it doesn't make...
Read more >
What does "Syntax error: Unexpected token var" mean?
Given the error you received it seems most likely that you tried to declare a variable incorrectly. My guess is that you might...
Read more >
JavaScript Error Handling: Unexpected Token - GeeksforGeeks
Not follow them throws an error.An unexpected token occurs if JavaScript code has a missing or extra character { like, ) + –...
Read more >
SyntaxError: Unexpected token - JavaScript - MDN Web Docs
The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided.
Read more >
uncaught syntaxerror - unexpected token 'var' - Syncfusion
uncaught syntaxerror - unexpected token 'var' · Hi · Due to the critical need of reported bug 'Component not rendering properly in multi...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found