Advertisement
Aluf

Specifics of npm's Package.json handling - By Aluf

Mar 14th, 2015
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.92 KB | None | 0 0
  1. ##########################################
  2. #Specifics of npm's package.json handling#
  3. ##########################################
  4.  
  5. ########################################################################
  6. ########################################################################
  7. #### Posts © Aluf ####
  8. #### http://D3F4ULTS.chatango.com ####
  9. #### ####
  10. _____ .__ _____ ####
  11. / _ \ | | __ ___/ ____\ ####
  12. / /_\ \| | | | \ __\ ####
  13. / | \ |_| | /| | ####
  14. \____|__ /____/____/ |__| © Aluf-[http://pastebin.com/u/Aluf] ####
  15. \/ ####
  16. #### Message me anytime for any doubts .CC's i post might not ####
  17. #### be working everytime,so message me for fresh cc's . ####
  18. #### Website : www.pastebing.tk ####
  19. #### If you need suggestion for the pastebin bots of chatango ####
  20. #### message me,lol i have run all the bots here myself,so ####
  21. #### i guess ,i know which is the worst and best . ####
  22. #### Do not ask for my bot code on pm ,i quit years back . ####
  23. #### If you have doubts regarding other codes on my pastebin ####
  24. #### Still message me on D3F4ULTS or 93. [93 FOR COMPLAINS] ####
  25. #### I Have been getting a lot of messages lately for cc's ####
  26. #### and the worst part is many want to know to hack cc ####
  27. #### I cannot help you with that ,you have to find a way yourself ####
  28. #### but ofc you can always ask for cc's but don't ask me how to ####
  29. ____ _ _ _____ ####
  30. / _ \/ \ / \ /\/ / ####
  31. | / \|| | | | ||| __\ ####
  32. | |-||| |_/\| \_/|| | ####
  33. \_/ \|\____/\____/\_/ © PMR [ Add me before messaging ] ####
  34. ########################################################################
  35.  
  36.  
  37. Description
  38.  
  39. This document is all you need to know about what's required in your package.json file. It must be actual JSON, not just a JavaScript object literal.
  40.  
  41. A lot of the behavior described in this document is affected by the config settings described in npm-config(7).
  42.  
  43. name
  44.  
  45. The most important things in your package.json are the name and version fields. Those are actually required, and your package won't install without them. The name and version together form an identifier that is assumed to be completely unique. Changes to the package should come along with changes to the version.
  46.  
  47. The name is what your thing is called. Some tips:
  48.  
  49. Don't put "js" or "node" in the name. It's assumed that it's js, since you're writing a package.json file, and you can specify the engine using the "engines" field. (See below.)
  50. The name ends up being part of a URL, an argument on the command line, and a folder name. Any name with non-url-safe characters will be rejected. Also, it can't start with a dot or an underscore.
  51. The name will probably be passed as an argument to require(), so it should be something short, but also reasonably descriptive.
  52. You may want to check the npm registry to see if there's something by that name already, before you get too attached to it. https://www.npmjs.com/
  53. A name can be optionally prefixed by a scope, e.g. @myorg/mypackage. See npm-scope(7) for more detail.
  54.  
  55. version
  56.  
  57. The most important things in your package.json are the name and version fields. Those are actually required, and your package won't install without them. The name and version together form an identifier that is assumed to be completely unique. Changes to the package should come along with changes to the version.
  58.  
  59. Version must be parseable by node-semver, which is bundled with npm as a dependency. (npm install semver to use it yourself.)
  60.  
  61. More on version numbers and ranges at semver(7).
  62.  
  63. description
  64.  
  65. Put a description in it. It's a string. This helps people discover your package, as it's listed in npm search.
  66.  
  67. keywords
  68.  
  69. Put keywords in it. It's an array of strings. This helps people discover your package as it's listed in npm search.
  70.  
  71. homepage
  72.  
  73. The url to the project homepage.
  74.  
  75. NOTE: This is not the same as "url". If you put a "url" field, then the registry will think it's a redirection to your package that has been published somewhere else, and spit at you.
  76.  
  77. Literally. Spit. I'm so not kidding.
  78.  
  79. bugs
  80.  
  81. The url to your project's issue tracker and / or the email address to which issues should be reported. These are helpful for people who encounter issues with your package.
  82.  
  83. It should look like this:
  84.  
  85. { "url" : "http://github.com/owner/project/issues"
  86. , "email" : "[email protected]"
  87. }
  88. You can specify either one or both values. If you want to provide only a url, you can specify the value for "bugs" as a simple string instead of an object.
  89.  
  90. If a url is provided, it will be used by the npm bugs command.
  91.  
  92. license
  93.  
  94. You should specify a license for your package so that people know how they are permitted to use it, and any restrictions you're placing on it.
  95.  
  96. The simplest way, assuming you're using a common license such as BSD-3-Clause or MIT, is to just specify the standard SPDX ID of the license you're using, like this:
  97.  
  98. { "license" : "BSD-3-Clause" }
  99. You can check the full list of SPDX license IDs. Ideally you should pick one that is OSI approved.
  100.  
  101. It's also a good idea to include a LICENSE file at the top level in your package.
  102.  
  103. people fields: author, contributors
  104.  
  105. The "author" is one person. "contributors" is an array of people. A "person" is an object with a "name" field and optionally "url" and "email", like this:
  106.  
  107. { "name" : "Barney Rubble"
  108. , "email" : "[email protected]"
  109. , "url" : "http://barnyrubble.tumblr.com/"
  110. }
  111. Or you can shorten that all into a single string, and npm will parse it for you:
  112.  
  113. "Barney Rubble <[email protected]> (http://barnyrubble.tumblr.com/)
  114. Both email and url are optional either way.
  115.  
  116. npm also sets a top-level "maintainers" field with your npm user info.
  117.  
  118. files
  119.  
  120. The "files" field is an array of files to include in your project. If you name a folder in the array, then it will also include the files inside that folder. (Unless they would be ignored by another rule.)
  121.  
  122. You can also provide a ".npmignore" file in the root of your package, which will keep files from being included, even if they would be picked up by the files array. The ".npmignore" file works just like a ".gitignore".
  123.  
  124. main
  125.  
  126. The 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.
  127.  
  128. This should be a module ID relative to the root of your package folder.
  129.  
  130. For most modules, it makes the most sense to have a main script and often not much else.
  131.  
  132. bin
  133.  
  134. A lot of packages have one or more executable files that they'd like to install into the PATH. npm makes this pretty easy (in fact, it uses this feature to install the "npm" executable.)
  135.  
  136. To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.
  137.  
  138. For example, myapp could have this:
  139.  
  140. { "bin" : { "myapp" : "./cli.js" } }
  141. So, when you install myapp, it'll create a symlink from the cli.js script to /usr/local/bin/myapp.
  142.  
  143. If you have a single executable, and its name should be the name of the package, then you can just supply it as a string. For example:
  144.  
  145. { "name": "my-program"
  146. , "version": "1.2.5"
  147. , "bin": "./path/to/program" }
  148. would be the same as this:
  149.  
  150. { "name": "my-program"
  151. , "version": "1.2.5"
  152. , "bin" : { "my-program" : "./path/to/program" } }
  153. man
  154.  
  155. Specify either a single file or an array of filenames to put in place for the man program to find.
  156.  
  157. If only a single file is provided, then it's installed such that it is the result from man <pkgname>, regardless of its actual filename. For example:
  158.  
  159. { "name" : "foo"
  160. , "version" : "1.2.3"
  161. , "description" : "A packaged foo fooer for fooing foos"
  162. , "main" : "foo.js"
  163. , "man" : "./man/doc.1"
  164. }
  165. would link the ./man/doc.1 file in such that it is the target for man foo
  166.  
  167. If the filename doesn't start with the package name, then it's prefixed. So, this:
  168.  
  169. { "name" : "foo"
  170. , "version" : "1.2.3"
  171. , "description" : "A packaged foo fooer for fooing foos"
  172. , "main" : "foo.js"
  173. , "man" : [ "./man/foo.1", "./man/bar.1" ]
  174. }
  175. will create files to do man foo and man foo-bar.
  176.  
  177. Man files must end with a number, and optionally a .gz suffix if they are compressed. The number dictates which man section the file is installed into.
  178.  
  179. { "name" : "foo"
  180. , "version" : "1.2.3"
  181. , "description" : "A packaged foo fooer for fooing foos"
  182. , "main" : "foo.js"
  183. , "man" : [ "./man/foo.1", "./man/foo.2" ]
  184. }
  185. will create entries for man foo and man 2 foo
  186.  
  187. directories
  188.  
  189. The CommonJS Packages spec details a few ways that you can indicate the structure of your package using a directories object. If you look at npm's package.json, you'll see that it has directories for doc, lib, and man.
  190.  
  191. In the future, this information may be used in other creative ways.
  192.  
  193. directories.lib
  194.  
  195. Tell people where the bulk of your library is. Nothing special is done with the lib folder in any way, but it's useful meta info.
  196.  
  197. directories.bin
  198.  
  199. If you specify a bin directory, then all the files in that folder will be added as children of the bin path.
  200.  
  201. If you have a bin path already, then this has no effect.
  202.  
  203. directories.man
  204.  
  205. A folder that is full of man pages. Sugar to generate a "man" array by walking the folder.
  206.  
  207. directories.doc
  208.  
  209. Put markdown files in here. Eventually, these will be displayed nicely, maybe, someday.
  210.  
  211. directories.example
  212.  
  213. Put example scripts in here. Someday, it might be exposed in some clever way.
  214.  
  215. repository
  216.  
  217. Specify the place where your code lives. This is helpful for people who want to contribute. If the git repo is on GitHub, then the npm docs command will be able to find you.
  218.  
  219. Do it like this:
  220.  
  221. "repository" :
  222. { "type" : "git"
  223. , "url" : "http://github.com/npm/npm.git"
  224. }
  225.  
  226. "repository" :
  227. { "type" : "svn"
  228. , "url" : "http://v8.googlecode.com/svn/trunk/"
  229. }
  230. The URL should be a publicly available (perhaps read-only) url that can be handed directly to a VCS program without any modification. It should not be a url to an html project page that you put in your browser. It's for computers.
  231.  
  232. scripts
  233.  
  234. The "scripts" property is a dictionary containing script commands that are run at various times in the lifecycle of your package. The key is the lifecycle event, and the value is the command to run at that point.
  235.  
  236. See npm-scripts(7) to find out more about writing package scripts.
  237.  
  238. config
  239.  
  240. A "config" object can be used to set configuration parameters used in package scripts that persist across upgrades. For instance, if a package had the following:
  241.  
  242. { "name" : "foo"
  243. , "config" : { "port" : "8080" } }
  244. and then had a "start" command that then referenced the npm_package_config_port environment variable, then the user could override that by doing npm config set foo:port 8001.
  245.  
  246. See npm-config(7) and npm-scripts(7) for more on package configs.
  247.  
  248. dependencies
  249.  
  250. Dependencies are specified in a simple object that maps a package name to a version range. The version range is a string which has one or more space-separated descriptors. Dependencies can also be identified with a tarball or git URL.
  251.  
  252. Please do not put test harnesses or transpilers in your dependencies object. See devDependencies, below.
  253.  
  254. See semver(7) for more details about specifying version ranges.
  255.  
  256. version Must match version exactly
  257. >version Must be greater than version
  258. >=version etc
  259. <version
  260. <=version
  261. ~version "Approximately equivalent to version" See semver(7)
  262. ^version "Compatible with version" See semver(7)
  263. 1.2.x 1.2.0, 1.2.1, etc., but not 1.3.0
  264. http://... See 'URLs as Dependencies' below
  265. * Matches any version
  266. "" (just an empty string) Same as *
  267. version1 - version2 Same as >=version1 <=version2.
  268. range1 || range2 Passes if either range1 or range2 are satisfied.
  269. git... See 'Git URLs as Dependencies' below
  270. user/repo See 'GitHub URLs' below
  271. tag A specific version tagged and published as tag See npm-tag(1)
  272. path/path/path See Local Paths below
  273. For example, these are all valid:
  274.  
  275. { "dependencies" :
  276. { "foo" : "1.0.0 - 2.9999.9999"
  277. , "bar" : ">=1.0.2 <2.1.2"
  278. , "baz" : ">1.0.2 <=2.3.4"
  279. , "boo" : "2.0.1"
  280. , "qux" : "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0"
  281. , "asd" : "http://asdf.com/asdf.tar.gz"
  282. , "til" : "~1.2"
  283. , "elf" : "~1.2.3"
  284. , "two" : "2.x"
  285. , "thr" : "3.3.x"
  286. , "lat" : "latest"
  287. , "dyl" : "file:../dyl"
  288. }
  289. }
  290. URLs as Dependencies
  291.  
  292. You may specify a tarball URL in place of a version range.
  293.  
  294. This tarball will be downloaded and installed locally to your package at install time.
  295.  
  296. Git URLs as Dependencies
  297.  
  298. Git urls can be of the form:
  299.  
  300. git://github.com/user/project.git#commit-ish
  301. git+ssh://user@hostname:project.git#commit-ish
  302. git+ssh://user@hostname/project.git#commit-ish
  303. git+http://user@hostname/project/blah.git#commit-ish
  304. git+https://user@hostname/project/blah.git#commit-ish
  305. The commit-ish can be any tag, sha, or branch which can be supplied as an argument to git checkout. The default is master.
  306.  
  307. GitHub URLs
  308.  
  309. As of version 1.1.65, you can refer to GitHub urls as just "foo": "user/foo-project". Just as with git URLs, a commit-ish suffix can be included. For example:
  310.  
  311. {
  312. "name": "foo",
  313. "version": "0.0.0",
  314. "dependencies": {
  315. "express": "visionmedia/express",
  316. "mocha": "visionmedia/mocha#4727d357ea"
  317. }
  318. }
  319. Local Paths
  320.  
  321. As of version 2.0.0 you can provide a path to a local directory that contains a package. Local paths can be saved using npm install --save, using any of these forms:
  322.  
  323. ../foo/bar
  324. ~/foo/bar
  325. ./foo/bar
  326. /foo/bar
  327. in which case they will be normalized to a relative path and added to your package.json. For example:
  328.  
  329. {
  330. "name": "baz",
  331. "dependencies": {
  332. "bar": "file:../foo/bar"
  333. }
  334. }
  335. This feature is helpful for local offline development and creating tests that require npm installing where you don't want to hit an external server, but should not be used when publishing packages to the public registry.
  336.  
  337. devDependencies
  338.  
  339. If someone is planning on downloading and using your module in their program, then they probably don't want or need to download and build the external test or documentation framework that you use.
  340.  
  341. In this case, it's best to map these additional items in a devDependencies object.
  342.  
  343. These things will be installed when doing npm link or npm install from the root of a package, and can be managed like any other npm configuration param. See npm-config(7) for more on the topic.
  344.  
  345. For build steps that are not platform-specific, such as compiling CoffeeScript or other languages to JavaScript, use the prepublish script to do this, and make the required package a devDependency.
  346.  
  347. For example:
  348.  
  349. { "name": "ethopia-waza",
  350. "description": "a delightfully fruity coffee varietal",
  351. "version": "1.2.3",
  352. "devDependencies": {
  353. "coffee-script": "~1.6.3"
  354. },
  355. "scripts": {
  356. "prepublish": "coffee -o lib/ -c src/waza.coffee"
  357. },
  358. "main": "lib/waza.js"
  359. }
  360. The prepublish script will be run before publishing, so that users can consume the functionality without requiring them to compile it themselves. In dev mode (ie, locally running npm install), it'll run this script as well, so that you can test it easily.
  361.  
  362. peerDependencies
  363.  
  364. In some cases, you want to express the compatibility of your package with an host tool or library, while not necessarily doing a require of this host. This is usually referred to as a plugin. Notably, your module may be exposing a specific interface, expected and specified by the host documentation.
  365.  
  366. For example:
  367.  
  368. {
  369. "name": "tea-latte",
  370. "version": "1.3.5",
  371. "peerDependencies": {
  372. "tea": "2.x"
  373. }
  374. }
  375. This ensures your package tea-latte can be installed along with the second major version of the host package tea only. The host package is automatically installed if needed. npm install tea-latte could possibly yield the following dependency graph:
  376.  
  377. Trying to install another plugin with a conflicting requirement will cause an error. For this reason, make sure your plugin requirement is as broad as possible, and not to lock it down to specific patch versions.
  378.  
  379. Assuming the host complies with semver, only changes in the host package's major version will break your plugin. Thus, if you've worked with every 1.x version of the host package, use "^1.0" or "1.x" to express this. If you depend on features introduced in 1.5.2, use ">= 1.5.2 < 2".
  380.  
  381. bundledDependencies
  382.  
  383. Array of package names that will be bundled when publishing the package.
  384.  
  385. If this is spelled "bundleDependencies", then that is also honorable.
  386.  
  387. optionalDependencies
  388.  
  389. If a dependency can be used, but you would like npm to proceed if it cannot be found or fails to install, then you may put it in the optionalDependencies object. This is a map of package name to version or url, just like the dependencies object. The difference is that build failures do not cause installation to fail.
  390.  
  391. It is still your program's responsibility to handle the lack of the dependency. For example, something like this:
  392.  
  393. try {
  394. var foo = require('foo')
  395. var fooVersion = require('foo/package.json').version
  396. } catch (er) {
  397. foo = null
  398. }
  399. if ( notGoodFooVersion(fooVersion) ) {
  400. foo = null
  401. }
  402.  
  403. // .. then later in your program ..
  404.  
  405. if (foo) {
  406. foo.doFooThings()
  407. }
  408. Entries in optionalDependencies will override entries of the same name in dependencies, so it's usually best to only put in one place.
  409.  
  410. engines
  411.  
  412. You can specify the version of node that your stuff works on:
  413.  
  414. { "engines" : { "node" : ">=0.10.3 <0.12" } }
  415. And, like with dependencies, if you don't specify the version (or if you specify "*" as the version), then any version of node will do.
  416.  
  417. If you specify an "engines" field, then npm will require that "node" be somewhere on that list. If "engines" is omitted, then npm will just assume that it works on node.
  418.  
  419. You can also use the "engines" field to specify which versions of npm are capable of properly installing your program. For example:
  420.  
  421. { "engines" : { "npm" : "~1.0.20" } }
  422. Note that, unless the user has set the engine-strict config flag, this field is advisory only.
  423.  
  424. engineStrict
  425.  
  426. If you are sure that your module will definitely not run properly on versions of Node/npm other than those specified in the engines object, then you can set "engineStrict": true in your package.json file. This will override the user's engine-strict config setting.
  427.  
  428. Please do not do this unless you are really very very sure. If your engines object is something overly restrictive, you can quite easily and inadvertently lock yourself into obscurity and prevent your users from updating to new versions of Node. Consider this choice carefully. If people abuse it, it will be removed in a future version of npm.
  429.  
  430. os
  431.  
  432. You can specify which operating systems your module will run on:
  433.  
  434. "os" : [ "darwin", "linux" ]
  435. You can also blacklist instead of whitelist operating systems, just prepend the blacklisted os with a '!':
  436.  
  437. "os" : [ "!win32" ]
  438. The host operating system is determined by process.platform
  439.  
  440. It is allowed to both blacklist, and whitelist, although there isn't any good reason to do this.
  441.  
  442. cpu
  443.  
  444. If your code only runs on certain cpu architectures, you can specify which ones.
  445.  
  446. "cpu" : [ "x64", "ia32" ]
  447. Like the os option, you can also blacklist architectures:
  448.  
  449. "cpu" : [ "!arm", "!mips" ]
  450. The host architecture is determined by process.arch
  451.  
  452. preferGlobal
  453.  
  454. If your package is primarily a command-line application that should be installed globally, then set this value to true to provide a warning if it is installed locally.
  455.  
  456. It doesn't actually prevent users from installing it locally, but it does help prevent some confusion if it doesn't work as expected.
  457.  
  458. private
  459.  
  460. If you set "private": true in your package.json, then npm will refuse to publish it.
  461.  
  462. This is a way to prevent accidental publication of private repositories. If you would like to ensure that a given package is only ever published to a specific registry (for example, an internal registry), then use the publishConfig dictionary described below to override the registry config param at publish-time.
  463.  
  464. publishConfig
  465.  
  466. This is a set of config values that will be used at publish-time. It's especially handy if you want to set the tag or registry, so that you can ensure that a given package is not tagged with "latest" or published to the global public registry by default.
  467.  
  468. Any config values can be overridden, but of course only "tag" and "registry" probably matter for the purposes of publishing.
  469.  
  470. See npm-config(7) to see the list of config options that can be overridden.
  471.  
  472. DEFAULT VALUES
  473.  
  474. npm will default some values based on package contents.
  475.  
  476. "scripts": {"start": "node server.js"}
  477.  
  478. If there is a server.js file in the root of your package, then npm will default the start command to node server.js.
  479.  
  480. "scripts":{"preinstall": "node-gyp rebuild"}
  481.  
  482. If there is a binding.gyp file in the root of your package, npm will default the preinstall command to compile using node-gyp.
  483.  
  484. "contributors": [...]
  485.  
  486. If there is an AUTHORS file in the root of your package, npm will treat each line as a Name <email> (url) format, where email and url are optional. Lines which start with a # or are blank, will be ignored.
  487.  
  488. See Also
  489.  
  490. semver(7)
  491. npm-init(1)
  492. npm-version(1)
  493. npm-config(1)
  494. npm-config(7)
  495. npm-help(1)
  496. npm-faq(7)
  497. npm-install(1)
  498. npm-publish(1)
  499. npm-rm(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement