2017-12-20 1 views
5

현재 node.js 응용 프로그램에서 typescript를 사용하고 있습니다. 내 앱을 컴파일했고, typescript's path mapping feature을 사용하고 있습니다.노드 응용 프로그램에서 Typescript 경로 매핑이 변환되지 않습니다.

'api/*'는 대신 루트 폴더를 확인해야합니다. 개발에 다음 사용하면 작동합니다

nodemon --watch src -e ts,tsx --exec ts-node -r tsconfig-paths/register --disableWarnings ./src/index.ts 

tsconfig-paths/register이 (가) 제대로 번역하고 TS-노드/실행 제대로 응용 프로그램을 실행할 필요가 있습니다. 자, 문제는 생산으로 옮겨 가면서 발생합니다. 이전에는 제작 과정에서 간단히 폴더에 tsc을 실행하고 내 도커 이미지에서 outDir (dist /)의 내용을/app로 이동하고 node /app/index.js을 실행했습니다. 필자는 typescript의 경로 매핑 기능을 사용하기 전까지이 작업을 수행했습니다. this github comment에서

Error: Cannot find module 'api/module/path/here'

모듈 이름이 분명히 출력 컴파일 된 자바 스크립트에 매핑되지 않은 : 지금은 단순히 오류가 발생합니다.

내 tsconfig.json 파일 :

{ 
    "compilerOptions": { 
     "target": "es2015", 
     "module": "commonjs", 
     "moduleResolution": "node", 
     "allowSyntheticDefaultImports": true, 
     "jsx": "react", 
     "allowJs": true, 
     "alwaysStrict": true, 
     "sourceMap": true, 
     "forceConsistentCasingInFileNames": true, 
     "noFallthroughCasesInSwitch": true, 
     "noImplicitReturns": true, 
     "noUnusedLocals": true, 
     "noUnusedParameters": true, 
     "noImplicitAny": false, 
     "noImplicitThis": false, 
     "strictNullChecks": false, 
     "experimentalDecorators": true, 
     "emitDecoratorMetadata": true, 
     "lib": ["es2017", "dom"], 
     "baseUrl": "src", 
     "outDir": "dist", 
     "types": [ 
     "node" 
     ], 
     "paths": { 
     "universal/*": ["../../universal/*"], 
     "api/*": ["*"], 
     "*": ["node_modules/*"] 
     }, 
     "typeRoots": [ 
     "./node_modules/@types", 
     "./src/types" 
     ] 
    }, 
    "include": [ 
     "src/**/*" 
    ] 
} 

Node.js를 환경에서 상대 경로 매핑을 사용에 권장되는 방법은 무엇입니까? typescript가 해상도를 수행하는 경우 왜 require 문을 다시 작성하면 안됩니까? 타이프 스크립트가 모듈 해상도를 제공하는 기능을 추가하기 위해 바벨 (babel)이나 웹팩 (webpack)과 같은 또 다른 단계를 포함하는 것은 어리석은 일입니다.

EDIT : 추가 파기 후 내 노드 환경에 -r tsconfig-paths/register을 사용할 수 있음을 알았습니다 (단지 tsconfig.json 파일에 복사해야 함). 디렉토리 src/가 존재하지 않는 한 지금, 내 tsconfig.json base을 수정해야합니다,

ENTRYPOINT [ "node", "-r", "tsconfig-paths/register", "index.js" ] 

문제는 : 나는에 고정 표시기 내 엔트리 포인트를 전환 할 수 있습니다. 또한, 해상도가 모듈 'util'(node.js 유틸리티 라이브러리 대신 node_modules 폴더에서 util을 사용하고있는 것 같습니다)에서 작동하지 않는 것으로 나타났습니다. 이로 인해 내 응용 프로그램이 중단되는 것입니다.

"rootDirs": [ 
     "src/api/module/path/here", 
     "api/module/path/here" 
    ] 

The problem is, I now need to modify my tsconfig.json baseUrl, as the directory src/ doesn't exist.

typescript's path mapping feature 상태 : 또한

The flexibility of rootDirs is not limited to specifying a list of physical source directories that are logically merged. The supplied array may include any number of ad hoc, arbitrary directory names, regardless of whether they exist or not. This allows the compiler to capture sophisticated bundling and runtime features such as conditional inclusion and project specific loader plugins in a type safe way.

답변

1

이 제공 한 소스에 따르면, 당신은 (예를 들어 첫 번째 경로로 마지막 경로를 해결) 사용할 수 있습니다 , 시도 했습니까 추적 모듈 해상도 : tsc - traceResolution

+0

ts-node는 현재 구성으로 올바르게 컴파일되고 실행됩니다. – FrankerZ

관련 문제