有没有办法从一个Nuxt+Node项目创建可执行文件(.exe)?

3

我有一个NuxtJS项目,需要一个NodeJS程序在后台运行一些功能和逻辑。该项目的结构如下:

api
assets
components
layouts
middleware
pages
plugins
server
static
store
nuxt.config.js
package.json

nuxt.config.js

module.exports =  {
  head: {
    titleTemplate: '%s',
    title: 'Project',
    htmlAttrs: {
      lang: 'en'
    },
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: '' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },

  css: [
    '@/assets/css/main.scss'
  ],

  plugins: [
  ],

  components: true,

  buildModules: [
    '@nuxtjs/vuetify'
  ],

  modules: [
    'nuxt-socket-io',
    'nuxt-i18n',
    '@nuxtjs/axios',
    '@nuxtjs/auth-next'
  ],

  io: {
    sockets: [
      {
        name: 'main',
        url: process.env.APP_SERVER_URL,
        default: true
      }
    ]
  },

  i18n: {
    locales: [
      {
        code: 'en',
        file: 'en-US.js'
      }
    ],
    lazy: true,
    langDir: 'lang/',
    defaultLocale: 'en'
  },

  serverMiddleware: [
    { path: '/api', handler: '~/api/index.js' },
  ],

  axios: {
    baseURL: process.env.APP_SERVER_URL,
  },

  vuetify: {
    customVariables: ['~/assets/variables.scss'],
    theme: {
      dark: true,
      themes: {
        dark: {},
        light: {}
      }
    }
  },

  build: {
    extend(config) {}
  }
}

package.json

{
  "name": "my-project",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "dev": "nodemon -w server -w nuxt.config.js server",
    "build": "nuxt generate",
    "start": "cross-env NODE_ENV=production node server",
    "generate": "nuxt generate"
  },
  "dependencies": {
    "@nuxtjs/auth-next": "5.0.0-1611574754.9020f2a",
    "@nuxtjs/axios": "^5.12.5",
    "body-parser": "^1.19.0",
    "core-js": "^3.8.3",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "http": "0.0.1-security",
    "moment": "^2.29.1",
    "nuxt": "^2.14.12",
    "nuxt-i18n": "^6.18.0",
    "nuxt-socket-io": "^1.1.14"
  },
  "devDependencies": {
    "@nuxtjs/vuetify": "^1.11.3",
    "cross-env": "^7.0.3",
    "nodemon": "^2.0.7"
  }
}

server/index.js

require('dotenv').config();

const isProd = process.env.NODE_ENV === 'production'
const http = require('http')
const app = require('express')()
const server = http.createServer(app)
const io = require('socket.io')(server)
const axios = require('axios')

const { Nuxt, Builder } = require('nuxt')
const config = require('../nuxt.config.js');
config.dev = !isProd;

const nuxt = new Nuxt(config)
const { host, port } = nuxt.options.server
if (config.dev) {
    const builder = new Builder(nuxt)
    builder.build()
} else {
    nuxt.ready()
}
app.use(nuxt.render)

server.listen(port, () => {
    console.log(`Server listening on http://${host}:${port}`)
});

// other logic

我需要一个exe文件,可以被安装在其他电脑上运行Nodejs服务器和Nuxt工具,就像我在本地开发电脑上通过npm run devnpm run build/start来运行代码一样。

我尝试过使用nexe运行nexe -i server但没有成功。还有其他方法可以做到吗?

谢谢。

2个回答

0

我觉得你可以看一下pm2。你可以用它来运行Node服务器和其他东西。


谢谢您的回复。但是这仍然需要我在每台计算机上安装源代码。我不想这样做。 - kolojo

0

将Node.js应用程序编译为.exe文件

编译JavaScript文件为可执行文件最常用的两个软件包是:

nexe:它是一个简单的命令行实用程序,可以将您的Node.js应用程序编译为单个可执行文件。默认情况下,它会将其转换为Windows可执行文件。

pkg:这是一个Node.js软件包,可以将您的Node.js应用程序转换为多个操作系统的可执行文件(同时转换所有操作系统)或单个操作系统的可执行文件。

输入链接描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接