在YARN构建后,从.nuxt/dist/client文件夹中丢失一个.js资源

发布于 2025-02-12 14:32:52 字数 4312 浏览 0 评论 0原文

我正在尝试使用Apache和PM2部署我的NUXT项目。

NUXT项目将是前端,并且具有不同域上的后端API。 我的虚拟主机设置正常,我可以访问登录页面,但由于某些原因,它总是在一个.js文件上返回404,该文件应该在我运行yarn build build内部。 DIST/客户端文件夹。

不过,值得注意的一些奇怪的事情是,当我使用无效的api_url时,例如我使用不存在的URL https://xxx.xxx/api/1而不是正确的url https://xxx.xxx/api/v1。重试纱构建和PM2重新启动后,错误将消失。

这是错误:

我的设置:

  • OS:Debian 11
  • Server:Apache2& PM2

我的nuxt.config.js

    import colors from "vuetify/es5/util/colors";

export default {
  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    titleTemplate: "%s - " + process.env.APP_NAME,
    title: process.env.APP_NAME,
    htmlAttrs: {
      lang: "en",
    },
    meta: [
      { charset: "utf-8" },
      { name: "viewport", content: "width=device-width, initial-scale=1" },
      { hid: "description", name: "description", content: "" },
      { name: "format-detection", content: "telephone=no" },
    ],
    link: [{ rel: "icon", type: "image/x-icon", href: "/favicon.ico" }],
  },

  watchers: {
    webpack: {
      aggregateTimeout: 300,
      poll: 1000,
    },
  },

  auth: {
    strategies: {
      laravelPassport: {
        provider: "laravel/passport",
        endpoints: {
          userInfo: process.env.API_URL + "/me",
        },
        url: process.env.API_SERVER,
        clientId: process.env.PASSPORT_CLIENT_ID,
        clientSecret: process.env.PASSPORT_CLIENT_SECRET,
      },
      local: {
        endpoints: {
          login: {
            url: "/user/login",
            method: "post",
            propertyName: "data.access_token",
          },
          logout: { url: "/user/logout", method: "post" },
          user: { url: "/me", method: "get", propertyName: "data" },
        },
        token: {
          property: "data.access_token",
        },
        user: {
          property: "data",
        },
        tokenRequired: true,
        tokenType: "Bearer",
        autoFetchUser: true,
      },
      customStrategy: {
        scheme: "~/schemes/customScheme",
        endpoints: {
          login: {
            url: "/user/login",
            method: "post",
            propertyName: "data.access_token",    
          },
          logout: { url: "/user/logout", method: "post" },
          user: { url: "/me", method: "get", propertyName: "data" },
        },
        tokenRequired: true,
        tokenType: "Bearer",
        autoFetchUser: true,
      },
    },
    redirect: {
      login: "/",
      logout: "/",
      home: "/admin",
    },
  },

  router: {
    middleware: ["auth"],
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: ["~plugins/vue-api-query"],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
    // https://go.nuxtjs.dev/vuetify
    "@nuxtjs/vuetify",
  ],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: ["@nuxtjs/axios", "@nuxtjs/auth-next"],

  // Vuetify module configuration: https://go.nuxtjs.dev/config-vuetify
  vuetify: {
    customVariables: ["~/assets/variables.scss"],
    theme: {
      dark: false,
      themes: {
        dark: {
          primary: colors.blue.darken2,
          accent: colors.grey.darken3,
          secondary: colors.amber.darken3,
          info: colors.teal.lighten1,
          warning: colors.amber.base,
          error: colors.deepOrange.accent4,
          success: colors.green.accent3,
        },
      },
    },
  },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    maxChunkSize: 300000,
    extend(config, ctx) {
      config.performance.maxAssetSize = 700 * 1024;
    },
  },

  // Axios module configuration: https://go.nuxtjs.dev/config-axios
  axios: {
    baseURL: process.env.API_SERVER || "https://xxx.xxx",
    apiUrl: process.env.API_URL || "https://xxx.xxx/api/v1",
    headers: {
      common: {
        Platform: "web",
      },
    },
  },
};

I'm trying to deploy my Nuxt project using apache and pm2.

The nuxt project will be the front-end, and it has a backend api that is on a different domain.
I got the virtual host setup fine, I can access the login page but for some reasons, it always returns a 404 on one .js file that's supposed to be generated after I ran yarn build inside .nuxt/dist/client folder.

Some odd thing that's worth noting though is that when I use an invalid API_URL, like if I use a non-existent url https://xxx.xxx/api/1 instead of the correct url https://xxx.xxx/api/v1. The error will disappear after retrying yarn build and pm2 restart

Here's the error:
enter image description here

