#ReferenceError: require is not defined in ES module scope

1 messages · Page 1 of 1 (latest)

surreal pilot
#

I get this error during runtime. Obviously this error means I'm meant to use ES imports instead of CJS require. That would be simple. However the offending require() call is in a dependency (@auzre/blob-storage). How can I configure Vite to translate the dynamic require() this into an ES import?
Thanks 🙏

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/Users/ben/Documents/my-app/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.```

Offending code from dependency:
``` typescript
// Can't use import as Typescript doesn't recognize "buffer".
const maxBufferLength = require("buffer").constants.MAX_LENGTH;```

I have also tried vite-plugin-commonjs in my vite config with no luck:
```typescript
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';
import { esbuildCommonjs } from '@originjs/vite-plugin-commonjs'

export default defineConfig({
    plugins: [
        sveltekit()
    ],
    ssr: {
        noExternal: ['typesafe-i18n'],
    },
    test: {
        include: ['src/**/*.{test,spec}.{js,ts}']
    },
    build: {
        commonjsOptions: {
            transformMixedEsModules: true,
            
        }
    },
    optimizeDeps: {
        esbuildOptions: {
            plugins: [esbuildCommonjs(['@azure/storage-blob'])]
        }
    }
});
surreal pilot
#

I solved it with this vite config