prettier-vscode: TypeError: t.match is not a function

Summary

When running Prettier in VSCode, the following exception is thrown:

["INFO" - 1:43:42 PM] Loaded module 'prettier@1.17.0' from 'C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier'
["INFO" - 1:43:42 PM] Using ignore file (if present) at d:\Documents\Projects\moorecodes\.prettierignore
["INFO" - 1:43:42 PM] File Info:
{
  "ignored": false,
  "inferredParser": "typescript"
}
["INFO" - 1:43:42 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 1:43:42 PM] Prettier Options:
{
  "arrowParens": "avoid",
  "bracketSpacing": true,
  "endOfLine": "auto",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 80,
  "proseWrap": "preserve",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 2,
  "trailingComma": "none",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "d:\\Documents\\Projects\\moorecodes\\src\\SomeProject\\Header\\index.tsx",
  "parser": "typescript"
}
["INFO" - 1:43:42 PM] Loaded module 'prettier@1.17.0' from 'C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier'
["INFO" - 1:43:42 PM] Formatting using 'prettier-tslint'
["ERROR" - 1:43:42 PM] Error formatting document.
t.match is not a function
TypeError: t.match is not a function
	at Object.e.extract (C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier\parser-babylon.js:1:1119)
	at Object.h [as hasPragma] (C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier\parser-babylon.js:1:2691)
	at format (C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier\index.js:14030:63)
	at formatWithCursor (C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier\index.js:14117:12)
	at C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier\index.js:42518:15
	at format (C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier\index.js:42537:12)
	at c:\Users\nate2\.vscode\extensions\esbenp.prettier-vscode-3.13.0\dist\extension.js:1:352591
	at t.default.safeExecution (c:\Users\nate2\.vscode\extensions\esbenp.prettier-vscode-3.13.0\dist\extension.js:1:353582)
	at t.default.<anonymous> (c:\Users\nate2\.vscode\extensions\esbenp.prettier-vscode-3.13.0\dist\extension.js:1:352554)
	at Generator.next (<anonymous>)
	at s (c:\Users\nate2\.vscode\extensions\esbenp.prettier-vscode-3.13.0\dist\extension.js:1:347571)
["INFO" - 1:43:42 PM] Formatting completed in 17.5141ms.

This happens with both auto-format on save and when running the format command.

VS Code Version:

Version: 1.41.0 (system setup)
Commit: 9579eda04fdb3a9bba2750f15193e5fafe16b959
Date: 2019-12-11T18:37:42.077Z
Electron: 6.1.5
Chrome: 76.0.3809.146
Node.js: 12.4.0
V8: 7.6.303.31-electron.0
OS: Windows_NT x64 10.0.18362

Prettier Extension Version:

Name: Prettier - Code formatter
Id: esbenp.prettier-vscode
Description: Code formatter using prettier
Version: 3.13.0
Publisher: Esben Petersen
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode

OS and version:

Windows 10

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 18
  • Comments: 26 (11 by maintainers)

Most upvoted comments

I have changed my current node version to 10.17.0 and the prettier vscode extension works!.

I shouldn’t have to downgrade node all the way to 10.17.0

I just provide an alternative workaround or a point of reference to track better the issue, In the meantime, if you have a better solution I will love to know it.

You can downgrade to the previous version (3.12.0) from VSCode which will revert the breaking changes introduced in 3.13.0.

image

You have to reload the window (or restart VSCode) for the changes to take effect.

I think i fixed it, try updating to 3.14.0. I accidentally left a hard-coded string in that was supposed to be a variable. See: https://github.com/prettier/prettier-vscode/blob/69af3e2713c0e0411b0e1deaf18bfd21dca220c1/src/ModuleResolver.ts#L148

Not sure how tests didn’t get this. I need to investigate, but don’t have time right now. My wife gave birth to our second kid last night so just doing a bit of work while they are both sleeping. 😉

Let me at least know that this fixed the problem.

I have changed my current node version to 10.17.0 and the prettier vscode extension works!.

Getting the same all the time on macos since recenly, could be related to bumping VSCode to 1.41.0

Great. I’m going to leave this open so I can write some tests later, but glad it is working again.

@ntotten I reinstalled prettier globally and reproduced the bug in prettier-vscode@3.13.0 again. After upgrading to prettier-vscode@3.14.0 and reloading VSCode, the error disappeared 🎉

All works, many thanks for your quick reaction and for cutting a new release! 🙌

Okay I looked into the source code and I am pretty sure the error is coming from the most recent commit 3b2030104a4719779e37e5d8ac000cf46aa6a9b8 here:

https://github.com/prettier/prettier-vscode/blob/69af3e2713c0e0411b0e1deaf18bfd21dca220c1/src/ModuleResolver.ts#L139-L155

I dont think the new ModuleResolver is returning a moduleInstance now and when it gets called here it throws the TypeError: r is not a function https://github.com/prettier/prettier-vscode/blob/69af3e2713c0e0411b0e1deaf18bfd21dca220c1/src/PrettierEditService.ts#L340-L348

@ntotten Same thing for me, reinstalled globally, 3.13 still had the issue, upgraded to 3.14 and the problem was solved. Thanks for fixing quickly and congrats on the second child! 🎉

@ntotten I think the issue is that the new update is somehow forcing the extension to load the global package, look at these logs:

// here it finds and loads the local prettier module
["INFO" - 12:20:09 PM] Loaded module 'prettier@1.19.1' from '/Users/tjlang/projects/ledger-app/ledger/node_modules/prettier/index.js'

["INFO" - 12:20:19 PM] Formatting /Users/tjlang/projects/ledger-app/ledger/sagas/auth.js
["INFO" - 12:20:19 PM] Loaded module 'prettier@1.19.1' from '/Users/tjlang/projects/ledger-app/ledger/node_modules/prettier/index.js'
["INFO" - 12:20:19 PM] Using ignore file (if present) at /Users/tjlang/projects/ledger-app/ledger/.prettierignore
["INFO" - 12:20:19 PM] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 12:20:19 PM] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 12:20:19 PM] Prettier Options:
{
  "filepath": "/Users/tjlang/projects/ledger-app/ledger/sagas/auth.js",
  "parser": "babel",
  "semi": false,
  "trailingComma": "all",
  "singleQuote": true,
  "printWidth": 100,
  "tabWidth": 4
}

// now here it loads the global prettier module
["INFO" - 12:20:19 PM] Loaded module 'prettier@1.19.1' from '/usr/local/lib/node_modules/prettier'

["INFO" - 12:20:19 PM] Formatting using 'prettier-eslint'
["ERROR" - 12:20:19 PM] Error formatting document.

It initially finds the local package but after getting all of the configs it loads the global package.

The fix for now is to run npm rm -g prettier like @kachkaev mentioned. This forces the extension to use the local package and it then formats correctly.

So somehow it is trying to load the global package and returning undefined somewhere even if the local package exists…

@kachkaev Can you paste your prettier output log.

The Failed to load module error should only happen when prettier is found in the package.json but you haven’t run npm install or yarn, but I am taking it that you have does those and the module is there, but still it isn’t finding it.

I have changed my current node version to 10.17.0 and the prettier vscode extension works!.

I shouldn’t have to downgrade node all the way to 10.17.0

I just provide an alternative workaround or a point of reference to track better the issue, In the meantime, if you have a better solution I will love to know it.