webpack: Unexpected token error at code with optional chaining when using Typescript
UPD for everyone who will find this issue. For fix this error acornjs/acorn#891 and acornjs/acorn#890 must be merged.
UPD2 we all awaiting while estree/estree/pull/204 will be merged
UPD3 fix for optional chaining landed into acorn 7.3.0 but webpack@4 uses acorn 6.x. so fix of this issue will be in webpack@5: https://github.com/webpack/webpack/issues/10227#issuecomment-642734920
Temporary fixes
For Babel
Add @babel/plugin-proposal-optional-chaining
and @babel/plugin-proposal-nullish-coalescing-operator
plugins in your config
For Typescript
Do not upgrade to version 3.8 or set compiler option target
to es2019
Yarn/NPM level
See the https://github.com/webpack/webpack/issues/10227#issuecomment-698662228
Do you want to request a feature or report a bug? I don’t know is it a bug or feature
What is the current behavior?
Get error “Unecpected token”
What is the expected behavior?
Build bundle successfull
Description
When I set in tsconfig.json
target: "ESNext"
TS keeps my code with optional chaining (and nullish coalescing) as is. But I’ve got an error
Module parse failed: Unexpected token (2:10)
File was processed with these loaders:
* ./node_modules/ts-loader/index.js
You may need an additional loader to handle the result of these loaders.
| var a = { b: 2 };
> var c = a?.b;
I made a minimum reproduce repo inoyakaigor/webpack-typescript-optional-chaining
How I can avoid this error?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 48
- Comments: 19 (3 by maintainers)
Links to this issue
Commits related to this issue
- Fix error on optional chaining ref: webpack/webpack/issues/10227 — committed to crowi/crowi by sotarok 4 years ago
- fix: webpack 4 don't support optional chanining https://github.com/webpack/webpack/issues/10227 — committed to linonetwo/Rising-of-the-Eternity by linonetwo 4 years ago
- Force enable nullish-coalescing plugin [publish] This plugin can be auto-disabled in case of modern browsers target, but will break webpack @4 due to https://github.com/webpack/webpack/issues/10227 ... — committed to JetBrains/babel-preset by andrey-skl 4 years ago
- Update "@jetbrains/babel-preset-jetbrains" to 2.2.3 to avoid non-transpiling nullish coalescing operator if new browsers are targeted Nullish coalescing operator must be transpiled when webpack@4 use... — committed to JetBrains/ring-ui by andrey-skl 4 years ago
- fix: support new syntax in `webpack@4` https://github.com/webpack/webpack/issues/10227 — committed to ClarkSource/ember-auto-import-typescript by buschtoens 4 years ago
- fix: support new syntax in `webpack@4` (#219) https://github.com/webpack/webpack/issues/10227 — committed to ClarkSource/ember-auto-import-typescript by buschtoens 4 years ago
- Fix `rooms.html` jQuery dependency by restructuring The jQuery-dependent code (i.e. that part requiring access to the `bootstrapTable` functions) has been moved into a proper typescript module. This ... — committed to agdsn/pycroft by lukasjuhrich 3 years ago
- Add explict Babel loader for optional chaining. This is required for consumers of this library that use Webpack 4. See https://github.com/webpack/webpack/issues/10227 for more info. Presumably, the ... — committed to mubi/lrud by simontsang 3 years ago
- Add explict Babel loader for optional chaining. This is required for consumers of this library that use Webpack 4. See https://github.com/webpack/webpack/issues/10227 for more info. Presumably, the ... — committed to mubi/lrud by simontsang 3 years ago
- fix: revert TS target to ES2019 to support Webpack 4 For more info about the problem when using ES2020 see https://github.com/webpack/webpack/issues/10227 — committed to vaadin/hilla by Haprog 3 years ago
- fix: revert TS target to ES2019 to support Webpack 4 For more info about the problem when using ES2020 see https://github.com/webpack/webpack/issues/10227 — committed to vaadin/hilla by Haprog 3 years ago
- fix: fix issue with optional chaining https://github.com/webpack/webpack/issues/10227 — committed to screfy/cf-worker-template by screfy 3 years ago
- part of ideogram js optional chaining token / webpack 4 error see https://github.com/webpack/webpack/issues/10227 — committed to NIAGADS/GenomicsDBWebsite by fossilfriend 2 years ago
- fix: upgrade storybook & use webpack 5 with storybook this change fixes optional chaining issue which was fixed in acord@v8 and that is fixed in webpack 5 reference link: https://github.com/webpack/... — committed to pangolindex/components by SarjuHansaliya 2 years ago
- fix: upgrade storybook & use webpack 5 with storybook (#182) this change fixes optional chaining issue which was fixed in acord@v8 and that is fixed in webpack 5 reference link: https://github.com... — committed to pangolindex/components by SarjuHansaliya 2 years ago
- chore: Set compilation target to es2019 Sets The compilation target of the repo to 2019 for webpack 4 compat webpack/webpack#10227 which does not support optional chaining without extra babel loader ... — committed to ling1726/fluentui-contrib by ling1726 a year ago
- fix unexpected token caused by webpack webpack did not recognize optional chaining token (?.) https://github.com/webpack/webpack/issues/10227#issuecomment-653408484 — committed to beekeeper-studio/beekeeper-studio by azmy60 5 months ago
It looks like you just deleted our lovely crafted issue template. It was there for good reasons. Please help us solving your issue by answering the questions asked in this template. I’m closing this. Please either update the issue with the template and reopen, or open a new issue.
@evilebottnawi Closing the issue because I removed the template looks like excessive bureaucracy.
However, I updated the description.
@evilebottnawi Can we reopen this issue? Since this is a unresolved problem, it will help people find answers and let people track the progress.
Good news everyone! Acorn 7.3.0 with optional chaining support just released 2 hours ago! https://github.com/acornjs/acorn/blob/master/acorn/CHANGELOG.md
Maybe I am missing something, but doing this did not fix the issue for me. I still get the same unexpected token at each use of optional chaining I have in my code.
EDIT: After investigation, I came to understand you are using Yarn, but I am not. So the resolutions section wouldn’t be considered by npm alone. I could fix the problem with npm by installing https://www.npmjs.com/package/npm-force-resolutions, and then adding the following to my package.json:
Based on @sboisse temporary solution, in Yarn we just need resolution part,
After that, just run
yarn install
.as a temporary solution; https://github.com/OnurGvnc/acorn-with-stage3
package.json
:Yes, it will be part of webpack@5
Unfortunately webpack 4 uses acorn 6.x - not sure if we’ll get this before webpack 5. Fingers crossed!
@evilebottnawi looks like @inoyakaigor updated the description and also provided a reproduction repo. I think we’re hitting this same issue with optional chaining.
The world has moved onto to use optional chaining in npm packages. Like people have said this has to do with a limitation with builds using webpack versions 4 or earlier.
To resolve this issue for legacy builds that use webpack v4, add
@babel/plugin-proposal-optional-chaining
to your babel plugins and set the resolution foracorn
package to7.3.0
or later.FYI to others that are considering using the
acorn-with-stage3
solution https://github.com/webpack/webpack/issues/10227#issuecomment-653408484 just be aware that it breaks the built-in Split Chunks: Dynamic Imports. Presumably because the AST generated by acorn v7 (which acorn-with-stage3 uses) is different enough that webpack v4 cannot find theimport()
nodes to perform the splitting.I’m getting the same error-
tsconfig.json-