eclipse-typescript icon indicating copy to clipboard operation
eclipse-typescript copied to clipboard

module reolution with node js

Open zebalu opened this issue 9 years ago • 8 comments
trafficstars

I try to use typescript plugin with node.js as module resolver. I have cloned your project and found the parts of this feature is already implemented. (When I finish, I will be happy to share the changes with you) I found that TypeScript.js trys to resolve node_modules, and finally creates the correct file names (e.g.: "node_modules/angular2/core.d.ts") but whent send it to host's fileExists() method, than it always returns false.

Could you help, where to look for further answers? Could it help if it would use absolute paths? Should path came from Java side or should be found in JavaScript?

Thanks in advance!

zebalu avatar Feb 25 '16 17:02 zebalu

The paths used in the TypeScript side are all based on Eclipse paths. Its been a while since I've worked on that part of the code but I believe there are basically 2 paths for each file: an Eclipse virtual path (its relative to a project in Eclipse) and an absolute path in the real file system.

derekcicerone-zz avatar Feb 26 '16 16:02 derekcicerone-zz

@zebalu If you have node_modules in your Eclipse project, I think it's because the compiler option is not set so that moduleResolution is "NodeJs" (2). I've got a change that I'll open this weekend to handle this case.

Typescript, in general, recursively searches every parent directory to look for a node_modules folder. In the case of an Eclipse Typescript project that has node_modules above the root project folder, the compiler currently fails, because it stops searching for node_modules once it reaches the root project. For example, if you have /home/myFolder/myProject and a Typescript project for myProject, then the TS compiler gets files of the form eclipse:/myProject/<file_name>. So if you have node_modules in /home/myFolder/myProject, the TS-compiler will find them if run from the command-line but not when invoked from Eclipse. I haven't figured out how to handle this case yet.

markwongsk avatar Mar 10 '16 18:03 markwongsk

@markwongsk Hi! So far I have found that the problem lies within the gigantic compiler script that uses special logic to resolve node_modules. This part is not like other does not know where the project is, and it never uses absolute paths and I couldn't reach any information to generate them.

In my case it starts from this path information: eclipse:/myProject/src/main/ts/app/main.ts, and tries the following paths (for example angular2/core.d.ts): node_modules/myProject/src/main/ts/app/angular2/core.d.ts node_modules/myProject/src/main/ts/angular2/core.d.ts node_modules/myProject/src/main/angular2/core.d.ts node_modules/myProject/src/angular2/core.d.ts node_modules/myProject/angular2/core.d.ts node_modules/angular2/core.d.ts

this last path would be good, because node_modules are in the project directory, but this path is not resolved by this compiler script, but passed to Node.js, that can not find this file (as far as I got it seems that because it is not in this relative path to my node installation) and it returns failure.

Currently I am stuck, but thank you for any help in advance!

zebalu avatar Mar 10 '16 22:03 zebalu

@zebalu your node_modules is above your eclipse project folder, so Eclipse does not know that it exists. Is it possible to move node_modules one level into myProject? In addition to that, you also need to modify TypeScriptPlugin.java to use NODE_JS as its ModuleResolutionKind:

        store.setDefault(IPreferenceConstants.COMPILER_MODULE_RESOLUTION, ModuleResolutionKind.NODE_JS.toString());

markwongsk avatar Mar 12 '16 01:03 markwongsk

Sorry I was missunderstandable. The node_modules folder are in my eclipse project's folder. The project folder wher I can compile in commandline looks like this:

|-bin |-node_modules |-src |-typings |-package.json |-tsconfig.json |-typings.json

zebalu avatar Mar 12 '16 08:03 zebalu

Hey @zebalu, I've gotten a working version in Eclipse. However, it requires me to explicitly specify the node_module folder that I'm including. For example, when I right click -> properties -> Typescript, I have to specify "node_modules/" as an additional source. I will try and see whether this is avoidable, but debugging the the typescript bridge hasn't been easy.

markwongsk avatar Mar 14 '16 07:03 markwongsk

I'm not able to resolve the node_module

I try to use angular2 in an Dynamic Web Projekt. I am not sure how to setup the projekt.

-> ts files in an seperate src folder. -> WebContent, src ist generated into that folder. -> node_modules where should this folder be

idefixcert avatar May 19 '16 12:05 idefixcert

I resolved it by adding the node_modules path to the Exported Folder(s) section of the project properties. As soon as I did it, it stopped giving me these errors. Try it out if its still a problem.

smrutimo avatar Jun 22 '16 19:06 smrutimo