0

I'm pretty new to coding & using VS Code / Prettier I'm trying to turn on strict mode in my JS code, using "use strict";

when I save my file, Prettier formats the code from "use strict" to ('use strict');

which then as far as I can see means strict mode isn't initialised. I couldn't find anyone else talking about this problem online anywhere, so wonder if anyone could shed any light on it?

Dilan
  • 2,610
  • 7
  • 23
  • 33
jckadms
  • 3
  • 4

1 Answers1

5

Assuming you have tried with a recent version of Prettier, this indicates you have put "use strict"; at a wrong place.

"use strict"; must appear before any code in the script, or when it is used within a function, it must appear before any code in the body of that function.

If you put "use strict"; anywhere else, Prettier will add the parentheses.

You can reproduce this behaviour on the Prettier playground.

which then as far as I can see means strict mode isn't initialised

True. It must be a plain string literal, and ("use strict") violates that rule.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    I found that I had to include the semi-colon after the string too. Even if 'use strict' was on the first line of the file. Adding the semi-colon fixed the issue for me. – Stephen Cooper Mar 03 '22 at 15:08