【问题标题】:Nuxt JS SSL Server Installation Problem on MacOSMacOS 上的 Nuxt JS SSL 服务器安装问题
【发布时间】:2021-12-10 06:07:15
【问题描述】:

朋友们好。

我需要使用 SSL 继续我的 Nuxt JS 工作。但是,安装后,我收到以下错误。我知道问题是因为 Node JS 无法识别“IMPORT”这个词。但我不知道如何解决这个问题。因为我在整个项目中都使用组件作为 IMPORT。你有什么建议?

提前非常感谢您。 ????


package.json

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

ERROR IMAGE


错误

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()
}

【问题讨论】:

标签: node.js ssl nuxt.js web-development-server


【解决方案1】:

我手动修复了这种情况。我在 Nuxt Config 中使用了 REQUIRE 而不是 IMPORT,并使用了 module.exports 而不是 Export Default。尽管我目前通过 HTTPS 登录,但它已被 Google Chrome 删除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 2019-03-05
    • 2014-05-13
    • 2018-02-05
    • 2020-06-08
    • 1970-01-01
    • 2020-09-26
    相关资源
    最近更新 更多