Typescript path-mapping does not translate to node application

Typescript is currently used in node.js. I compiled my application and I use the typescript path matching function .

'api / *' should be searched in the root folder. Using the following works in development:

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

tsconfig-paths/register allows you to correctly translate the request, and ts-node will correctly execute / run the application. Now the problem arises when I switch to production. Earlier in production, I simply ran tsc in a folder and moved the contents of outDir (dist /) in / app to my docker image and ran node /app/index.js . This worked until I started using the typescript path display function. Now I just get the error message:

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

From this github comment, the module names do not seem to appear in the output of compiled javascript.

My tsconfig.json file:

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

What is the recommended approach for using relative path mapping in a node.js environment? If typescript is the one making permission, why shouldn't he rewrite require requests? It feels silly to involve another step, such as babel or webpack, only to add the functions that typescript provides with module permission.

EDIT: after extra digging, I found that I can use -r tsconfig-paths/register in my node environment (I just need to copy the tsconfig.json file). I can switch my docker entry point to:

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

The problem is that now I need to change my tsconfig.json baseUrl, since the src/ directory does not exist. In addition, I noticed that permission does not work for the util module (it apparently uses the utility in my node_modules folder instead of the node.js util library), which causes my application to crash.

+5
source share
1 answer

According to the source you provided, you can use (for example, resolve the last AS path in the first way):

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

The problem is that now I need to change my tsconfig.json baseUrl, since the src / directory does not exist.

typescript function display path :

The flexibility of rootDirs is not limited to specifying a list of physical source directories that are logically combined. The enclosed array can include any number of arbitrary arbitrary directory names, whether they exist or not. This allows the compiler to capture complex layout and execution functions, such as conditional inclusion and project-specific plugins, in a safe way.

You also tried the resolution of the trace module: tsc --traceResolution

+1
source

Source: https://habr.com/ru/post/1274233/


All Articles