search

Home  >  Q&A  >  body text

Using custom HTTPS certificate in Nuxt (but only in development mode)?

The dependencies I'm using require me to have HTTPS on localhost. I used the following code in nuxt.config.js to accomplish this:

server: {        
        https: {
            key: fs.readFileSync(path.resolve(__dirname, 'localhost-key.pem')),
            cert: fs.readFileSync(path.resolve(__dirname, 'localhost.pem'))
        }
    },

These are the keys I created myself using mkcert. However, I will use the actual certificate on the live page. Is there a way to restrict the server block in nuxt.config.js to development mode only?

P粉478445671P粉478445671252 days ago443

reply all(1)I'll reply

  • P粉521697419

    P粉5216974192024-03-22 15:32:34

    I have used it before

    server: {
        https: process.env.NODE_ENV === 'development' && process.env.USE_LOCAL_HTTPS === 'true'
          ? {
            key: fs.readFileSync(path.resolve(__dirname, 'server.key')),
            cert: fs.readFileSync(path.resolve(__dirname, 'server.crt')),
          }
          : false,
    },
    

    NODE_ENV is used to double check that the environment is development and USE_LOCAL_HTTPS is another variable to make sure it is not staging development environment. Of course, you might not even need it if your NODE_ENV has something like staging or test.

    Otherwise, I've never double checked, but this server key configuration might even only work for local development. Give it a try, otherwise try my configuration.

    reply
    0
  • Cancelreply