2017-12-20 5 views
5

現在、node.jsアプリケーションではtypecriptを使用しています。私は自分のアプリをコンパイルして、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のパスマッピング機能を使い始めるまで働いています。今、私は単にエラーを取得する:

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

this github commentからモジュール名は明らかに出力コンパイルされたJavaScriptにマップされていません。

マイ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文を書き換えないのですか?これは、タイプコードがモジュール解決で提供している機能を追加するためだけに、バベルやウェブパックのような別のステップを必要とするのは馬鹿だと感じています。

EDIT:さらに掘り下げた後、自分のノード環境で-r tsconfig-paths/registerを使用できることがわかりました(私のtsconfig.jsonファイルにコピーするだけです)。ディレクトリsrc/が存在しないように私は今、私のtsconfig.json baseUrlにを変更する必要があり、

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

問題がある:私はにドッキングウィンドウで私のエントリポイントを切り替えることができます。また、解決がモジュール 'util'のために働いていないことに気付きました(私のアプリケーションがクラッシュする原因となっているのは、明らかにnode_modulesフォルダ内のutilをnode.js utilライブラリの代わりに使用しています)。

答えて

1

あなたが供給源によると、あなたが使用することができるかもしれないが(例えば、第1パスAS最後のパスを解決):

"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.

も、試しましたトレースモジュールの解像度tsc - トレース解像度

+0

ts-nodeは現在の設定で正しくコンパイルされて実行されます。 – FrankerZ

関連する問題