2017-12-20 129 views
5

में टाइपस्क्रिप्ट पथ-मानचित्रण का अनुवाद नहीं किया जा रहा है वर्तमान में node.js एप्लिकेशन में टाइपस्क्रिप्ट का उपयोग कर रहा है। मैंने अपना ऐप संकलित किया है, और मैं typescript's path mapping feature का उपयोग कर रहा हूं।नोड एप

'api/*' को रूट फ़ोल्डर में देखना चाहिए। विकास में निम्नलिखित का उपयोग करते हुए काम करता है:

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

tsconfig-paths/register सही ढंग से अनुवाद किया जाना है और ts नोड पर अमल/ठीक से एप्लिकेशन चलेगा की आवश्यकता होती है की अनुमति देता है। अब, समस्या तब होती है जब मैं उत्पादन में जाता हूं। अतीत में, मैंने फ़ोल्डर पर tsc भाग लिया, और मेरे डॉकर छवि में आउटडिर (dist /) से/ऐप की सामग्री को स्थानांतरित कर दिया, और node /app/index.js चलाया। यह तब तक काम करता है जब तक मैंने टाइपस्क्रिप्ट की पथ मैपिंग सुविधा का उपयोग करना शुरू नहीं किया।

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

this github comment से

मॉड्यूल नाम जाहिरा तौर पर उत्पादन संकलित जावास्क्रिप्ट पर मैप नहीं कर रहे हैं: अब मैं बस त्रुटि मिलती है।

मेरे 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 वातावरण में रिश्तेदार पथ-मानचित्रण का उपयोग करने के लिए सिफारिश दृष्टिकोण क्या है? यदि टाइपस्क्रिप्ट कोई संकल्प कर रहा है, तो उसे आवश्यकता बयानों को फिर से लिखना क्यों नहीं चाहिए? मॉड्यूल रिज़ॉल्यूशन के साथ टाइपस्क्रिप्ट प्रदान करने वाली कार्यक्षमता को जोड़ने के लिए बस बेबेल या वेबपैक जैसे एक और कदम को शामिल करने के लिए मूर्खतापूर्ण लगता है।

संपादित करें: अतिरिक्त खुदाई के बाद, मैंने पाया कि मैं अपने नोड पर्यावरण में -r tsconfig-paths/register का उपयोग कर सकता हूं (मुझे बस अपनी tsconfig.json फ़ाइल में प्रतिलिपि बनाना है)। मैं करने के लिए डोकर में मेरी entrypoint स्विच कर सकते हैं:

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

समस्या है, मैं अब, के रूप में निर्देशिका src/ मौजूद नहीं है मेरी tsconfig.json baseurl को संशोधित करने की जरूरत है। साथ ही, मैंने देखा कि संकल्प मॉड्यूल 'उपयोग' के लिए काम नहीं कर रहा है (यह स्पष्ट रूप से node.js उपयोग लाइब्रेरी के बजाय मेरे node_modules फ़ोल्डर में उपयोग का उपयोग कर रहा है), जो मेरे ऐप को क्रैश कर रहा है।

उत्तर

1

स्रोत आप आपूर्ति के अनुसार, आप का उपयोग करने में सक्षम हो सकता है (उदाहरण के लिए पिछले पथ पहले पथ के रूप में हल):

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

+0

टीएस-नोड वर्तमान कॉन्फ़िगरेशन के साथ संकलित और सही ढंग से चलता है। – FrankerZ