{"action":"opened","issue":{"url":"https://api.github.com/repos/npm/npm/issues/10506","labels_url":"https://api.github.com/repos/npm/npm/issues/10506/labels{/name}","comments_url":"https://api.github.com/repos/npm/npm/issues/10506/comments","events_url":"https://api.github.com/repos/npm/npm/issues/10506/events","html_url":"https://github.com/npm/npm/issues/10506","id":118192604,"number":10506,"title":"enhance the \"main\" field of the package.json for entry folder","user":{"login":"taijiweb","id":6938640,"avatar_url":"https://avatars.githubusercontent.com/u/6938640?v=3","gravatar_id":"","url":"https://api.github.com/users/taijiweb","html_url":"https://github.com/taijiweb","followers_url":"https://api.github.com/users/taijiweb/followers","following_url":"https://api.github.com/users/taijiweb/following{/other_user}","gists_url":"https://api.github.com/users/taijiweb/gists{/gist_id}","starred_url":"https://api.github.com/users/taijiweb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/taijiweb/subscriptions","organizations_url":"https://api.github.com/users/taijiweb/orgs","repos_url":"https://api.github.com/users/taijiweb/repos","events_url":"https://api.github.com/users/taijiweb/events{/privacy}","received_events_url":"https://api.github.com/users/taijiweb/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-11-21T11:58:51Z","updated_at":"2015-11-21T11:58:51Z","closed_at":null,"body":"I hope that the \"main\" field can specify a folder as the module entry, so that the root folder can be not polluted by many JavaScript files and folders.\r\n\r\nnpm should can distinct file entry from folder entry by a tailing slash.\r\n\r\nSay, for the folder structure like below:\r\n\r\n\tfoo\r\n\t|----src\r\n\t|    |---- a.coffee\r\n\t|    |---- b.coffee\r\n\t|    \\---- index.coffee\r\n\t|\r\n\t|----lib\r\n\t|    |---- a.js\r\n\t|    |---- b.js\r\n\t|    \\---- index.js\r\n\t|\r\n\t|---- .gitignore\r\n\t|---- gulpfile.js\r\n\t|---- package.json\r\n\t|---- license\r\n\t\\---- readme.md\r\n\r\nAnd with the package.json file as below:\r\n\r\n    {\r\n      ...\r\n      main: \"lib/\"\r\n      ...\r\n    }\r\n\r\n\r\nBy this method, we can tell npm to use foo/lib/index.js as the module entry. If there exists only one file in lib/ folder, it will be used, and it should can use other file name.\r\n\r\nWe can also use the syntax below to specify a entry file for the \"foo\" module\r\n\r\n    {\r\n      ...\r\n      main: \"lib/ @ path/to/foo-entry.js\"\r\n      ...\r\n    }\r\n\r\nSome spaces before or/and after @ are permitted and can be omitted.\r\n\r\nWithout the improvement above, we must write require(\"foo/lib/a\"), require(\"foo/lib/b\"). It's a bit annoying. To avoid this under, the current package.json specification, we must put all the folders and script files under the root folder, like below:\r\n\r\n\t|----src\r\n\t|    |---- a.coffee\r\n\t|    |---- b.coffee\r\n\t|    \\---- index.coffee\r\n\t|\r\n\t|---- a.js\r\n\t|---- b.js\r\n\t|---- index.js\r\n\t|---- .gitignore\r\n\t|---- gulpfile.js\r\n\t|---- package.json\r\n\t|---- license\r\n\t\\---- readme.md\r\n\r\nHere two annoying problems exists: \r\n\r\n1. the module's root folder is a mess with mixing the script files and other kind of files. \r\n2. It's more annoying while we are writing the build script.\r\n\r\nE.g. for \"gulp clean\", we need write the code below\r\n\r\n    del = require(\"del\");\r\n\tgulp.task(\"clean\", function(callback){ \r\n      del([\"*.js\", \"!gulpfile.js\"], callback); \r\n    })\r\n\r\nEspecially while there are a lot of sub folders and scripts in foo/src folder, some code like below need to be written:\r\n\r\n    del = require(\"del\");\r\n\tgulp.task(\"clean\", function(callback){ \r\n      del([ \"*/\", \r\n            \"!doc/\", \r\n            \"!test/\",\r\n            // ... other folder should not be deleted \r\n            \"*.js\", \r\n            \"!gulpfile.js\",\r\n            // ... other js files should not be deleted\r\n          ], callback); \r\n    })\r\n\r\nI'm always afraid to delete some other files by mistake while I am doing this.\r\n\r\nWith the improvement described as above, we can use `{ ... main: \"lib/\", ...}` in package.json to enable writing require(\"foo/a\"),  require(\"foo/b\") instead, simply. So we can organize the module and api more conveniently, and small sub modules is encouraged.\r\n\r\nI think the syntax `{ main: \"entry-folder/\" } or `{ main: \"entry-folder/ @path/to/entry-file.js\" }' will not break the existed package.json. We can talk about this more deeply.\r\n\r\nI'm not sure this improvement should be implemented in npm or in node.js. I'll submit this in node.js project, too.\r\n\r\nBelow is the specification for the \"main\" field in npm document:\r\n\r\n\tmain\r\n\t\r\n\tThe main field is a module ID that is the primary entry point to your program. That is, if your package is named foo, and a user installs it, and then does require(\"foo\"), then your main module's exports object will be returned.\r\n\t\r\n\tThis should be a module ID relative to the root of your package folder.\r\n\t\r\n\tFor most modules, it makes the most sense to have a main script and often not much else."}}