Definitelytyped: error TS2304: Cannot find name 'Set'. 815 allowedNodeEnvironmentFlags: Set<string>;

Created on 25 Sep 2018  ·  20Comments  ·  Source: DefinitelyTyped/DefinitelyTyped

D:\workshop\www
λ node -v
v8.12.0

D:\workshop\www
λ npm -v
6.4.1

D:\workshop\www
λ npm install typescript -g
C:\Users\dell\AppData\Roaming\npm\tsc -> C:\Users\dell\AppData\Roaming\npm\node_modules\typescript\bin\tsc
C:\Users\dell\AppData\Roaming\npm\tsserver -> C:\Users\dell\AppData\Roaming\npm\node_modules\typescript\bin\tsserver
+ [email protected]
updated 1 package in 3.061s

D:\workshop\www
λ tsc --version
Version 3.0.3

D:\workshop\www
λ mkdir typescript_learning

D:\workshop\www
λ cd typescript_learning

D:\workshop\www\typescript_learning
λ npm init -y
Wrote to D:\workshop\www\typescript_learning\package.json:

{
  "name": "typescript_learning",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}



D:\workshop\www\typescript_learning  ([email protected])
λ tsc --init
message TS6071: Successfully created a tsconfig.json file.

D:\workshop\www\typescript_learning  ([email protected])
λ npm install @types/node --dev-save
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

+ @types/[email protected]
added 1 package from 30 contributors in 1.657s

D:\workshop\www\typescript_learning  ([email protected])
λ touch helloworld.ts

D:\workshop\www\typescript_learning  ([email protected])
λ tsc helloworld.ts
node_modules/@types/node/index.d.ts:815:38 - error TS2304: Cannot find name 'Set'.

815         allowedNodeEnvironmentFlags: Set<string>;

helloworld.ts

var a:string = "HelloWorld"
console.log(a)

What should I do?

Most helpful comment

Same here with target and lib as es6.
I've managed to fix this by installing @types/[email protected].

All 20 comments

Add "lib": ["es6"] or higher.

I'm having the same problem, but addig the lib option on the tsconfig file did not solve it.

Typescript version: 3.1.1
Node typings version: 10.11.5

Steps to reproduce

$ mkdir test
$ cd test
$ npm init -y
$ npm install --save-dev typescript @types/node
$ echo 'console.log("test")' > index.ts
$ ./node_modules/.bin/tsc --init

Add lib: "es2015" to tsconfig.json

$ ./node_modules/.bin/tsc index.ts

node_modules/@types/node/index.d.ts:815:38 - error TS2583: Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

815         allowedNodeEnvironmentFlags: Set<string>;

I talked to numerous people about this, if lib doesn't fix it then at some point your tsconfig isn't being loaded correctly, please ensure that the lib is correctly being loaded together with the file using the include or files option in the tsconfig.json .

@andy-ms should I revert the Set change?
While people really should be using the >es6 lib for use with node this _could_ be seen as a breaking change.

I think you could define interface Set<T> {} below interface SetConstructor {} in node/index.d.ts so that it will work with --lib es5.

same issue.

☁  jest-codelab [master] ⚡  $(npm bin)/tsc ./src/@types/index.ts
node_modules/@types/node/index.d.ts:815:38 - error TS2583: Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

815         allowedNodeEnvironmentFlags: Set<string>;
                                         ~~~

tsconfig.json:

"lib": ["es2015", "es2016"]

package.json:

"devDependencies": {
    "@types/jest": "^23.3.3",
    "@types/node": "^10.11.4",
    "jest": "^23.6.0",
    "ts-jest": "^23.10.4",
    "tslint": "^5.11.0",
    "typescript": "^3.1.1"
  }

@mrdulin I can tell you don't have the latest @types/node installed (currently 10.12.0), because there it's allowedNodeEnvironmentFlags: ReadonlySet<string>; and interface ReadonlySet<T> {} is declared in @types/node/index.d.ts.

@andy-ms Thanks. After npm rm @types/node -D, this issue is gone.

I have the same issue and i resolve it using compilerOptions.types on my tsconfig.json. So basically i only listed the needed @types package in that property.

@rannie-peralta this issue has long been fixed in type by doing an empty ReadonlySet declaration.

I too encountered the same kind of issue.
package.json
{
"name": "map",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"typescript": "^3.4.5"
}
}
tsconfig.json
{
"compilerOptions": {
"module": "amd",
"noImplicitAny": true,
"esModuleInterop": true,
"sourceMap": true,
"jsx": "react",
"jsxFactory": "tsx",
"target": "es6",
"experimentalDecorators": true,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true,
"lib": ["es2015", "es2016"]
},
"include": [
"./src/*"
],
"exclude": [
"node_modules"
]
}
index.js
function displayData(){
var myMap = new Map();
myMap.set("name","xxxxxxxx");
}
command to convert into js file

tsc .src\index.ts
Error :
src/index.ts:4:21 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing thelib` compiler option to es2015 or later.

4 var myMap = new Map();
~~~

Found 1 error.`

@Praveer1981 the solution is literally in the error message.

I came across this same issue and was confused because my tsconfig.json has exactly what the error message says to do. Modifying my tsconfig.json further would not fix the issue.

When you pass a file path to tsc, I believe the tsconfig.json is ignored. This is what was happening to me.

tsc src/foo.ts # results in compiler errors stating to upgrade my lib/target
tsc # works fine because my tsconfig.json was specifying a target of es2015 or later

So if you're getting this and your tsconfig.json looks good, double check if you're passing a custom path to the compiler, and potentially ignoring your config.

Same here

node_modules/@types/react/index.d.ts:377:23 - error TS2583: Cannot find name 'Set'. Do you need to change your target library? Try changing the lib compiler option to es2015 or later.

interactions: Set<SchedulerInteraction>,

Config:

{
  "compilerOptions": {
    "target": "es5",
    "target: "es5", // <= doesn't help
    "lib": [
      "dom",
      "dom.iterable",
      "esnext",
      "es2015", // <= doesn't help
      "es6", // <= doesn't help
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "sourceMap": true,
    "baseUrl": "./",
    "jsx": "preserve"
  },
  "include": [
    "./**/*",
    "*.tsx"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

File I parse that throws:

class Greeter {
  public greeting: string;
  constructor(msg: string) {
    this.greeting = msg;
  }
}

let g = new Greeter('hello');

export { Greeter };

Same here with target and lib as es6.
I've managed to fix this by installing @types/[email protected].

@brunobertoldi
Yes, I can confirm. Your answer fixes the issue.
Here https://github.com/DefinitelyTyped/DefinitelyTyped/issues/29172#issuecomment-527708175

@brunobertoldi, I would like to add to this that I had to open a prompt with administrator privileges to install these typings, but once I did, it worked!

Thank you, by the way.

Thanks for the additional details, @thomasio101!

Same here with target and lib as es6.
I've managed to fix this by installing @types/[email protected].

excellent cool!

I would suggest to follow the way create-react-app works

Inside the compilerOptions

    ...
    "skipLibCheck": true,
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
   ...

The content in lib is totally up to you.

Same here with target and lib as es6.
I've managed to fix this by installing @types/[email protected].

Solves the problem

Was this page helpful?
0 / 5 - 0 ratings