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.

SyntaxError: unknown: 'with' in strict mode

See original GitHub issue

Bug Report

  • I would like to work on a fix!

Current behavior

SyntaxError: unknown: ‘with’ in strict mode (1:27)

Input Code

const babel = require('@babel/core');

var code = 'function f(foo, values) {  with (foo) {   }  }'

var res = babel.transform(code, {
   retainLines: true,
   compact: false,
   presets: ['@babel/preset-env'],
   plugins: [
    ["@babel/plugin-transform-modules-commonjs", { "strictMode": false }]
  ]
}).code;

Expected behavior

The instrumentation should succeed.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js
None

Environment

None
  • Babel version(s): 7.0.0
  • Node/npm version: 15.2.0
  • OS: [e.g. OSX 10.13.4, Windows 10]
  • Monorepo: No
  • How you are using Babel: I used the above code

Possible Solution

Additional context

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gogo9thcommented, Nov 24, 2020

I think you meant:

You could use sourceType: "unambiguous", which means "if there are import/export it’s sourceType: "module", otherwise it’s sourceType: "script". This does solve the problem perfectly. Thanks a lot!

0reactions
nicolo-ribaudocommented, Nov 24, 2020

Well, if you are using import or export it’s going to crash, because they are only allowed in modules.

You could use sourceType: "unambiguous", which means "if there are import/export it’s sourceType: "unambiguous", otherwise it’s sourceType: "module".

I think that this will work in 99.9% of the cases (there are some differences between modules and scripts, but usually the problem is only when going from script to module).

Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: unknown: interface is a reserved word in strict ...
SyntaxError : unknown: interface is a reserved word in strict mode. Is this expected behaviour? How can I get this to compile? I...
Read more >
Strict mode - JavaScript - MDN Web Docs
Strict mode makes with a syntax error, so there's no chance for a name in a with to refer to an unknown location...
Read more >
JavaScript Mistakes — Strict Mode | by John Au-Yeung
Strict mode eliminates lots of silent errors by changing them to throw ... SyntaxError: Unexpected eval or arguments in strict mode' when we ......
Read more >
Strict mode
Strict mode makes with a syntax error, so there's no chance for a name in a with to refer to an unknown location...
Read more >
Strict mode - Angular
Enables strict mode in TypeScript, as well as other strictness flags recommended by the TypeScript team. · Turns on strict Angular compiler flags...
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