Nuxt 3.5.1 构建时出现错误 “ERROR 无法读取未定义的属性 (读取 ‘sys’) (x4)”

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

Nuxt 3.5.1 get error on build " ERROR Cannot read properties of undefined (reading 'sys') (x4) "

问题

我使用Nuxt 3.5.1制作了一个应用程序,以下是script标签的内容:

<script lang="ts" setup>
     import { IProduct } from './types';
     const p = defineProps<IProduct>();
</script>

types.ts文件的内容如下:

export type IProduct =  {
   name?: string;
   id?: number;
   price?: number;
   thumbnail?: string;
   title: string;
}

export interface IProductSecond {
   n: string;
   id: number;
}

但是当我运行以下命令时:

npm run dev

我收到以下错误消息:

Nuxt 3.5.1 构建时出现错误 “ERROR 无法读取未定义的属性 (读取 ‘sys’) (x4)”

如果我将以下代码:

const p = defineProps<IProduct>();

替换为:

const p = defineProps<{ name?: string;
   id?: number;
   price?: number;
   thumbnail?: string;
   title: string;}>();

那么它可以正常工作。

英文:

I have an application made using Nuxt 3.5.1 this is the script tag :

&lt;script lang=&quot;ts&quot; setup&gt;
     import { IProduct } from &#39;./types&#39;;
     const p = defineProps&lt;IProduct&gt;();
&lt;/script&gt;

types.ts :

export type IProduct =  {
   name?: string;
   id?: number;
   price?: number;
   thumbnail?: string;

   title: string;

}

export interface IProductSecond {
   n: string;
   id: number;
}

but when I run

&gt;npm run  dev 

I get this error :
Nuxt 3.5.1 构建时出现错误 “ERROR 无法读取未定义的属性 (读取 ‘sys’) (x4)”

If I replace :

const p = defineProps&lt;IProduct&gt;();

with

const p = defineProps&lt;{ name?: string;
   id?: number;
   price?: number;
   thumbnail?: string;
   title: string;}&gt;();

It works fine

答案1

得分: 3

不久前,Vue 3.3发布,目前Nuxt不支持Vue 3.3的“Imported Types”。有一种解决方法:

  • 如果package.json中没有typescript,请添加npm i -D typescript (required)
  • 在你的nuxt.config.ts中添加以下代码:
import { existsSync, readFileSync } from "node:fs";

export default defineNuxtConfig({
  vite: {
    vue: {
      script: {
        fs: {
          fileExists(file: string) {
            return existsSync(file);
          },
          readFile(file: string) {
            return readFileSync(file, "utf-8");
          },
        },
      },
    },
  },
});

参考:

英文:

It's not long since Vue 3.3 is dropped and as of now, Nuxt doesn't have built-in support for Vue 3.3's "Imported Types". There's a workaround for this though:

  • If you don't have typescript in package.json, add it npm i -D typescript (required)
  • Add following code in your nuxt.config.ts:
import { existsSync, readFileSync } from &quot;node:fs&quot;;

export default defineNuxtConfig({
  vite: {
    vue: {
      script: {
        fs: {
          fileExists(file: string) {
            return existsSync(file);
          },
          readFile(file: string) {
            return readFileSync(file, &quot;utf-8&quot;);
          },
        },
      },
    },
  },
});

References:

答案2

得分: 0

"vite"在nuxt.config.ts中的配置不再必要。只需安装typescript包即可。

英文:

The config for vite in the nuxt.config.ts isn't necessary anymore. It is enough to install the typescript package.

huangapple
  • 本文由 发表于 2023年5月28日 18:47:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351088.html
匿名

发表评论

匿名网友

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

确定