构建编译失败 - 尝试导入错误
Build failed to compile - Attempted import error
由于这个错误,我每次尝试构建生产都失败了:
Attempted import error: 'decrypt' is not exported from
'../../../../util/security' (imported as 'decrypt').
此错误仅在我尝试构建生产环境时出现(npm 运行 构建)。
这是我导入模块的方式:
import {encrypt2, decrypt} from "../../../../util/security";
以及我如何导出它:
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);
},
};
(模块解密我的加密路由)
我检查了一下,文件在正确的位置,此外,在开发模式下它按预期工作。
将您的导入更改为:
import * as security from "../../../../util/security";
并像这样使用它:
security.decrypt(...)
这里的解决方案是将 Node.js 样式导出 (module.exports =
) 替换为 ES6 导出(export ...
或 export default ...
,具体取决于您要导入的方式)。
原因是 import/export 样式必须兼容,虽然在某些情况下这可以工作,但在 React 应用程序中混合两种样式并不是最好的主意。
由于这个错误,我每次尝试构建生产都失败了:
Attempted import error: 'decrypt' is not exported from '../../../../util/security' (imported as 'decrypt').
此错误仅在我尝试构建生产环境时出现(npm 运行 构建)。
这是我导入模块的方式:
import {encrypt2, decrypt} from "../../../../util/security";
以及我如何导出它:
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);
},
};
(模块解密我的加密路由)
我检查了一下,文件在正确的位置,此外,在开发模式下它按预期工作。
将您的导入更改为:
import * as security from "../../../../util/security";
并像这样使用它:
security.decrypt(...)
这里的解决方案是将 Node.js 样式导出 (module.exports =
) 替换为 ES6 导出(export ...
或 export default ...
,具体取决于您要导入的方式)。
原因是 import/export 样式必须兼容,虽然在某些情况下这可以工作,但在 React 应用程序中混合两种样式并不是最好的主意。