{"componentChunkName":"component---src-templates-post-js","path":"/post/the-basics-of-package-json-in-node-js-and-npm","result":{"data":{"contentfulHero":{"coverImage":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/1jqMTE2RJBiMLIcZAnu6It/5495c6ec7df10a55768df442e73fe4b2/home-hero-min.jpg","fileName":"home-hero-min.jpg"}}}},"pageContext":{"post":{"id":"3063210b-1dd4-51ea-8360-4b76be1c5447","slug":"the-basics-of-package-json-in-node-js-and-npm","title":"The Basics of Package.json in Node.js and npm","createdAt":"May 30, 2020","tweetText":null,"type":"blog","video":null,"podcast":null,"categories":[{"id":"50768c9e-fc69-55ce-8a68-88617e5bcfbd","name":"How To","slug":"how-to"},{"id":"4384d042-dd09-5c7b-967b-95eab1334a2c","name":"Node.js","slug":"node-js"}],"image":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/2Fdb4tkf4JgHLCFEzpSWJg/7cc53c0c539a56b2fea629b3daa5b8cc/19-min.jpg","fileName":"19-min.jpg"}},"author":{"name":"NodeSource","bio":{"childMarkdownRemark":{"html":"<p>This blog post was first published in NodeSource blog. Find out more <a href=\"https://nodesource.com/blog\">here</a></p>"}},"avatar":{"file":{"fileName":"mark-circle-light-1-solid@4x.png","url":"//images.ctfassets.net/xmu5vdhtphau/6TzBUzOu342mquRuYsxWhj/27416699cd376436863f29e85851ff19/mark-circle-light-1-solid_4x.png"}}},"bodyContent":{"childMarkdownRemark":{"html":"<p>This article was first published in <a href=\"https://nodesource.com/blog/the-basics-of-package-json-in-node-js-and-npm/\">NodeSource blog</a> on March 2017.</p>\n<hr>\n<p>The <code>package.json</code> file is core to the Node.js ecosystem and is a basic part of understanding and working with Node.js, npm, and even modern JavaScript. The <code>package.json</code> is used as what equates to a manifest about applications, modules, packages, and more - it's a tool to that's used to make modern development streamlined, modular, and efficient.</p>\n<p>As a developer in the Node.js ecosystem, understanding the basics of <code>package.json</code> is one of the first steps to really kicking off your development experience with Node.js.</p>\n<p>Because of how <strong>essential</strong> understanding the basics of <code>package.json</code> is to development with Node.js, I've gone through and outlined some of the most common and important properties of a <code>package.json</code> file that you'll need to use <code>package.json</code> effectively.</p>\n<h2>Identifying Metadata Inside <code>package.json</code></h2>\n<h3>The <code>name</code> property</h3>\n<p>The <code>name</code> property in a <code>package.json</code> file is one of the fundamental components of the <code>package.json</code> structure. At its core, <code>name</code> is a string that is <em>exactly</em> what you would expect - the name of the module that the <code>package.json</code> is describing.</p>\n<p>Inside your <code>package.json</code>, the <code>name</code> property as a string would look something like this:</p>\n<pre><code class=\"language-json\">    \"name\": \"metaverse\"\n</code></pre>\n<p>Despite having only a few material restrictions (a max length of 214 characters, can't begin with <code>.</code> or <code>_</code>, no uppercase letters, and no characters that aren't URL-friendly), one interesting aspect of the <code>name</code> property is that, there have been software ecosystems that have developed standard naming conventions that enable discoverability simply by using the <code>name</code> property. </p>\n<p>A few examples of this kind of namespacing are <a href=\"http://npmsearch.com/?q=babel-plugin\"><code>babel-plugin-</code> for Babel</a> and the the <a href=\"http://npmsearch.com/?q=-loader\">webpack <code>-loader</code></a> tooling.</p>\n<h3>The <code>version</code> property</h3>\n<p>The <code>version</code> property is a key part of a <code>package.json</code>, as it denotes the current version of the module that the <code>package.json</code> file is describing.</p>\n<p>While the <code>version</code> property isn't <em>required</em> to follow <a href=\"https://nodesource.com/blog/semver-a-primer/\">semver</a>, semver is the standard used by the vast majority of modules and projects in the Node.js ecosystem - and the module version, according to semver, is what you'll typically find in the <code>version</code> property of a <code>package.json</code> file.</p>\n<p>Inside your <code>package.json</code>, the <code>version</code> property as a string using semver could look like this:</p>\n<pre><code class=\"language-json\">    \"version\": \"5.12.4\"\n</code></pre>\n<h3>The <code>license</code> property</h3>\n<p>The <code>license</code> property of a <code>package.json</code> file is used to note what license the module that the <code>package.json</code> file is describing. While there are some complex ways you can use the <code>license</code> property of a <code>package.json</code> file (to do things like dual-licensing or defining your own license), the most typical usage of it is to use a <a href=\"https://spdx.org/licenses/\">SPDX License</a> identifier - some examples that you may recognize are <code>MIT</code>, <code>ISC</code>, and <code>GPL-3.0</code>.</p>\n<p>Inside your <code>package.json</code>, the <code>license</code> property with an <code>MIT</code> license look like this:</p>\n<pre><code class=\"language-json\">    \"license\": \"MIT\"\n</code></pre>\n<div class=\"blog-cta nsolid\" style=\"background-color: #4cb5ff;\">\n\tLooking for more info on npm? Check out our complete guide: \n  <a class=\"button more large\" href=\"https://pages.nodesource.com/npm-guide-ultimate-wb.html?utm_campaign=blogref&utm_source=blog&utm_content=blog-npmbasics\">Read now: The Ultimate Guide to npm</a>\n</div>\n<h3>The <code>description</code> property</h3>\n<p>The <code>description</code> property of a <code>package.json</code> file is a string that contains a human-readable description about the module - basically, it's the module developer's chance to quickly let users know what <em>exactly</em> a module does. The <code>description</code> property is frequently indexed by search tools like <a href=\"http://npmsearch.com/\">npm search</a> and the npm CLI search tool to help find relevant packages based on a search query.</p>\n<p>Inside your <code>package.json</code>, the <code>description</code> property would look like this:</p>\n<pre><code class=\"language-json\">    \"description\": \"The Metaverse virtual reality. The final outcome of all virtual worlds, augmented reality, and the Internet.\"\n</code></pre>\n<h3>The <code>keywords</code> property</h3>\n<p>The <code>keywords</code> property inside a <code>package.json</code> file is, as you may have guessed, a collection of keywords about a module. Keywords can help identify a package, related modules and software, and concepts.</p>\n<p>The <code>keywords</code> property is always going to be an array, with one or more strings as the array's values - each one of these strings will, in turn, be one of the project's keywords.</p>\n<p>Inside your <code>package.json</code>, the <code>keywords</code> array would look something like this:</p>\n<pre><code class=\"language-json\">    \"keywords\": [\n        \"metaverse\",\n        \"virtual reality\",\n        \"augmented reality\",\n        \"snow crash\"\n    ]\n</code></pre>\n<h2>Functional Metadata Inside <code>package.json</code></h2>\n<h3>The <code>main</code> property</h3>\n<p>The <code>main</code> property of a <code>package.json</code> is a direction to the entry point to the module that the <code>package.json</code> is describing. In a Node.js application, when the module is called via a require statement, the module's exports from the file named in the <code>main</code> property will be what's returned to the Node.js application.</p>\n<p>Inside your <code>package.json</code>, the <code>main</code> property, with an entry point of <code>app.js</code>, would look like this:</p>\n<pre><code class=\"language-json\">    \"main\": \"app.js\",\n</code></pre>\n<h3>The <code>repository</code> property</h3>\n<p>The <code>repository</code> property of a <code>package.json</code> is an array that defines <em>where</em> the source code for the module lives. Typically, for open source projects, this would be a public GitHub repo, with the <code>repository</code> array noting that the type of version control is <code>git</code>, and the URL of the repo itself. One thing to note about this is that it's not just a URL the repo can be accessed from, but the full URL that the <em>version control</em> can be accessed from.</p>\n<p>Inside your package.json, the <code>repository</code> property would look like this:</p>\n<pre><code class=\"language-json\">  \"repository\": {\n      \"type\": \"git\",\n      \"url\": \"https://github.com/bnb/metaverse.git\"\n  }\n</code></pre>\n<h3>The <code>scripts</code> property</h3>\n<p>The <code>scripts</code> property of a <code>package.json</code> file is simple conceptually, but is complex functionally to the point that it's used as a build tool by many. </p>\n<p>At its simplest, the <code>scripts</code> property takes an object with as many key/value pairs as desired. Each one of the keys in these key/value pairs is the name of a command that can be run. The corresponding value of each key is the actual command that <em>is</em> run. Scripts are frequently used for testing, building, and streamlining of the needed commands to work with a module. </p>\n<p>Inside your package.json, the <code>scripts</code> property with a <code>build</code> command to execute <code>node app.js</code> (presumably to build your application) and a <code>test</code> command using <a href=\"https://github.com/feross/standard\">Standard</a> would look like this:</p>\n<pre><code class=\"language-json\">    \"scripts\": {\n        \"build\": \"node app.js\",\n        \"test\": \"standard\"\n    }\n</code></pre>\n<h3>The <code>dependencies</code> property</h3>\n<p>The <code>dependencies</code> property of a module's <code>package.json</code> is where dependencies - the <em>other</em> modules that <em>this</em> module uses - are defined. The <code>dependencies</code> property takes an object that has the name and version at which each dependency should be used. Tying things back to the <code>version</code> property defined earlier, the version that a module needs is defined. Do note that you'll frequently find carets (<code>^</code>) and tildes (<code>~</code>) included with package versions. These are the notation for version range - taking a deep-dive into these is outside the scope of this article, but you can learn more in our <a href=\"https://nodesource.com/blog/semver-a-primer/\">primer on semver</a>.</p>\n<p>Inside your package.json, the <code>dependencies</code> property of your module may look something like this:</p>\n<pre><code class=\"language-json\">  \"dependencies\": {\n    \"async\": \"^0.2.10\",\n    \"npm2es\": \"~0.4.2\",\n    \"optimist\": \"~0.6.0\",\n    \"request\": \"~2.30.0\",\n    \"skateboard\": \"^1.5.1\",\n    \"split\": \"^0.3.0\",\n    \"weld\": \"^0.2.2\"\n  },\n</code></pre>\n<h3>The <code>devDependencies</code> property</h3>\n<p>The <code>devDependencies</code> property of a <code>package.json</code> is almost identical to the <code>dependencies</code> property in terms of structure, with a key difference. The <code>dependencies</code> property is used to define the dependencies that a module needs to run in <em>production</em>. The <code>devDependencies</code> property is <em>usually</em> used to define the dependencies the module needs to run in <em>development</em>.</p>\n<p>Inside your package.json, the <code>devDependencies</code> property would look something like this:</p>\n<pre><code class=\"language-json\">    \"devDependencies\": {\n        \"escape-html\": \"^1.0.3\",\n        \"lucene-query-parser\": \"^1.0.1\"\n    }\n</code></pre>\n<h2>Want to keep going?</h2>\n<p>If you want to keep learning about Node.js, npm, package.json, and development with the Node.js stack, I've got some <strong>awesome</strong> articles for you. </p>\n<p>We <em>also</em> have a guide on some <a href=\"https://nodesource.com/blog/nine-fantastic-utilities-for-the-node-js-developer\">great utilities for Node.js developers</a> - if you want to kick your developer experience to 11, be sure to check it out to find some tools to help you get there.</p>\n<p>The goal with this guide was to help kickstart you with <code>package.json</code> for development with Node.js and npm. If you want to take the leap and ensure that you're <em>always</em> on solid footing with Node.js and npm modules, you should check out <a href=\"https://nodesource.com/products/certified-modules/technical-details?utm_campaign=blogref&#x26;utm_source=blog&#x26;utm_content=blog-npmbasics\">NodeSource Certified Modules</a> - an awesome tool to help ensure that you spend more time building applications and less time worrying about modules.</p>\n<div class=\"blog-cta certified-modules\">\n\tLearn more and create your free account \n  <a class=\"button more large\" href=\"https://accounts.nodesource.com/sign-up-blogref/?utm_campaign=blogref&utm_source=blog&utm_content=blog-npmbasics\">Create your NodeSource account</a>\n</div>"}}}}}}