1

Hello friends.

I need to continue my Nuxt JS work with SSL. However, after installation, I am getting the following error. I know the problem is because Node JS doesn't recognize the word "IMPORT". But I don't know how to solve the problem. Because I use Components as IMPORT all over the project. What is your suggestion?

Thank you very much in advance.


package.json

"dev": "node server.js",
"nuxt": "^2.15.7",
"express": "^4.17.1"

ERROR IMAGE


error

SyntaxError: Cannot use import statement outside a module at compileFunction (<anonymous>)

nuxt.config.js

import axiosModule from './modules/axiosModule'
import momentModule from './modules/momentModule'
export default {
  server: {
    host: '0.0.0.0',
    port: 3000,
  },
......

server.js

const { Nuxt, Builder } = require('nuxt')
const expressServer = require('express')()
const thisHttp = require('http')
const thisHttps = require('https')
const thisFs = require('fs-extra')

const isProd = (process.env.NODE_ENV === 'production')
const isPort = 3000

let thisServer
if (isProd) {
  const pKey = thisFs.readFileSync('./key.pem')
  const pCert = thisFs.readFileSync('./cert.pem')
  const httpsOptions = { key: pKey, cert: pCert }
  thisServer = thisHttps.createServer(httpsOptions, expressServer)
} else {
  thisServer = thisHttp.createServer(expressServer)
}

const nuxtConfig = require('./nuxt.config')
nuxtConfig.dev = !isProd
const nuxtServer = new Nuxt(nuxtConfig)
expressServer.use(nuxtServer.render)

const listen = () => { thisServer.listen(isPort, 'localhost') }

if (nuxtConfig.dev) {
  new Builder(nuxtServer).build().then(listen()).catch(error => { console.log(error); process.exit(1) })
} else {
  listen()
}
Uğurcan
  • 91
  • 6
  • You should probably give a try to this: https://stackoverflow.com/questions/56966137/how-to-run-nuxt-npm-run-dev-with-https-in-localhost And to this one: https://stackoverflow.com/questions/61401475/why-is-type-module-in-package-json-file/68558580#68558580 – kissu Oct 25 '21 at 02:47

1 Answers1

0

I fixed the situation manually. I did REQUIRE instead of IMPORT in Nuxt Config and used module.exports instead of Export Default. Even though I'm currently logging in via HTTPS, it's crossed out by Google Chrome.

Uğurcan
  • 91
  • 6