inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

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

실행하면 오류가 나네요.

294

Green Bak

작성한 질문수 1

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}`)
})

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

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

답변 부탁드립니다.

tsc express mongodb nodejs ssr NestJS

답변 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/**/*"]
}

프로젝트 환경 세팅할 때 최신 노드 버젼을 사용하시는 분들은 참고하셔도 좋을 것 같아요~

2

77

1

DTO에 대한 질문

1

86

2

백엔드 MVC에서 View의 역할은 무엇인가요?

1

91

2

추가 업데이트 관련 건

0

89

2

nest js 버전문제

0

79

2

mongdb 스키마 공식 문서와 형태가 다른 이유 궁금합니다.

0

103

1

라인 끝에 에러 표시(eslint) 때문에 구글 찾아 보니.

0

72

1

전체 고양이 조회 라우터 중 error.message 오류

0

66

1

캡슐화 추가 설명 중 단일책임원칙 관련 질문

0

103

0

42강 고양이끼리 소통 댓글 구현 중 Schema hasn't been registered for model 'comments' 에러 해결

0

80

1

채팅 이슈

0

132

1

모듈이 더 이상 지원하지 않는답니다

0

205

1

오류가 있습니다

0

105

1

import 에서 오류가 납니다

0

126

1

이런 오류가 나옵니다

0

99

1

에러가 발생합니다

0

108

1

프론트 에러 뜨는데 수정 안해주시나요

0

154

1

emit() broadcast.emit() 질문있습니다

0

100

1

서버연결이 안됩니다.

1

402

1

[PM2][ERROR] Command not found

0

516

1

S3에 업로드까지는 성공했는데 사진이 나오지 않습니다.

0

242

1

error_code : Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.ts(2339)

0

598

1

jwt를 따로 연습하고 있는데 env를 못읽는 것 같습니다.

0

316

2

Ec2로 안하시는 이유가 있을까요?

0

341

1