xx11
xx11

Reputation: 1

nodejs can not detect .pem file

I have a kubernetes typescript file and a .pem file under same parent directory in vscode, but i got error error: ENOENT: no such file or directory, open './cert.pem', it seems like my ts file can not detect pem file exists

const dataServiceTlsSecret = addResource(
    new Secret({
      metadata: {
        name: DATA_SERVICE,
      },
      type: "kubernetes.io/tls",
      stringData: {
        "tls.crt": fs.readFileSync("./cert.pem", 'utf-8'),
        "tls.key": fs.readFileSync("./key.pem", 'utf-8'),
      }
    })
  )

I also tried to import .pem file on the top of my ts file, it doesn't show any .pem file from suggestion popout. any suggestions on how I can read pem file content from my ts file?

Upvotes: -1

Views: 706

Answers (1)

buffolander
buffolander

Reputation: 102

I believe there could be different issues, but it's most likely related to using relative paths.

If your script is nested in a subdirectory – as in src/my-script.ts – and you're running it from your project root directory – as in ts-node src/myscript.ts –, then readFileSync is looking for the file in the project root /.

You might want to consider passing absolute paths to readFileSync.

const certPath = path.resolve(__dirname, "./cert.pem")
const keyPath = path.resolve(__dirname, "./key.pem")

const dataServiceTlsSecret = addResource(
    new Secret({
      metadata: {
        name: DATA_SERVICE,
      },
      type: "kubernetes.io/tls",
      stringData: {
        "tls.crt": fs.readFileSync(certPath, 'utf-8'),
        "tls.key": fs.readFileSync(keyPath, 'utf-8'),
      }
    })
  )

Upvotes: 1

Related Questions