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

Error: Module parse failed: Unexpected token #53

Closed
MshHooman opened this issue Feb 28, 2023 · 3 comments
Closed

Error: Module parse failed: Unexpected token #53

MshHooman opened this issue Feb 28, 2023 · 3 comments
Assignees

Comments

@MshHooman
Copy link

Hi,

I'm trying to use the package but faced this error when I want to start the project.

important dependencies:

"@babel/core": "7.12.3",
"@lukemorales/query-key-factory": "^1.1.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"@tanstack/react-query": "^4.24.4",
"webpack": "4.44.2",

plus that the project is ejected from CRA.

source code:

import { createQueryKeys } from '@lukemorales/query-key-factory';
export const usersKeys = createQueryKeys('users');

Error:

./node_modules/@lukemorales/query-key-factory/dist/index.mjs 67:37
Module parse failed: Unexpected token (67:37)
File was processed with these loaders:
 * ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|         } : void 0)
|       },
>           a = [...n, ...(r.queryKey ?? [])],
|           y = {
|         queryKey: a,

do you have any idea about what loader should I import or what makes this problem?

@lukemorales
Copy link
Owner

@MshHooman it seems that it's not being able to compile the ?? operator? If that's really the case, you can try to use this loader: https://babeljs.io/docs/babel-plugin-proposal-nullish-coalescing-operator

@gbyesiltas
Copy link

gbyesiltas commented Apr 28, 2023

I have the same issue on Nuxt 2, error points to the same line. I tried adding the babel plugin but may not be doing it correctly because it didn't work. I'll try to figure it out and update you guys here

Update: could be related to the nuxt issue: nuxt/nuxt#6688

Update 2: indeed, using the solution from the aforementioned nuxt issue worked for me. I needed to add the following to nuxt.config

build: {
// ... 
extend(config, _) {
      // transpile .mjs too (see: https://github.com/nuxt/nuxt/issues/6688)
      const babelRule = config.module.rules.find(
        (rule) => `${rule.test}` === "/\\.jsx?$/i"
      );
      if (babelRule) {
        babelRule.test = /\.m?jsx?$/i;
      }
    },
    transpile: [({ isLegacy }) => isLegacy && "@lukemorales/query-key-factory"],
}

@lukemorales
Copy link
Owner

@gbyesiltas thanks for sharing the fix!

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

3 participants