Misha Krul
Misha Krul

Reputation: 397

Cannot find name 'Symbol' when compiling typescript

I'm receiving the following error when trying to compile a ts file:

node_modules/@types/node/util.d.ts(121,88): error TS2304: Cannot find name 'Symbol'.

I did some reading and saw that this can be linked to not having the correct target or lib options declared in the tsconfig.json file. I've tried a few different things such as changing the target to "es15" and including "es2015" in the lib, but I am not having much luck.

I am using this tutorial as a basis for my project: https://itnext.io/building-restful-web-apis-with-node-js-express-mongodb-and-typescript-part-1-2-195bdaf129cf

File structure:

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')

Upvotes: 2

Views: 3483

Answers (2)

user3792812
user3792812

Reputation: 155

I am not sure for es2017 will it work or not but

I was using

"target":"es5"

and lib was initially in tsconfig.ts

"lib":[]

and still getting error.

I found solution at this github post and it worked. In summary,

edit your tsconfig.ts file with

 "lib": [
  "es2015"
]

my node version : 8.11.2 and npm version : 5.6.0 , just if that helps.

Upvotes: 2

Doğancan Arabacı
Doğancan Arabacı

Reputation: 3982

It seems like you are not including all required libraries for es7 target. If you downgrade your target to es5 and remove libs option, you should be good.

Upvotes: 1

Related Questions