0

Anyone knows how I can import each of below function in Vue and React without an error? Right now I am getting an error if I am importing vueFetch and reactFetch.

So if am importing vueFetch into a Vue project, I am getting an error for React that React is not defined and the same applies if I import reactFetch (Vue ref not defined).

import { vueFetch } from './composables/vue/vue-fetch';
import { reactFetch } from './composables/react/react-fetch';

export { vueFetch, reactFetch };

I am trying to make a Vue and React NPM package, but I only have one index file, and I need to import both Hook into the single index file. So I do not need to create 2 packages.

here is the link to the package: https://www.npmjs.com/package/use-lightweight-fetch

4
  • 1
    Why would you do this in the same file? Just have two files and tell people which one to use for Vue vs. React? No one should need to load code for a framework they're not even using =) Commented Jan 29, 2023 at 0:33
  • @Mike'Pomax'Kamermans Thanks. But it seems like I can only have one index file when creating a NPM package. Could you show me an example? Cause I am having two test folders, one for Vue and one for React and when I am linking my package, it is using the index.js file Commented Jan 29, 2023 at 0:55
  • You can only have one index.js (well, no, you can technically have two, one for ESM and one for CJS, but that's not relevant here) but that index.js can export two different imports from two different files, so that someone can say import { VueThing } from "libraryname"; and then have treeshaking during bundling remove all the code for "the framework I'm not using". Commented Jan 29, 2023 at 1:09
  • @Mike'Pomax'Kamermans Could you give me an example? Commented Jan 29, 2023 at 1:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.