Font-Awesome
Font-Awesome copied to clipboard
package doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix.
Describe the bug
@fortawesome/fontawesome-free doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix.
I'm using FontAwesome in my sveltekit app, and using vite v2.7.3. There I get this error message
To Reproduce
- Create a new sveltekit project:
npm init svelte@next my-app
cd my-app
npm install
- add @fortawesome/fontawesome-free as a dependency
- run
npm run build
Expected behavior The error should not show up and it should be a valid es module
Version and implementation
Version:
"@fortawesome/fontawesome-free": "^5.15.4",
"@sveltejs/kit": "1.0.0-next.202"
vite v2.7.3
- [x] SVG with JS
- [ ] Web Fonts with CSS
- [ ] SVG Sprites
- [ ] On the Desktop
Bug report checklist
- [x] I have filled out as much of the above information as I can
- [x] I have included a test case because my odds go way up that the team can fix this when I do
- [x] I have searched for existing issues and to the best of my knowledge this is not a duplicate
same here.
this is when using vitejs
I had the same problem after installing it on a fresh svelte-kit/electron template (https://github.com/Dax89/electron-sveltekit).
I have multiple projects getting this same warning.
Me too. I also have problems with the icon
method.
import { icon } from "@fortawesome/fontawesome-svg-core";
^^^^
SyntaxError: Named export 'icon' not found. The requested module '@fortawesome/fontawesome-svg-core' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from '@fortawesome/fontawesome-svg-core';
const { icon } = pkg;
I put it in an isolated repo here: https://github.com/fabianmossberg/fontawesome-problem
A workaround until the packages are shipped as esm is to fix it in the vite config.
In SvelteKit, edit svelte.config.js
and add '@fortawesome/*'
to kit.vite.ssr.noExternal
https://github.com/fabianmossberg/fontawesome-problem/blob/7357af392e3858b726520a95688bb2a82d9913e0/svelte.config.js#L4-L13
@fabianmossberg Glad you got it working; I made #18720 a month ago and it hasn’t been triaged, so I’m not expecting a prompt fix.
@fabianmossberg Glad you got it working; I made #18720 a month ago and it hasn’t been triaged, so I’m not expecting a prompt fix.
I would guess that the files in the js-packages
folder are coming from some private repos that the FontAwesome team are handling, so the fix needs to be done in their repos, not in this one.
A workaround until the packages are shipped as esm is to fix it in the vite config.
In SvelteKit, edit
svelte.config.js
and add'@fortawesome/*'
tokit.vite.ssr.noExternal
https://github.com/fabianmossberg/fontawesome-problem/blob/7357af392e3858b726520a95688bb2a82d9913e0/svelte.config.js#L4-L13
Same in vite with vue. It seems that ssr/ssg is the problem. The font awesome packages gets detected as commonjs modules. From the fontawesome project you can try to adjust the esm exports to be compatible with vite's automatic detection. Or maybe vite will improve its autodetection in the future. Meanwhile @fabianmossberg's answer also works for vite with vue:
// vite.config.ts
export default defineViteConfig({
// ...
ssr: {
noExternal: ['@fortawesome/*'],
},
});
I'd argue this is a duplicate of #16439, right?
See also https://github.com/FortAwesome/Font-Awesome/issues/18075, https://github.com/FortAwesome/Font-Awesome/issues/18677
It appears the vite configuration no longer has the ssr: noExternal option. This is using vite 2.9.14. I see this comment in the index.d.ts file:
/* Excluded from this release type: ssr */
I tried using the "All The Things" installation method and imported the js using the esm import syntax (import '@fortawesome/fontawesome-pro/js/solid.js';
) and I still get that error on 6.2.0 using sveltekit. Am I importing the .js correctly?
The error:
@fortawesome/fontawesome-pro doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix.
Using the "Certain Specific Things" installation works as expected.
Am I importing the .js correctly?
I may be wrong, but using svelte and other JS frameworks with the Font Awesome JS+SVG implementation should require a different approach
There is a quite new svelte component that should be used for this use case. You may find more information at https://github.com/FortAwesome/svelte-fontawesome and examples
think you can import normally (no workaround needed) now since #19041 https://github.com/FortAwesome/Font-Awesome/pull/19041#issuecomment-1232025150
As of FA 6.2.0 and Vite, I still have the warning.
I am also on 6.2.0 and seeing the same warning. Please reopen this issue
Any chance to provide a reproducible test case?
Please provide as many information as possible (version of FA, how it is imported, version of Vite). Even better if it is possible to provide a reduced test case hosted on github
@tagliala you can clone this repo Dibbyo456/fa-bug-report to reproduce.