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

Bug: exports is not defined #90

Closed
RaulCatalinas opened this issue Apr 20, 2024 · 2 comments
Closed

Bug: exports is not defined #90

RaulCatalinas opened this issue Apr 20, 2024 · 2 comments

Comments

@RaulCatalinas
Copy link

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']
      })
    ]
  }
})
@jobcespedes
Copy link

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}']
  }
})

@RaulCatalinas
Copy link
Author

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

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

2 participants