简体   繁体   中英

read private.key with fs.readFileSync, nvalid left-hand side expression in prefix operation

Hi i try to read my private key with fs.readFileSync but always get this error:

-----BEGIN RSA PRIVATE KEY----- ^^^^^^

SyntaxError: Invalid left-hand side expression in prefix operation

and i don't understand why because i was quiet sure in the past it worked.

Fy function is this:

const fs = require("fs");
const RSA_PRIVATE_KEY = fs.readFileSync('private.key', 'utf-8');

Thank you for your help.

For anyone running into the same issue, it turns out that relative paths are a bit dodgy with filesync. I was able to resolve it by adding the following code to my call to fs.readfilesync.

const fs = require('fs');
const path = require('path');
const privateKey = fs.readFileSync(path.join(__dirname, '../../../private.pem'), 'utf8');

__dirname returns the path to the current file's directory. By combining this with our relative path, we can create the full file path.

The other answer to this question suggests you import the file and then try to reference it in readfilesync; however, I found this created additional problems and didn't solve the issue.

Found the error in an other class where i missclicked and imported this file wrong.

const key = require('../../config/private.key')

which of course ends in an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM