{"componentChunkName":"component---src-templates-post-js","path":"/post/11-simple-npm-tricks-that-will-knock-your-wombat-socks-off","result":{"data":{"contentfulHero":{"coverImage":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/1jqMTE2RJBiMLIcZAnu6It/5495c6ec7df10a55768df442e73fe4b2/home-hero-min.jpg","fileName":"home-hero-min.jpg"}}}},"pageContext":{"post":{"id":"f9c51868-7064-5f10-beb2-f98643f62cca","slug":"11-simple-npm-tricks-that-will-knock-your-wombat-socks-off","title":"11 Simple npm Tricks That Will Knock Your Wombat Socks Off","createdAt":"Jun 04, 2020","tweetText":null,"type":"blog","video":null,"podcast":null,"categories":[{"id":"a397ebc3-492b-5568-b45d-6d4aa9722be7","name":"Tutorials","slug":"tutorials"},{"id":"4384d042-dd09-5c7b-967b-95eab1334a2c","name":"Node.js","slug":"node-js"}],"image":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/192NyfRtbUtQpMtP9JYYZV/462bac50e7028a042ced07e97434a30e/8-min.jpg","fileName":"8-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 blog post was first published on March 2017. Find out more <a href=\"https://nodesource.com/blog/configuring-your-npmrc-for-an-optimal-node-js-environment\">here</a></p>\n<p>Using npm effectively can be difficult. There are a ton of features built-in, and it can be a daunting task to try to approach learning them.</p>\n<p>Personally, even learning and using just one of these tricks (<code>npm prune</code>, which is #4) saved me from getting rid of unused modules manually by deleting <code>node_modules</code> and re-installing <em>everything</em> with <code>npm install</code>. As you can probably imagine, that was insanely stressful.</p>\n<p>We've compiled this list of 11 simple-to-use npm tricks that will allow you to speed up development using npm, no matter what project you're working on.</p>\n<h2>1. Open a package’s homepage</h2>\n<p><strong>Run:</strong> <code>npm home $package</code></p>\n<p>Running the <code>home</code> command will open the homepage of the package you're running it against. Running against the <code>lodash</code> package will bring you to the Lodash website. This command can run without needing to have the package installed globally on your machine or within the current project.</p>\n<h2>2. Open package’s GitHub repo</h2>\n<p><strong>Run:</strong> <code>npm repo $package</code></p>\n<p>Similar to <code>home</code>, the <code>repo</code> command will open the GitHub repository of the package you're running it against. Running against the <code>express</code> package will bring you to the official Express repo. Also like <code>home</code>, you don’t need to have the package installed.</p>\n<h2>3. Check a package for outdated dependencies</h2>\n<p><strong>Run:</strong> <code>npm outdated</code></p>\n<p>You can run the <code>outdated</code> command within a project, and it will check the npm registry to see if any of your packages are outdated. It will print out a list in your command line of the current version, the wanted version, and the latest version.</p>\n<p><img src=\"https://nodesource.com/media/2016/Aug/Screen_Shot_2016_08_26_at_3_52_53_PM-1472242056432.png\" alt=\"Running npm outdated on a Node project\"></p>\n<h2>4. Check for packages not declared in package.json</h2>\n<p><strong>Run:</strong> <code>npm prune</code></p>\n<p>When you run <code>prune</code>, the npm CLI will run through your <code>package.json</code> and compare it to your project’s <code>/node_modules</code> directory. It will print a list of modules that aren’t in your <code>package.json</code>.</p>\n<p>The <code>npm prune</code> command then strips out those packages, and removes any you haven't manually added to <code>package.json</code> or that were <code>npm install</code>ed without using the <code>--save</code> flag.</p>\n<p><img src=\"https://nodesource.com/media/2016/Aug/Screen_Shot_2016_08_26_at_3_54_00_PM-1472242073013.png\" alt=\"Running npm prune on a Node project\"></p>\n<p><strong>Update:</strong> Thanks to <a href=\"https://twitter.com/EvanHahn\">@EvanHahn</a> for noticing a personal config setting that made <code>npm prune</code> provide a slightly different result than the default <code>npm</code> would provide!</p>\n<h2>5. Lock down your dependencies versions</h2>\n<p><strong>Run:</strong> <code>npm shrinkwrap</code></p>\n<p>Using <code>shrinkwrap</code> in your project generates an <code>npm-shrinkwrap.json</code> file. This allows you to pin the dependencies of your project to the specific version you’re currently using within your <code>node_modules</code> directory. When you run <code>npm install</code> and there is a <code>npm-shrinkwrap.json</code> present, it will override the listed dependencies and any semver ranges in <code>package.json</code>.</p>\n<p>If you need verified consistency across <code>package.json</code>, <code>npm-shrinkwrap.json</code> and <code>node_modules</code> for your project, you should consider using <a href=\"https://github.com/uber/npm-shrinkwrap\">npm-shrinkwrap</a>.</p>\n<p><img src=\"https://nodesource.com/media/2016/Aug/Screen_Shot_2016_08_26_at_3_54_36_PM-1472242098179.png\" alt=\"Running npm shrinkwrap on a Node project\"></p>\n<h2>6. Use npm v3 with Node.js v4 LTS</h2>\n<p><strong>Run:</strong> <code>npm install -g npm@3</code></p>\n<p>Installing <code>npm@3</code> globally with npm will update your npm v2 to npm v3, including on the Node.js v4 LTS release (“Argon”) ships with the npm v2 LTS release. This will install the latest stable release of npm v3 within your v4 LTS runtime.</p>\n<h2>7. Allow <code>npm install -g</code> without needing <code>sudo</code></h2>\n<p><strong>Run:</strong> <code>npm config set prefix $dir</code></p>\n<p>After running the command, where <code>$dir</code> is the directory you want npm to <em>install your global modules to</em>, you won’t need to use sudo to install modules globally anymore. The directory you use in the command becomes your global bin directory. </p>\n<p>The only caveat: you will need to make sure you <strong>adjust your user permissions</strong> for that directory with <code>chown -R $USER $dir</code> and you <strong>add</strong> <code>$dir/bin</code> to your PATH.</p>\n<h2>8. Change the default save prefix for all your projects</h2>\n<p><strong>Run:</strong> <code>npm config set save-prefix=\"~\"</code></p>\n<p>The tilde (<code>~</code>) is more conservative than what npm defaults to, the caret (<code>^</code>), when installing a new package with the <code>--save</code> or <code>--save-dev</code> flags. The tilde pins the dependency to the minor version, allowing patch releases to be installed with <code>npm update</code>. The caret pins the dependency to the major version, allowing minor releases to be installed with <code>npm update</code>.</p>\n<h2>9. Strip your project's <code>devDependencies</code> for a production environment</h2>\n<p>When your project is ready for production, make sure you install your packages with the added <code>--production</code> flag. The <code>--production</code> flag installs your <code>dependencies</code>, ignoring your <code>devDependencies</code>. This ensures that your development tooling and packages won’t go into the production environment.</p>\n<p>Additionally, you can set your <code>NODE_ENV</code> environment variable to <code>production</code> to ensure that your project’s <code>devDependencies</code> are never installed.</p>\n<h2>10. Be careful when using <code>.npmignore</code></h2>\n<p>If you haven't been using <code>.npmignore</code>, it defaults to <code>.gitignore</code> with a few additional sane defaults.</p>\n<p>What many don't realize that once you add a <code>.npmignore</code> file to your project the <code>.gitignore</code> rules are (ironically) ignored. The result is you will need to audit the two ignore files in sync to prevent sensitive leaks when publishing.</p>\n<h2>11. Automate <code>npm init</code> with defaults</h2>\n<p>When you run <code>npm init</code> in a new project, you’re able to go through and set up your <code>package.json</code>’s details. If you want to set defaults that <code>npm init</code> will always use, you can use the <code>config set</code> command, with some extra arguments:</p>\n<pre><code>npm config set init.author.name $name\nnpm config set init.author.email $email\n</code></pre>\n<p>If, instead, you want to completely customize your init script, you can point to a self-made default init script by running </p>\n<pre><code>npm config set init-module ~/.npm-init.js`\n</code></pre>\n<p>Here’s a sample script that prompts for private settings and creates a GitHub repo if you want. Make sure you change the default GitHub username (<code>YOUR_GITHUB_USERNAME</code>) as the fallback for the GitHub username environment variable.</p>\n<pre><code class=\"language-js\">var cp = require('child_process');\nvar priv;\n\nvar USER = process.env.GITHUB_USERNAME || 'YOUR_GITHUB_USERNAME';\n\nmodule.exports = {\n\n  name: prompt('name', basename || package.name),\n\n  version: '0.0.1',\n\n  private: prompt('private', 'true', function(val){\n    return priv = (typeof val === 'boolean') ? val : !!val.match('true')\n  }),\n\n  create: prompt('create github repo', 'yes', function(val){\n    val = val.indexOf('y') !== -1 ? true : false;\n\n    if(val){\n      console.log('enter github password:');\n      cp.execSync(\"curl -u '\"+USER+\"' https://api.github.com/user/repos -d \" +\n        \"'{\\\"name\\\": \\\"\"+basename+\"\\\", \\\"private\\\": \"+ ((priv) ? 'true' : 'false')  +\"}' \");\n      cp.execSync('git remote add origin '+ 'https://github.com/'+USER+'/' + basename + '.git');\n    }\n\n    return undefined;\n  }),\n\n  main: prompt('entry point', 'index.js'),\n\n  repository: {\n    type: 'git',\n    url: 'git://github.com/'+USER+'/' + basename + '.git' },\n\n  bugs: { url: 'https://github.com/'+USER'/' + basename + '/issues' },\n\n  homepage: \"https://github.com/\"+USER+\"/\" + basename,\n\n  keywords: prompt(function (s) { return s.split(/\\s+/) }),\n\n  license: 'MIT',\n\n  cleanup: function(cb){\n\n    cb(null, undefined)\n  }\n\n}\n</code></pre>\n<br />\n# One last thing...\n<p>If you want to learn more about npm, Node.js, JavaScript, Docker, Kubernetes, Electron, and tons more, you should follow <a href=\"https://twitter.com/nodesource\">@NodeSource</a> on Twitter. We're always around and would love to hear from you!</p>"}}}}}}