无法启动 Nuxt: 无法读取未定义的属性 (读取 ‘srcDir’)

huangapple go评论79阅读模式
英文:

Cannot start Nuxt: Cannot read properties of undefined (reading 'srcDir')

问题

Description:
我在尝试启动我的Nuxt.js应用程序时遇到了错误。我看到的错误消息如下:

无法启动nuxt:无法读取未定义的属性(读取'srcDir')
在module.exports(node_modules\@nuxtjs\dotenv\lib\module.js:9:24)
在installModule(/D:/muse/node_modules/@nuxt/kit/dist/index.mjs:2409:101)
在async initNuxt(/D:/muse/node_modules/nuxt/dist/index.mjs:3237:7)
在async load(/D:/muse/node_modules/nuxi/dist/chunks/dev.mjs:205:9)
在async Object.invoke(/D:/muse/node_modules/nuxi/dist/chunks/dev.mjs:249:5)
在async _main(/D:/muse/node_modules/nuxi/dist/cli.mjs:49:20)

这是我的package.json文件

{
  "name": "nuxt-app",
  "private": true,
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "devDependencies": {
    "@nuxt/devtools": "latest",
    "@nuxtjs/tailwindcss": "^6.8.0",
    "@types/node": "^18.16.19",
    "autoprefixer": "^10.4.14",
    "nuxt": "^3.6.3",
    "nuxt-icon": "^0.4.2",
    "postcss": "^8.4.26",
    "tailwind-scrollbar": "^3.0.4",
    "tailwindcss": "^3.3.3"
  },
  "dependencies": {
    "@headlessui/vue": "^1.7.14",
    "@nuxtjs/dotenv": "^1.4.1",
    "@tailwindcss/forms": "^0.5.4",
    "axios": "^1.4.0",
    "bootstrap": "^5.3.0",
    "tailwind-scrollbar-hide": "^1.1.7"
  }
}

我尝试调查这个问题,似乎与@nuxtjs/dotenv模块有关。错误似乎发生在该模块的代码的第9行,它试图从一个对象中访问名为'srcDir'的属性,但该属性未定义。

我已经检查了我的nuxt.config.js文件,配置似乎是正确的。@nuxtjs/dotenv模块已正确安装在我的项目中,并且我有一个包含所需环境变量的有效.env文件。

有人可以帮助我理解为什么会发生这个错误以及如何解决它吗?任何见解或建议将不胜感激。
我随时提供所需的详细信息。
谢谢。

这是我的nuxtconfig.js文件

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  modules: ["@nuxtjs/tailwindcss", "nuxt-icon", "@nuxtjs/dotenv"],
  css: ["@/assets/css/main.css"],
  postcss: {
    plugins: {
      tailwindcss: {},
      autoprefixer: {},
    },
  },
  app: {
    head: {
      title: "Muse",
      meta: [
        {
          name: "description",
          content: "",
        },
      ],
      link: [{}],
    },
  },
});
英文:

Description:
I'm encountering an error while trying to start my Nuxt.js application. The error message I'm seeing is as follows:

Cannot start nuxt: Cannot read properties of undefined (reading 'srcDir')
at module.exports (node_modules\@nuxtjs\dotenv\lib\module.js:9:24)
at installModule (/D:/muse/node_modules/@nuxt/kit/dist/index.mjs:2409:101)
at async initNuxt (/D:/muse/node_modules/nuxt/dist/index.mjs:3237:7)
at async load (/D:/muse/node_modules/nuxi/dist/chunks/dev.mjs:205:9)
at async Object.invoke (/D:/muse/node_modules/nuxi/dist/chunks/dev.mjs:249:5)
at async _main (/D:/muse/node_modules/nuxi/dist/cli.mjs:49:20)

this is my package.json file

{
  "name": "nuxt-app",
  "private": true,
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "devDependencies": {
    "@nuxt/devtools": "latest",
    "@nuxtjs/tailwindcss": "^6.8.0",
    "@types/node": "^18.16.19",
    "autoprefixer": "^10.4.14",
    "nuxt": "^3.6.3",
    "nuxt-icon": "^0.4.2",
    "postcss": "^8.4.26",
    "tailwind-scrollbar": "^3.0.4",
    "tailwindcss": "^3.3.3"
  },
  "dependencies": {
    "@headlessui/vue": "^1.7.14",
    "@nuxtjs/dotenv": "^1.4.1",
    "@tailwindcss/forms": "^0.5.4",
    "axios": "^1.4.0",
    "bootstrap": "^5.3.0",
    "tailwind-scrollbar-hide": "^1.1.7"
  }
}

I have tried to investigate the issue and it seems to be related to the @nuxtjs/dotenv module. The error seems to be occurring in the module's code at line 9, where it tries to access a property named 'srcDir' from an object, but it's undefined.

I have already checked my nuxt.config.js file, and the configuration seems to be correct. The @nuxtjs/dotenv module is properly installed in my project, and I have a valid .env file with the required environment variables.

Could anyone help me understand why this error is happening and how to resolve it? Any insights or suggestions would be greatly appreciated.
I'm ready to also provide as much details as needed.
Thank you.

Here is my nuxtconfig.js file

    // https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  modules: ["@nuxtjs/tailwindcss", "nuxt-icon", "@nuxtjs/dotenv"],
  css: ["@/assets/css/main.css"],
  postcss: {
    plugins: {
      tailwindcss: {},
      autoprefixer: {},
    },
  },
  app: {
    head: {
      title: "Muse",
      meta: [
        {
          name: "description",
          content: "",
        },
      ],
      link: [{}],
    },
  },
});

答案1

得分: 1

  1. 卸载 @nuxtjs/dotenv 以供环境变量使用。
  2. 仅使用 .env 文件来存储环境变量。
  3. 在 nuxt.config.js 文件中使用类似 process.env.variableName 的方式来引用 .env 中的变量。
英文:
  1. Uninstall @nuxtjs/dotenv for environment variables.
  2. Use only .env file for environment variables.
  3. Use variables from .env like process.env.variableName in nuxt.config.js file.

huangapple
  • 本文由 发表于 2023年7月24日 17:55:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定