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 badges2 Answers
Reset to default 7this 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
});