Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to tell prettier to use json parser instead of default javascript parser for package.json? #101

Closed
jangid opened this issue Jan 16, 2022 · 7 comments

Comments

@jangid
Copy link

jangid commented Jan 16, 2022

This is more a question than bug-report. I couldn't find any forum link so posting it here. Others might as well find this useful.

I could not find a configuration in Info manual for setting prettier to use JSON parser for package.json file. By default it is using some javascript parser which is producing error. Please add this section to manual. Also please add an answer here. Thanks.

@jangid jangid changed the title How to tell prettier to use json parser instead of javascript for package.json? How to tell prettier to use json parser instead of default javascript parser for package.json? Jan 16, 2022
@laampui
Copy link

laampui commented Feb 10, 2022

C-u M-x prettier-prettify ?

@jscheid
Copy link
Owner

jscheid commented Feb 10, 2022

I'll find some time to look at this soon. @jangid it sounds like a bug, it should use the correct parser by default. There's been commits to that effect before, but maybe something has changed in latest Prettier.

@jscheid
Copy link
Owner

jscheid commented May 23, 2022

@jangid I've just had a look -- this seems to be working for me. What output do you get when you switch to some package.json buffer and do M-: (prettier--parsers) RET? Should be (json-stringify json).

If that's not what you're getting, can you check that these parsers are enabled in your customization? (M-x customize-option prettier-enabled-parsers RET) and that you're using the latest version of this package?

@jangid
Copy link
Author

jangid commented May 23, 2022

The output of M-: (prettier--parsers) RET is (babel meriyah espree flow babel-flow). But when I check the value of variable prettier-enabled-parsers it shows (angular babel babel-flow babel-ts css elm espree flow graphql html java json json5 json-stringify less lua markdown mdx meriyah php postgresql pug python ruby scss sh solidity svelte swift toml typescript vue xml yaml). As you can see (json-stringify json) are part of it.

But i still get this error

/home/pankaj/work/moodraa/moodraa-next/package.json: SyntaxError: Unexpected token (2:12)
  1 | {
> 2 |   "private": true,
    |            ^

This works fine with CLI prettier. and the prettier-js package which spawns new process.

@jscheid
Copy link
Owner

jscheid commented May 23, 2022

Ah, I think I know what it is. You're using js-mode for your package.json. Use json-mode and it should work.

I know this isn't ideal, really it should be using the same parser that Prettier CLI uses regardless of mode. I'm planning to change that. Hope you can work around it for now using json-mode.

@jangid
Copy link
Author

jangid commented May 23, 2022

Yes. This worked. I'll use this workaround till this is fixed. Thanks a lot for investigating this and for the nice package.

@jscheid jscheid closed this as completed May 24, 2022
@rofrol
Copy link

rofrol commented Aug 5, 2022

Just installing https://github.com/joshwnj/json-mode solved the problem.

Not sure if this will be needed (add-to-list 'auto-mode-alist '("\\.json\\'" . json-mode)) in some case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants