最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Build failed to compile - Attempted import error - Stack Overflow

programmeradmin2浏览0评论

My attempt to build for production fails every time because of this error:

Attempted import error: 'decrypt' is not exported from '../../../../util/security' (imported as 'decrypt').

This error only appears when I try to build for production (npm run build).

Here is the way I import my modules:

import {encrypt2, decrypt} from "../../../../util/security";

And how I export it :

module.exports = {

    decrypt: function(encrypted_route) {
        encrypted_route = atob(encrypted_route);
        let [initialVector, salt, aes_route] = encrypted_route.split("::");
        let key = generateKey(salt);

        let cipherParams = CryptoJS.lib.CipherParams.create({
            ciphertext: CryptoJS.enc.Base64.parse(aes_route)
        });

        let unencrypted = CryptoJS.AES.decrypt(
            cipherParams,
            key,
            {iv: CryptoJS.enc.Hex.parse(initialVector)}
        );
        return unencrypted.toString(CryptoJS.enc.Utf8);
    },

};

My attempt to build for production fails every time because of this error:

Attempted import error: 'decrypt' is not exported from '../../../../util/security' (imported as 'decrypt').

This error only appears when I try to build for production (npm run build).

Here is the way I import my modules:

import {encrypt2, decrypt} from "../../../../util/security";

And how I export it :

module.exports = {

    decrypt: function(encrypted_route) {
        encrypted_route = atob(encrypted_route);
        let [initialVector, salt, aes_route] = encrypted_route.split("::");
        let key = generateKey(salt);

        let cipherParams = CryptoJS.lib.CipherParams.create({
            ciphertext: CryptoJS.enc.Base64.parse(aes_route)
        });

        let unencrypted = CryptoJS.AES.decrypt(
            cipherParams,
            key,
            {iv: CryptoJS.enc.Hex.parse(initialVector)}
        );
        return unencrypted.toString(CryptoJS.enc.Utf8);
    },

};

(The module decrypts my encrypted route)

I checked and the file is in the right place, moreover, in development mode it works as intended.

Share Improve this question asked May 25, 2022 at 7:03 QuentinQuentin 3718 silver badges23 bronze badges 5
  • 2 Have you tried exporting with export ... or export default ... rather than module.exports? What you try at the moment looks like mixing node.js style of exporting (module.exports) with es6 importing (import ...) – Wiktor Zychla Commented May 25, 2022 at 7:41
  • Nop, didn't try with export ..., I will give it a shot ! – Quentin Commented May 25, 2022 at 7:43
  • Aaah it's was my export solution that was causing the error, exporting every function like export ... fixed my problem ! Yeah module.exports was a bit messy to use ... – Quentin Commented May 25, 2022 at 7:52
  • Will post this as an answer then. – Wiktor Zychla Commented May 25, 2022 at 7:54
  • Oh please do, if you want, was about to make it :p – Quentin Commented May 25, 2022 at 7:56
Add a ment  | 

2 Answers 2

Reset to default 3

The solution here is to replace Node.js style exports (module.exports = ) with ES6 exports (export ... or export default ... depending on how you want to import it).

The reason is that import/export styles have to be patible and while there are scenarios where this could work, mixing the two styles in a React app is not the best idea.

Change your import to:

import * as security from "../../../../util/security";

and use it like:

security.decrypt(...)
发布评论

评论列表(0)

  1. 暂无评论