davidmyersdev/vite-plugin-node-polyfills

Bug: exports is not defined

RaulCatalinas opened this issue · 2 comments

I'm importing the modules "fs" and "os" in this file

// Third-Party libraries
import ytdl from 'ytdl-core'

// Utils
import { getVideoTitle } from '@/utils/youtube'

// NodeJS
import fs from 'fs'
import os from 'os'

// i18n
import { getJson } from '@/i18n/utils'

// Types
import type { Language } from '@/types/language'
import type { OS } from '@/types/os.d'

// Constants
import { DOWNLOAD_FORMAT_FILTERS, DownloadQuality } from '@/constants/download'
import { FileExtensions, UTF8_ENCODING } from '@/constants/files'

// Utils
import { cleanInvalidChars } from '@/utils/chars'

interface DownloadControllerProps {
  url: string
  downloadVideo: boolean
  language: Language
  userOS: OS
}

interface DownloadControllerResult {
  success: boolean
  errorMessage?: string
  responseMessage?: string
}

export async function downloadController({
  url,
  downloadVideo,
  language,
  userOS
}: DownloadControllerProps): Promise<DownloadControllerResult> {
  const { download } = getJson(language)

  try {
    const originalTitle = await getVideoTitle(url)

    const titleWithOutInvalidChars = cleanInvalidChars({
      title: originalTitle,
      userOS
    })

    const extension = FileExtensions[downloadVideo ? 'Video' : 'Audio']

    const userDesktop = `${os.homedir()}/desktop`

    ytdl(url, {
      filter: DOWNLOAD_FORMAT_FILTERS[downloadVideo ? 'video' : 'audio'],
      quality: DownloadQuality[downloadVideo ? 'Video' : 'Audio']
    }).pipe(
      fs.createWriteStream(
        `${userDesktop}/${titleWithOutInvalidChars}.${extension}`,
        {
          encoding: UTF8_ENCODING
        }
      )
    )

    return {
      success: true,
      responseMessage: download.success
    }
  } catch (error) {
    console.error(error)

    return {
      success: false,
      errorMessage: download.errors.couldNotBeDownloaded
    }
  }
}

And when I pull up the development server it gives me this error

image

Here's my Astro configuration

// Astro
import { defineConfig } from 'astro/config'

// Integrations
import sitemap from '@astrojs/sitemap'
import solidJs from '@astrojs/solid-js'
import tailwind from '@astrojs/tailwind'
import vercel from '@astrojs/vercel/serverless'

// Vite plugins
import { nodePolyfills } from 'vite-plugin-node-polyfills'

// https://astro.build/config
export default defineConfig({
  integrations: [tailwind(), solidJs(), sitemap()],
  site: 'https://easyviewer.vercel.app',
  output: 'server',
  adapter: vercel({
    webAnalytics: {
      enabled: true
    }
  }),
  i18n: {
    defaultLocale: 'es',
    locales: ['es', 'en'],
    routing: {
      prefixDefaultLocale: false
    }
  },
  vite: {
    plugins: [
      nodePolyfills({
        include: ['fs', 'os']
      })
    ]
  }
})

Similar issue but referencing crypto. A workaround that seems to work is excluding it:

import { sveltekit } from '@sveltejs/kit/vite'
import { defineConfig } from 'vitest/config'
import { nodePolyfills } from 'vite-plugin-node-polyfills'

export default defineConfig({
  plugins: [
    sveltekit(),
    nodePolyfills({
      // WORKAROUND: https://github.com/davidmyersdev/vite-plugin-node-polyfills/issues/90
      exclude: ['crypto']
    })
  ],
  test: {
    include: ['src/**/*.{test,spec}.{js,ts}']
  }
})

It worked, I just had to exclude crypto to solve the error.