{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/247","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/247/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/247/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/247/events","html_url":"https://github.com/Microsoft/TypeScript/issues/247","id":38735235,"number":247,"title":"Support looking for modules under node_modules when importing","user":{"login":"chanon","id":357737,"avatar_url":"https://avatars.githubusercontent.com/u/357737?v=3","gravatar_id":"","url":"https://api.github.com/users/chanon","html_url":"https://github.com/chanon","followers_url":"https://api.github.com/users/chanon/followers","following_url":"https://api.github.com/users/chanon/following{/other_user}","gists_url":"https://api.github.com/users/chanon/gists{/gist_id}","starred_url":"https://api.github.com/users/chanon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chanon/subscriptions","organizations_url":"https://api.github.com/users/chanon/orgs","repos_url":"https://api.github.com/users/chanon/repos","events_url":"https://api.github.com/users/chanon/events{/privacy}","received_events_url":"https://api.github.com/users/chanon/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Committed","name":"Committed","color":"009800"},{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Suggestion","name":"Suggestion","color":"006b75"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":102,"created_at":"2014-07-25T14:22:14Z","updated_at":"2015-12-02T07:04:12Z","closed_at":"2015-08-31T17:06:26Z","body":"__Update 5 November 2015__\r\n\r\nThe functionality requested below is currently implemented in  ```typescript@next ``` with one main difference:\r\n\r\nInstead of having  ```typescript.main ``` and  ```typescript.definition ``` properties, there is only one  ```typings ``` property which you can point to either a  ```d.ts ``` file or a normal  ```.ts ``` file.\r\n\r\nIf you're developing a module to just use locally, you can have the  ```typings ``` point to a  ```.ts ``` file, but if you plan to publish the module, it is recommended to have it point to a  ```d.ts ``` file. This is because you don't want your module consumers to recompile your module files, just consume its typings.\r\n\r\nI have setup an example of using this here:\r\nhttps://github.com/chanon/typescript_module_example\r\n\r\nThere is a wiki page here that has more information:\r\nhttps://github.com/Microsoft/TypeScript/wiki/Typings-for-npm-packages\r\n\r\nThank you TypeScript devs and all contributors.\r\n\r\n__Original issue / feature request follows__\r\n\r\n---------------\r\n\r\n## Motivation\r\n\r\nIn TypeScript it is a lot harder to re-use typescript modules compared to re-using npm modules in JavaScript.\r\n\r\nIt would be beneficial if the typescript compiler is smart enough to look in node_modules folders and package.json files.\r\n\r\nThe reason is so that npm module developers that use TypeScript might be able to start writing and distributing modules through npm itself. TypeScript would be able to piggyback on npm's infrastructure and wide support.\r\n\r\n## Example for node_modules\r\n\r\nIf we had:\r\n\r\n```\r\n./node_modules/concator/index.ts\r\n./myApp.ts\r\n```\r\n\r\nAnd in index.ts we had:\r\n\r\n```\r\nexport function concat(param1: string, param2:string): string {\r\n      return param1 + ' ' + param2;\r\n}\r\n```\r\n\r\nin myApp.ts:\r\n\r\n```\r\nimport concator = require('concator');  // loads the module from node_modules\r\nvar result = concator.concat('I like', 'this.');\r\nvar wontWork = concator.concat('this will fail');  // compile error, concat needs 2 params\r\n```\r\n\r\nSo basically, the compiler is smart enough to find the module in node_modules and it automatically uses the typescript version (index.ts).\r\n\r\nThen when the code is compiled to JavaScript, it naturally uses the JavaScript version.\r\n\r\n## Importing Folders as Modules \r\n\r\nA more basic case is supporting Node.js's popular rule of http://nodejs.org/api/modules.html#modules_folders_as_modules as suggested by @vvakame below and in the closed (semi-duplicate) #207 issue.\r\n\r\n## typescript.main in package.json \r\n\r\nThere could be an addition to package.json files to specify where the main .ts file of a TypeScript npm module is. This would be similar to the ```main``` key that specifies where the main JavaScript / .js file is but for TypeScript instead.\r\n\r\nEg package.json for an npm module named \"myModule\" located at ```node_modules/myModule/package.json```\r\n\r\n```\r\n{\r\n     \"main\": \"./dist/index.js\",\r\n     \"typescript\": {\r\n          \"main\": \"./src/index.ts\"\r\n     }\r\n}\r\n```\r\n\r\nIn this example ```node_modules/myModule/src/index.ts``` would be the main TypeScript file and doing an ```import myModule = require(\"myModule\");``` would import the ```node_modules/myModule/src/index.ts``` file.\r\n\r\nFor a JavaScript coder writing ```var myModule = require(\"myModule\");``` in a JavaScript file, the require would load the ```node_modules/myModule/dist/index.js``` file as usual.\r\n\r\nAs can be seen in this example, the TypeScript src is in the ```node_modules/module-name/src``` folder and the compiled JS files would be in ```node_modules/module-name/dist```.\r\n\r\nSomething like this could be a (semi) standard for TypeScript npm modules so that the TypeScript source is cleanly separated from the compiled JavaScript output.\r\n\r\nA more basic case as suggested by @vvakame below is supporting the popular Node.js rule of http://nodejs.org/api/modules.html#modules_folders_as_module\r\n\r\n## typescript.definition in package.json\r\n\r\nAnother possible key for package.json for non-TypeScript (plain JavaScript) npm modules could be ```typescript.definition```. This would point to a .d.ts file that defines the module for TypeScript users of the npm module.\r\n\r\nSo that an\r\n```import $ = require('jquery');```\r\nwould automatically read a ```jquery.d.ts``` file defined in the ```typescript.definition``` key in jQuery's package.json and make ```$``` the correct type.\r\n\r\nExample format:\r\n```\r\n{\r\n     \"main\": \"./dist/index.js\",\r\n     \"typescript\": {\r\n          \"definition\": \"./index.d.ts\"\r\n     }\r\n}\r\n```\r\n(This format is already used by tsd as @Bartvds explains below.)\r\n\r\nThen us TypeScript coders would just have to try to get as many non-TypeScript npm module maintainers to merge our pull requests that have our .d.ts files and package.json ```typescript.definition``` keys.\r\n\r\nIf we succeed with that, then TypeScript coders' life would be bliss ... no more separate managing of DefinitelyTyped .d.ts files. Just npm install and you get your TypeScript definitions too! Automatically and up-to-date with the module version installed.\r\n\r\n## List of Benefits\r\n\r\nWhat we get from all of this is \r\n* npm modules can be written in TypeScript.\r\n* Both TypeScript and JavaScript coders can use these modules\r\n* The module users who use TypeScript get the benefits of static typing without the module coder (or user) having to use the usual method of writing (or generating) separate .d.ts files (so when the sourcecode of the module is already in TypeScript we don't have to have another .d.ts file to maintain)\r\n* There would be a way to easily re-use and distribute TypeScript modules using npm which everyone is already familiar with\r\n* It could help promote TypeScript usage itself, as JavaScript coders who use the npm modules written in TypeScript might see how nice / better the TypeScript source is and try switching to it. Or they might want to contribute to the module and since the source is in TypeScript they would have to learn it which may lead to them liking it and deciding to use it in their own projects.\r\n* Basically it would help grow the TypeScript community through all the code sharing that could result. Right now everyone's TypeScript code is mostly their own / in their own silo. This is actually probably an extremely important thing as not having a proper/easy way to share/distribute/re-use modules is probably limiting the growth of the language. [1]\r\n* Re-use of modules in internal projects would be a lot less of a hassle and would reduce the need for all those relative paths to .d.ts files and relative paths in module requires, and general .d.ts stuff. (Right now when writing TypeScript code, I find I have to learn to be good with counting ../../ s)\r\n* This approach also supports Browserify/Webpack as Browserify/Webpack also look under node_modules, so this allows for easy reusable / npm distributed TypeScript modules for both the server and browser\r\n* Non-TypeScript npm modules can easily be used in TypeScript with type information through the ```typescript.definition``` key addition. This allows .d.ts definition files to be packaged together with the npm module so that updating an npm module will automatically update its .d.ts definition file. This removes the need to update .d.ts files manually.\r\n* Using definition files through the ```typescript.definition``` is simpler because it is simply an ```import moduleName = require(\"moduleName\")``` statement with no need for a separate ```///<reference ...``` \r\n* Using definition files through the ```typescript.definition``` should also allow the use of different versions of the module in the same code base without type names clashing.\r\n\r\n## Detailed Proposal\r\n\r\n@Nemo157 has written a very detailed proposal for how all of this should work at:\r\n\r\n**Proposed TypeScript Require Resolution Semantics**\r\nhttps://gist.github.com/Nemo157/f20064a282ee620f3877\r\n\r\nAn addition in the proposal is the usage of ```/typings``` folders which can hold definition files that can be automatically managed by tools such as ```tsd``` for JavaScript npm modules that won't include definition files in their repositories.\r\n\r\n## Final Supporting Facts\r\n\r\nSince TypeScript compiles to JavaScript which runs mainly in two places: node.js and in browsers, supporting node_modules is beneficial to both places (practically all of where TypeScript is used) because of npm and Browserify/Webpack. \r\n\r\nie. there is no reason to come up with a different scheme when node_modules is what all TypeScript users use already for maybe 75%-100% of all their JavaScript code. (Taking out 25% for maybe RequireJS users.)\r\n\r\n### Footnotes\r\n\r\n[1] -  BTW I see there is a NuGet package manager (?) from Microsoft that can distribute typescript modules (?), but coming from a node.js focused (non .NET focused) background, I don't see NuGet becoming widely used outside of Microsoft focused shops, especially as npm is *the* standard for node.js and is a leading standard for client side JavaScript too. If I didn't use TypeScript I would have never heard of NuGet. The average node.js / client side JavaScript coder would probably prefer using npm, the same tool they use already rather than having to use something Microsoft specific such as NuGet. (I don't actually know a thing about NuGet so what I'm saying here might not actually matter.)"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/161203188","html_url":"https://github.com/Microsoft/TypeScript/issues/247#issuecomment-161203188","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/247","id":161203188,"user":{"login":"wmono","id":374925,"avatar_url":"https://avatars.githubusercontent.com/u/374925?v=3","gravatar_id":"","url":"https://api.github.com/users/wmono","html_url":"https://github.com/wmono","followers_url":"https://api.github.com/users/wmono/followers","following_url":"https://api.github.com/users/wmono/following{/other_user}","gists_url":"https://api.github.com/users/wmono/gists{/gist_id}","starred_url":"https://api.github.com/users/wmono/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wmono/subscriptions","organizations_url":"https://api.github.com/users/wmono/orgs","repos_url":"https://api.github.com/users/wmono/repos","events_url":"https://api.github.com/users/wmono/events{/privacy}","received_events_url":"https://api.github.com/users/wmono/received_events","type":"User","site_admin":false},"created_at":"2015-12-02T07:04:12Z","updated_at":"2015-12-02T07:04:12Z","body":"@chanon Pardon me; user error. It looked like webpack was struggling with the \"bare\" `require` but the problem lay elsewhere."}}