我在尝试编译ts文件时收到以下错误:
node_modules/@types/node/util.d.ts(121,88): error TS2304: Cannot find name 'Symbol'.
我做了一些阅读,发现这可能与没有在tsconfig.json文件中声明正确的目标或库选项有关。我尝试了一些不同的方法,例如将目标更改为"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.jsontsconfig.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')发布于 2019-04-20 01:23:08
我不确定es2017会不会工作,但是
我用的是
"target":"es5"而lib最初是用tsconfig.ts编写的。
"lib":[]但仍然会出错。
我在这个github post上找到了解决方案,它起作用了。总而言之,
使用编辑tsconfig.ts文件
"lib": [
"es2015"
]我的节点版本: 8.11.2,npm版本: 5.6.0,如果有帮助的话。
发布于 2019-04-19 21:02:26
您似乎没有包含es7目标所需的所有库。如果你将你的目标降级到es5并去掉libs选项,你应该是很好的。
https://stackoverflow.com/questions/55760457
复制相似问题