尝试编译ts文件时收到以下错误:
node_modules/@types/node/util.d.ts(121,88): error TS2304: Cannot find name 'Symbol'.
我做了一些阅读,发现这可能与没有在tsconfig.json
文件中声明正确的目标或lib选项有关。我尝试了一些其他操作,例如将目标更改为“ es15”,并在库中包含“ es2015”,但是运气不高。
我正在使用本教程作为我的项目的基础: https://itnext.io/building-restful-web-apis-with-node-js-express-mongodb-and-typescript-part-1-2-195bdaf129cf
文件结构:
dist
lib
├──controllers
| ├──controller.ts
|
├──models
| ├──model.ts
|
├──routes
| ├──routes.ts
|
├──app.ts
├──server.ts
node_modules
package.json
tsconfig.json
tsconfig.json:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"noImplicitThis": false,
"removeComments": true,
"experimentalDecorators": true,
"strictNullChecks": true,
"moduleResolution": "node",
"pretty": true,
"sourceMap": true,
"allowJs": true,
"noLib": false,
"jsx": "react",
"outDir": "./dist",
"lib": ["es2017"],
"baseUrl": "./lib"
},
"include": [
"lib/**/*.ts"
],
"exclude": [
"node_modules"
]
}
model.ts:
import * as mongodb from 'mongodb'
import * as fs from 'fs'
const filepath = __dirname + '/../file.txt'
function asyncReadFile(filepath: string, type: string) {
return new Promise((resolve, reject) => {
fs.readFile(filepath, (err, data) => {
console.log("Reading file...")
err ? reject(err) : resolve(data)
})
})
}
asyncReadFile(filepath, 'utf-8')
答案 0 :(得分:0)
似乎您并未包括es7 target所需的所有库。 如果将目标降级到es5并删除libs选项,那应该很好。
答案 1 :(得分:0)
我不确定es2017是否可以正常使用
我正在使用
"target":"es5"
lib最初位于tsconfig.ts
"lib":[]
仍然出现错误。
我在这个github post上找到了解决方案,并且有效。总之,
使用
编辑您的tsconfig.ts文件 "lib": [
"es2015"
]
我的节点版本:8.11.2和npm版本:5.6.0,即使有帮助。