【发布时间】: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"
错误
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