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

Problem with Nuxt3: Unexpected token 'export' #333

Closed
lukaszflorczak opened this issue Oct 20, 2021 · 7 comments
Closed

Problem with Nuxt3: Unexpected token 'export' #333

lukaszflorczak opened this issue Oct 20, 2021 · 7 comments

Comments

@lukaszflorczak
Copy link

lukaszflorczak commented Oct 20, 2021

Describe the bug
The vue-fontawesome (3.0.0-5) doesn't work with Nuxt3 (3.0.0-27244261.c49f9ca, the same on earlier releases too).

Zrzut ekranu 2021-10-20 o 08 10 20

(node:27545) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
Unexpected token 'export'
  at Object.compileFunction (node:vm:352:18)  
  at wrapSafe (node:internal/modules/cjs/loader:1031:15)  
  at Module._compile (node:internal/modules/cjs/loader:1065:27)  
  at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)  
  at Module.load (node:internal/modules/cjs/loader:981:32)  
  at Function.Module._load (node:internal/modules/cjs/loader:822:12)  
  at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:196:29)  
  at ModuleJob.run (node:internal/modules/esm/module_job:183:25)  
  at async Loader.import (node:internal/modules/esm/loader:178:24)  
  at async __instantiateModule__ (file://./.nuxt/dist/server/server.mjs:2253:3)

Reproducible test case
Clone and run project (branch test/fontawesome): https://github.com/lukaszflorczak/nuxt3-demo/tree/test/fontawesome

npm install
npm run dev

Visit http://localhost:3000

@lukaszflorczak lukaszflorczak changed the title Problem with Nuxt3: To load an ES module, set "type": "module" in the package.json Problem with Nuxt3: Unexpected token 'export' Oct 20, 2021
@mateuszgiza
Copy link

@lukaszflorczak
I've found a temporary solution. Rename the plugin to fontawesome.client.js, and remove it from nuxt.config.js (Nuxt3 automatically loads it).
My Nuxt plugin seams like that:

Please note that it might work only on 'client' side, it might throw an error on 'server' therefore SSR might not fully work.

// tsconfig.json
{
  "extends": "./.nuxt/tsconfig.json",
  "paths": {
    "font-awesome": "./node_modules/@fortawesome/fontawesome-svg-core"
  }
}
// nuxt.config.ts
// removed from css/plugins sections
...
alias: {
    'font-awesome': resolve(__dirname, './node_modules/@fortawesome/fontawesome-svg-core')
},
...
purgeCSS: {
    whitelistPatterns: [/svg.*/, /fa.*/]
},
// app.vue
...
<script lang="ts" setup>
import 'font-awesome/styles.css'
import './assets/css/tailwind.css'
</script>
// plugins/fontawesome.client.js

import { defineNuxtPlugin } from '#app'
import { library, config } from '@fortawesome/fontawesome-svg-core'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { fas } from '@fortawesome/free-solid-svg-icons'

export default defineNuxtPlugin(nuxtApp => {
    config.autoAddCss = false;
    library.add(fas);

    nuxtApp.vueApp.component('font-awesome-icon', FontAwesomeIcon);
})

@lukaszflorczak
Copy link
Author

lukaszflorczak commented Oct 28, 2021

I tried it today again, with nuxt3@3.0.0-27255771.f78ec93 and it works without any hacks (the plugin is loaded in SSR mode). @mateuszgiza could you confirm it too?

My code/implementation looks like this – maybe it'll be useful for someone:

// nuxt.config.ts

import { defineNuxtConfig } from 'nuxt3'

export default defineNuxtConfig({
  css: [
    '@fortawesome/fontawesome-svg-core/styles.css'
  ]
})
// plugins/icons.js

import { defineNuxtPlugin } from '#app'
import { config } from '@fortawesome/fontawesome-svg-core'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'

config.autoAddCss = false

export default defineNuxtPlugin(nuxtApp => {
  nuxtApp.vueApp.component('fa-icon', FontAwesomeIcon)
})
// composables/icons.js

import { library } from '@fortawesome/fontawesome-svg-core'

export const useIcons = icons => {
  for (const [key, value] of Object.entries(icons)) {
    if (/^fa/.test(key)) library.add(value)
  }
}
<!-- In component -->

<template>
  <fa-icon :icon="['fas', 'heart']"/>
</template>

<script setup>
  import { faHeart } from '@fortawesome/pro-solid-svg-icons'

  useIcons({ faHeart })
</script>

@mateuszgiza
Copy link

@lukaszflorczak thank you. It seems to be working now 🎉

@eMeRiKa13
Copy link

@mateuszgiza and @lukaszflorczak is-it still working for you?

I have a 500 error:

[Vue warn]: Unhandled error during execution of render function 
  at <FontAwesomeIcon icon= [ 'fas', 'browser' ] >
Cannot read properties of undefined (reading 'icon')

With

"nuxt3": "3.0.0-27296423.f3082ca"
"@fortawesome/fontawesome-svg-core": "^1.3.0-beta3",
 "@fortawesome/free-brands-svg-icons": "^6.0.0-beta3",
"@fortawesome/pro-solid-svg-icons": "^6.0.0-beta3",
"@fortawesome/vue-fontawesome": "^2.0.6",

@eMeRiKa13
Copy link

I switched to your versions and it's now working!

"@fortawesome/fontawesome-free": "^5.15.4",
"@fortawesome/fontawesome-svg-core": "^1.2.36",
"@fortawesome/free-brands-svg-icons": "^5.15.4",
"@fortawesome/vue-fontawesome": "^3.0.0-5"

@lukaszflorczak
Copy link
Author

lukaszflorczak commented Nov 26, 2021

Yep, vue-fontawesome has to be in the 3.x version. And probable 6.0.0-beta icons are still incompatible.

@Tragio
Copy link

Tragio commented Feb 11, 2022

Is anyone facing the same problem with nuxt-bridge? I'm having the same problem. I created a minimal reproduction here #344

"@fortawesome/fontawesome-svg-core": "^1.3.0",
"@fortawesome/free-solid-svg-icons": "^6.0.0",
"@fortawesome/vue-fontawesome": "^2.0.6",

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