인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

인프런 커뮤니티 질문&답변

Green Bak님의 프로필 이미지

작성한 질문수

탄탄한 백엔드 NestJS, 기초부터 심화까지

express + ts 개발 환경 셋업 & hello world!

실행하면 오류가 나네요.

작성

·

252

2

안녕하세요.

코드를 빌드하려고 실행하면 오류가 나네요.

윈도우의 기본 콘솔인 cmd 로 `tsc` 를 실행하니까 컴파일은 돼서, `npm run start` 로 실행을 하니까

이렇게 되네요.

다운로드 한 `package.json` 파일에서, 

`express` 랑 `@types/express` 추가했고,

{
  "scripts": {
    "build""tsc",
    "start:dev""tsc-watch --onSuccess \"node dist/app.js\"",
    "prestart""npm run build",
    "start""node dist/app.js"
  },
  "devDependencies": {
    "@types/express""^4.17.13",
    "@types/node""^15.3.0",
    "prettier""^2.2.1",
    "tsc""^2.0.3",
    "tsc-watch""^4.4.0",
    "typescript""^4.3.5"
  },
  "dependencies": {
    "express""^4.17.1"
  }
}

app.ts 파일을

import {default as expressExpressRequestResponsefrom 'express'

const app: Express = express()

const port: number = 8000


app.get('/', (req: Requestres: Response=> {
  console.log(req)
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app at ${port}`)
})

이렇게 코딩해서 실행했어요.

구글링을 해봤지만 딱히 해결책을 얻지 못해 질문 남깁니다.

답변 부탁드립니다.

답변 1

0

윤상석님의 프로필 이미지
윤상석
지식공유자

안녕하세요. Geen Bak님 :)

윈도우의 기본 콘솔인 cmd 로 `tsc` 를 실행하니까 컴파일은 돼서, `npm run start` 로 실행을 하니까...

컴파일에 성공은 하셨으나 설정한 컴파일 옵션에 맞지 않는 문법을 사용하셔서 오류가 났습니다.

import { default as express, Request, Response } from "express";

이 부분을 아래와 같이 변경하셔야 합니다.

import * as express from "express";
import { Request, Response } from "express";

만일, 

import { default as express, Request, Response } from "express";

방식으로 사용하고 싶으시다면,

tsconfig.json 파일을 아래와 같이 "esModuleInterop": true 를 추가해주세요.

{
"compilerOptions": {
"esModuleInterop": true, // 추가!
"strict": true,
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES5",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./src",
"incremental": true
},
"include": ["src/**/*"]
}