BUG复现:Cannot find name ‘require’. Do you need to install type definitions for node
以前在做上传图片时,某个组件使用 require 引用过 “ali-oss” 的包,今早在将项目内的包全部卸载并重新安装了一遍,然后就报警告了…
解决方案
打开项目根目录下的 tsconfig.json 文件,在 types 数组中添加 node 字符串即可
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"webpack-env",
// 添加 node 字符串即可
"node"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
在 types 数组中添加 node 字符串即可解决此类问题