我正在使用 jwt 解码我的令牌以检查它是否过期,我收到加密模块未找到的错误。
import jwt from 'jsonwebtoken';
const checkToken = token => {
try {
const decodedToken = jwt.decode(token);
const experiationTime = decodedToken.exp;
const expirationDate = new Date(experiationTime * 1000);
if (expirationDate > currentDate) {
console.log('Token is still valid', expirationDate);
return true;
} else {
console.log('Token is expired', expirationDate);
return false;
}
}
catch(err){
console.log(err)
}
}
我收到以下错误:
无法从
node_modules\jsonwebtoken 颂歌模块 节点模块 7 | const PS_SUPPORTED = require('./lib/psSupported'); 8 | const jws = require('jws');
9 | const {KeyObject, createSecretKey, createPublicKey} = require("crypto");
由于 React Native 不使用 Node 运行时环境,因此无法访问特定的库,例如“crypto”,所以我在 package.json 中安装了 nodeify & 我包含以下声明
npx rn-nodeify --安装
脚本:{ ... "postinstall": "node_modules/.bin/rn-nodeify --install crypto,assert,url,stream,events --hack" }
但仍然遇到同样的错误。
您可以在
import './shim.js'
之前导入 const {KeyObject, createSecretKey, createPublicKey} = require("crypto");
并修补它。
您可以检查一下如何配置它: 在 React Native 中解决 Node.js 加密模块