brix / crypto-js

JavaScript library of crypto standards.
Other
15.76k stars 2.38k forks source link

CryptoJS with Vue 3 and Vite #328

Open bayramorhan opened 3 years ago

bayramorhan commented 3 years ago

Commenting out in core.js 40. fixing the issue ([vite] Dep optimization failed with error: Could not load crypto (imported by node_modules/crypto-js/core.js): ENOENT: no such file or directory, open 'crypto')

// Native crypto import via require (NodeJS) // if (!crypto && typeof require === 'function') { // try { // crypto = require('crypto'); // } catch (err) {} // }

1414044032 commented 3 years ago

I also met this problem, how should I solve it?

danielholmes commented 2 years ago

I ran into a similar kind of problem with react + vite. Solved with the following vite.config.js:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react()
  ],
  resolve: {
    alias: {
      "crypto": "crypto-js"
    }
  }
})

Edit: This worked for local development, but it caused problems during production build. In the end i needed to upgrade the dependency that was causing the problem (aws-sdk-jsv2 -> v3)