Property ‘context‘ does not exist on type ‘NodeRequire‘.ts(2339)

文章讲述了在Vue3项目中使用require.context引发的错误,尝试通过安装@types/webpack-env和修改tsconfig.json配置来解决,但问题未完全消除。最终发现需在tsconfig.vitest.json中添加webpack-env到types才消除编译错误,然而浏览器仍报requireisnotdefined,因为require是webpack的方法,不适用于Vite构建的项目。建议使用Vite的导入机制替代require.context。

最近使用vue3官方脚手架创建的项目,在执行下面的代码后报错:

const files = require.context('./modules', false, /\.js$/)

查阅相关资料,有人提出解决方法如下,安装@types/webpack-env依赖:

npm i -D @types/webpack-env

修改tsconfig.json:

{
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "module": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5",
    "types": [
      "webpack",
      "webpack-env" // 加入这个
    ]
  },
}

但是发现,开始的报错并没有消失。

在项目根目录找到tsconfig.vitest.json,在"types": [“node”, “jsdom”]中加入"webpack-env"

{
  "extends": "./tsconfig.app.json",
  "exclude": [],
  "compilerOptions": {
    "composite": true,
    "lib": [],
    "types": ["node", "jsdom","webpack-env"]
  }
}

报错消失。

虽然编辑器不报错了,但是浏览器却无法识别这个方法:
Ucaught ReferenceError: require is not defined

结论:这个方法是webpack提供的,既然都用Vite了就别用这个方法了。

它的作用无非就是引入了一个目录下的所有文件,大不了手动引入。

ERROR in src/modules/data-map/data-table/assets-table-detail/blood/views/g6-blood-table.vue:37:14 TS2339: Property '$axios' does not exist on type 'CombinedVueInstance<Vue, object, object, object, Record<never, any>>'. 35 | if (isInit) { 36 | request = () => > 37 | vm.$axios | ^^^^^^ 38 | .$get(tableLineageBlood(tableId), { 39 | params: { 40 | upstreamLevel: 1, ERROR in src/modules/data-map/data-table/assets-table-detail/blood/views/g6-blood-table.vue:44:19 TS7006: Parameter 'res' implicitly has an 'any' type. 42 | }, 43 | }) > 44 | .then(res => res.payload) | ^^^ 45 | } else { 46 | request = () => 47 | vm.$axios ERROR in src/modules/data-map/data-table/assets-table-detail/blood/views/g6-blood-table.vue:47:14 TS2339: Property '$axios' does not exist on type 'CombinedVueInstance<Vue, object, object, object, Record<never, any>>'. 45 | } else { 46 | request = () => > 47 | vm.$axios | ^^^^^^ 48 | .$get(lineageBlood, { 49 | params: { 50 | nodeId: data.nodeId, ERROR in src/modules/data-map/data-table/assets-table-detail/blood/views/g6-blood-table.vue:55:19 TS7006: Parameter 'res' implicitly has an 'any' type. 53 | }, 54 | }) > 55 | .then(res => res.payload) | ^^^ 56 | } 57 | return request().then((payload: any) => { 58 | return { ERROR in src/modules/data-map/data-table/assets-table-detail/blood/views/g6-blood-table.vue:112:22 TS2339: Property '$router' does not exist on type 'CombinedVueInstance<Vue, object, object, object, Record<never, any>>'. 110 | value: data.dataCollectSuccess 111 | ? `<a class="tooltip-value primary" href="${ > 112 | vm.$router.resolve(`/data-map/data-table/${data.tableId}`).href | ^^^^^^^ 113 | }"> ${data.tableName} </a>` 114 | : data.tableName, 115 | }, FATAL Nuxt build error 09:02:49 at WebpackBundler.webpackCompile (node_modules/@nuxt/webpack/dist/webpack.js:1888:21) at async WebpackBundler.build (node_modules/@nuxt/webpack/dist/webpack.js:1853:5) at async Builder.build (node_modules/@nuxt/builder/dist/builder.js:249:5) at async ensureBuild (node_modules/@nuxt/cli/dist/cli-generate.js:117:3) at async Object.run (node_modules/@nuxt/cli/dist/cli-generate.js:234:7) at async NuxtCommand.run (node_modules/@nuxt/cli/dist/cli-index.js:356:7) ╭─────────────────────────────╮ │ │ │ ✖ Nuxt Fatal Error │ │ │ │ Error: Nuxt build error │ │ │ ╰─────────────────────────────╯
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值