Ivan Vukusic
12/10/2024, 7:05 PMUncaught TypeError: Failed to resolve module specifier "crypto". Relative references must start with either "/", "./", or "../".
I'll show you the difference with a quick video of the difference running locally and when it gets sent to my staging app.
It has been a second since I touched the vite.config.ts but this is what it should have been liked when Algolia worked.
import { vitePlugin as remix } from '@remix-run/dev'
import { sentryVitePlugin } from '@sentry/vite-plugin'
import { glob } from 'glob'
import { flatRoutes } from 'remix-flat-routes'
import { defineConfig } from 'vite'
import { remixDevTools } from 'remix-development-tools'
import { envOnlyMacros } from 'vite-env-only'
import { cjsInterop } from 'vite-plugin-cjs-interop'
const MODE = process.env.NODE_ENV
export default defineConfig({
//WARN: reason why <https://threads.typesense.org/2Lb60>
// ssr: {
// noExternal: ['typesense-instantsearch-adapter'],
// },
build: {
cssMinify: MODE === 'production',
rollupOptions: {
external: [/node:.*/, 'stream', 'crypto', 'fsevents', 'posthog'],
},
assetsInlineLimit: (source: string) => {
if (
source.endsWith('sprite.svg') ||
source.endsWith('favicon.svg') ||
source.endsWith('apple-touch-icon.png')
) {
return false
}
},
sourcemap: true,
},
server: {
watch: {
ignored: ['**/playwright-report/**'],
},
},
plugins: [
envOnlyMacros(),
remixDevTools(),
cjsInterop({
dependencies: ['typesense-instantsearch-adapter'],
}),
remix({
ignoredRouteFiles: ['**/*'],
serverModuleFormat: 'esm',
routes: async defineRoutes => {
return flatRoutes('routes', defineRoutes, {
ignoredRouteFiles: [
'.*',
'**/*.css',
'**/*.test.{js,jsx,ts,tsx}',
'**/__*.*',
// This is for server-side utilities you want to colocate
// next to your routes without making an additional
// directory. If you need a route that includes "server" or
// "client" in the filename, use the escape brackets like:
// my-route.[server].tsx
'**/*.server.*',
'**/*.client.*',
],
})
},
}),
process.env.SENTRY_AUTH_TOKEN
? sentryVitePlugin({
disable: MODE !== 'production',
authToken: process.env.SENTRY_AUTH_TOKEN,
org: process.env.SENTRY_ORG,
project: process.env.SENTRY_PROJECT,
release: {
name: process.env.COMMIT_SHA,
setCommits: {
auto: true,
},
},
sourcemaps: {
filesToDeleteAfterUpload: await glob([
'./build/**/*.map',
'.server-build/**/*.map',
]),
},
})
: null,
],
})
I have tried many iterations to try and make this work. Mainly the steps recommended to try and at the remix link here: https://remix.run/docs/en/main/guides/vite#esm--cjs
When trying to make it work with cjs-interop plugin mentioned it wouldn't even run and I get this error:
11:21:53 PM [vite] Error when evaluating SSR module /node_modules/typesense-instantsearch-adapter/lib/TypesenseInstantsearchAdapter.js:
|- ReferenceError: require is not defined
at eval (/Users/vonkoff/GoodUnionJobs/remix-guj/node_modules/typesense-instantsearch-adapter/lib/TypesenseInstantsearchAdapter.js:5:30)
at instantiateModule (file:///Users/vonkoff/GoodUnionJobs/remix-guj/node_modules/vite/dist/node/chunks/dep-BzOvws4Y.js:52870:11)
11:21:53 PM [vite] Error when evaluating SSR module /app/routes/_marketing+/index.tsx:
|- ReferenceError: require is not defined
at eval (/Users/vonkoff/GoodUnionJobs/remix-guj/node_modules/typesense-instantsearch-adapter/lib/TypesenseInstantsearchAdapter.js:5:30)
at instantiateModule (file:///Users/vonkoff/GoodUnionJobs/remix-guj/node_modules/vite/dist/node/chunks/dep-BzOvws4Y.js:52870:11)
11:21:53 PM [vite] Error when evaluating SSR module virtual:remix/server-build:
|- ReferenceError: require is not defined
at eval (/Users/vonkoff/GoodUnionJobs/remix-guj/node_modules/typesense-instantsearch-adapter/lib/TypesenseInstantsearchAdapter.js:5:30)
at instantiateModule (file:///Users/vonkoff/GoodUnionJobs/remix-guj/node_modules/vite/dist/node/chunks/dep-BzOvws4Y.js:52870:11)
ReferenceError: require is not defined
ReferenceError: require is not defined
at eval (/Users/vonkoff/GoodUnionJobs/remix-guj/node_modules/typesense-instantsearch-adapter/lib/TypesenseInstantsearchAdapter.js:5:30)
at instantiateModule (file:///Users/vonkoff/GoodUnionJobs/remix-guj/node_modules/vite/dist/node/chunks/dep-BzOvws4Y.js:52870:11)