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

javascript - How to read the pem file in nodejs? - Stack Overflow

programmeradmin1浏览0评论

Working in the Encode and Decode using the NodeJS, I want to Encode the data using the RS512 algorithm, for using that algorithm I have to pass the secret key as a pem file, so I use require to import that pem file but I cannot able to import that file

The Code that I used is

const secretKey = require("./secretkey.pem");

when I import the file like this am getting the error

ReferenceError: Invalid left-hand side expression in prefix operation

How to solve this issue.

Working in the Encode and Decode using the NodeJS, I want to Encode the data using the RS512 algorithm, for using that algorithm I have to pass the secret key as a pem file, so I use require to import that pem file but I cannot able to import that file

The Code that I used is

const secretKey = require("./secretkey.pem");

when I import the file like this am getting the error

ReferenceError: Invalid left-hand side expression in prefix operation

How to solve this issue.

Share Improve this question asked Jul 21, 2020 at 14:24 Raghul SKRaghul SK 1,3905 gold badges24 silver badges35 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 7

this one working great for me

import * as fs from 'fs';



const publicKey = fs.readFileSync("../server/src/config/public.pem", { encoding: "utf8" });

You can't require a PEM file - that's only used for JS & JSON files. The error is a complaint that the PEM file is not valid JS syntax.

To read raw data from other files, including PEM, you can use the fs module: https://nodejs.org/api/fs.html.

For example:

const fs = require('fs');

fs.readFile("./secretkey.pem", "ascii", function (pemContents) {
  // do whatever you want here
});
发布评论

评论列表(0)

  1. 暂无评论