๊ด€๋ฆฌ ๋ฉ”๋‰ด

\(@^0^@)/

TypeScript : Cannot find name 'console' Error ๋ณธ๋ฌธ

์—๋Ÿฌ๋…ธํŠธ

TypeScript : Cannot find name 'console' Error

minjuuu 2022. 8. 2. 18:18
728x90

๐Ÿ˜ฃ ์—๋Ÿฌ ๋ฐœ๊ฒฌ ์›์ธ

function printName(firstName: string, lastName?: string) {
    console.log(firstName);
    console.log(lastName);
}
printName("amy", "park");
printName("amy"); // undefined
printName("amy", undefined); // Optional parameter
ts-node 1-2-function.ts

ts-node๋ฅผ ํ†ตํ•ด, ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ์ฝ˜์†”๋กœ ์ฐ์–ด์„œ ํ™•์ธํ•˜๋ ค ํ–ˆ๋Š”๋ฐ, error ๋ฐœ์ƒํ•˜์˜€๋‹ค.

 

๐Ÿคฉ 1. runtime์ด Node.js์ธ ๊ฒฝ์šฐ ํ•ด๊ฒฐ๋ฒ•

npm i -D @types/node

ํ˜„์žฌ ๋‚ด๊ฐ€ ์ž‘์„ฑํ•˜๊ณ  ์žˆ๋Š” 1-2-function์€ ์ž‘์€ ํด๋”์ด๊ธฐ์—,
๊ทธ๋ณด๋‹ค ๋” ์ƒ์œ„์ธ typescript ํด๋”์— ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์—๋Ÿฌ ํ•ด๊ฒฐ.

 


๐Ÿคฉ 2. ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ํ•ด๊ฒฐ๋ฒ•

'lib' ์ปดํŒŒ์ผ๋Ÿฌ ์˜ต์…˜์„ 'dom'์„ ํฌํ•จํ•˜๊ฒŒ ๋ณ€๊ฒฝํ•ด๋ณด๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Œ.

{
  "compilerOptions": {
    "lib": [
      // ... your other libs
      "dom"
    ],
    // ... rest

  },
}

๐Ÿคฉ 3. ์ตœํ›„์˜ ๋ฐฉ๋ฒ•

node_modules์™€ package-lock.json ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๊ณ , npm install๋กœ vscode๋ฅผ ์žฌ์‹คํ–‰์‹œํ‚จ๋‹ค.

rm -rf node_modules package-lock.json

npm install

 


https://bobbyhadz.com/blog/typescript-cannot-find-name-console#:~:text=To%20solve%20the%20%22Cannot%20find,is%20ran%20in%20the%20browser.

 

Solve - Cannot find name 'console' Error in TypeScript | bobbyhadz

To solve the "Cannot find name `console`" error, install the `node` types if running in Node.js by running `npm i -D @types/node`, or include `dom` in the `lib` array in your `tsconfig.json` file if your code is ran in the browser.

bobbyhadz.com

 

728x90

'์—๋Ÿฌ๋…ธํŠธ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TypeScript : Uncaught ReferenceError: exports is not defined  (0) 2022.03.10
prettier ์—๋Ÿฌ๋‚  ๋•Œ  (0) 2022.02.19
[Error] Unhandled Rejection (TypeError): Cannot read properties of undefined  (0) 2021.12.13
Git push error  (0) 2021.10.15