#Component different project
7 messages · Page 1 of 1 (latest)
Hi @past plinth. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Yes, you can import components from different Astro projects or any JavaScript/TypeScript modules as long as they are installed in your project's node_modules directory. Astro uses ESM, the same import and export syntax supported in the browser.
For example, if you've installed an NPM package, you can import it in Astro like this:
---import { Icon } from 'astro-icon';---
Astro will try to convert the package to ESM so that import statements work. In some cases, you may need to adjust your vite config for it to work.
However, be aware that some packages rely on a browser environment. Astro components run on the server, so importing these packages in the frontmatter may lead to errors.
For more details, you can refer to the Astro documentation on imports (https://docs.astro.build/en/guides/imports/).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@past plinth kapa.ai is still learning and improving, please let us know how it did by reacting below
@mint geyser Suppose that in my project 1 I have:
AstroProject1\src\components\MyComponent.astro
I would like to import this component in a file for my second project
AstroProject2\src\pages\mypage.astro
Let me check the knowledge base for you. One moment! :books:
To import a component from one Astro project into another, you would need to publish the component from the first project as a package that can be installed via npm or yarn in the second project. Once the component is published and installed in the second project, you can import it like any other npm package.
However, the provided extracts do not contain specific instructions on how to publish an Astro component as an npm package.