My setup:

  • OS: Debian 11
  • Server: Apache2 & pm2

My nuxt.config.js

    import colors from "vuetify/es5/util/colors";

export default {
  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    titleTemplate: "%s - " + process.env.APP_NAME,
    title: process.env.APP_NAME,
    htmlAttrs: {
      lang: "en",
    },
    meta: [
      { charset: "utf-8" },
      { name: "viewport", content: "width=device-width, initial-scale=1" },
      { hid: "description", name: "description", content: "" },
      { name: "format-detection", content: "telephone=no" },
    ],
    link: [{ rel: "icon", type: "image/x-icon", href: "/favicon.ico" }],
  },

  watchers: {
    webpack: {
      aggregateTimeout: 300,
      poll: 1000,
    },
  },

  auth: {
    strategies: {
      laravelPassport: {
        provider: "laravel/passport",
        endpoints: {
          userInfo: process.env.API_URL + "/me",
        },
        url: process.env.API_SERVER,
        clientId: process.env.PASSPORT_CLIENT_ID,
        clientSecret: process.env.PASSPORT_CLIENT_SECRET,
      },
      local: {
        endpoints: {
          login: {
            url: "/user/login",
            method: "post",
            propertyName: "data.access_token",
          },
          logout: { url: "/user/logout", method: "post" },
          user: { url: "/me", method: "get", propertyName: "data" },
        },
        token: {
          property: "data.access_token",
        },
        user: {
          property: "data",
        },
        tokenRequired: true,
        tokenType: "Bearer",
        autoFetchUser: true,
      },
      customStrategy: {
        scheme: "~/schemes/customScheme",
        endpoints: {
          login: {
            url: "/user/login",
            method: "post",
            propertyName: "data.access_token",    
          },
          logout: { url: "/user/logout", method: "post" },
          user: { url: "/me", method: "get", propertyName: "data" },
        },
        tokenRequired: true,
        tokenType: "Bearer",
        autoFetchUser: true,
      },
    },
    redirect: {
      login: "/",
      logout: "/",
      home: "/admin",
    },
  },

  router: {
    middleware: ["auth"],
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: ["~plugins/vue-api-query"],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
    // https://go.nuxtjs.dev/vuetify
    "@nuxtjs/vuetify",
  ],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: ["@nuxtjs/axios", "@nuxtjs/auth-next"],

  // Vuetify module configuration: https://go.nuxtjs.dev/config-vuetify
  vuetify: {
    customVariables: ["~/assets/variables.scss"],
    theme: {
      dark: false,
      themes: {
        dark: {
          primary: colors.blue.darken2,
          accent: colors.grey.darken3,
          secondary: colors.amber.darken3,
          info: colors.teal.lighten1,
          warning: colors.amber.base,
          error: colors.deepOrange.accent4,
          success: colors.green.accent3,
        },
      },
    },
  },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    maxChunkSize: 300000,
    extend(config, ctx) {
      config.performance.maxAssetSize = 700 * 1024;
    },
  },

  // Axios module configuration: https://go.nuxtjs.dev/config-axios
  axios: {
    baseURL: process.env.API_SERVER || "https://xxx.xxx",
    apiUrl: process.env.API_URL || "https://xxx.xxx/api/v1",
    headers: {
      common: {
        Platform: "web",
      },
    },
  },
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天气好吗我好吗 2025-02-19 14:32:54

在寻找几天的答案后,我找不到解决问题的解决方案。
只是发现这是一个PM2问题,以某种方式停止纱线来生成一些文件。事实证明,当我停止PM2进程时,它仍在背景上运行。

以下是您可以检查是否遇到相同问题的方法:

  1. pm2 stop -namepm2停止PM2
  2. 进程使用PM2删除-namePM2删除所有(如果您没有其他进程运行)
  3. 运行pm2 ls确认没有,则没有其他运行
  4. 日志的 过程当前用户使用其他用户
  5. 运行PM2 LS登录。如果仍有进程正在运行,请停止它们。
  6. 纱构建PM2开始

After searching for answers for days, I couldn't find the solution to my problem.
Only to find out that it's a pm2 issue somehow stopping yarn to generate some files. It turns out that when I stopped pm2 processes, it was still running on the background.

Here's how you can check if you have the same problem:

  1. Stop pm2 process with pm2 stop --name or pm2 stop all (if you have no other processes running)
  2. Delete the process using pm2 delete --name or pm2 delete all (if you have no other processes running)
  3. Run pm2 ls to confirm that there is no other process running
  4. Log your current user out and login using a different user
  5. Run pm2 ls. If there are still processes running, stop them.
  6. Redeploy with yarn build and pm2 start
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文