<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>npm on Much Ado About IT</title><link>https://it.knightnet.org.uk/tags/npm/</link><description>
Recent content about npm from Much Ado About IT |
Ramblings and rantings from IT Architect &amp; Designer, Julian Knight</description><generator>Hugo | gohugo.io | Theme twenty-sixteen</generator><language>en-gb</language><copyright>This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.</copyright><lastBuildDate>Mon, 24 Apr 2023 21:27:28 +0000</lastBuildDate><atom:link href="https://it.knightnet.org.uk/tags/npm/feed.xml" rel="self" type="application/rss+xml"/><item><title>Cleaning up npm package install problems</title><link>https://it.knightnet.org.uk/kb/nr-qa/clean-up-npm/</link><guid isPermaLink="true">https://it.knightnet.org.uk/kb/nr-qa/clean-up-npm/</guid><pubDate>Wed, 05 Jun 2019 07:05:39 +0000</pubDate><guid>https://it.knightnet.org.uk/kb/nr-qa/clean-up-npm/</guid><description><div>Node-RED uses npm to manage package installations such as its nodes.
Sometimes, you can get into a situation where you start getting lots of npm installation or removal issues and it feels like you are going round in circles trying to fix things. If so, time to call it a day and to get drastic! Here, I try to explain an easy way to fix things quickly and completely without side effects.</div><div>&lt;p>Node-RED is built over Node.js which uses &lt;code>npm&lt;/code> as its library (AKA package) manager. It is great to have a standard mechanism to manage libraries but the complexities of managing JavaScript based packages sometimes trips things up.&lt;/p>
&lt;p>Once you&amp;rsquo;ve tried for a bit to understand what the issues are, it may be time to call time and get drastic.&lt;/p>
&lt;p>The following commmands will clean out the packages installed for your live instance of Node-RED - assuming you have it installed via the &amp;ldquo;standard&amp;rdquo; default method. If not, you will need to adjust the folder you operate in.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">cd&lt;/span> ~/.node-red
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">rm package-lock.json
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">rm -R node_modules
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">npm install
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>That will cleanly reinstall all of the packages marked in your &lt;code>package.json&lt;/code> file. So make sure that is correct first. Every time you install a package (as long as you have reasonably up-to-date versions of Node.js and npm) whether from the NR admin ui or command line, it gets added to package.json.&lt;/p>
&lt;p>I now always remove the lock file as well if I&amp;rsquo;m having issues as that can be the cause of some issues. The lock file isn&amp;rsquo;t generally needed in NR installations.&lt;/p>
&lt;p>Note, as always, I&amp;rsquo;ve assumed you have a &amp;ldquo;standard&amp;rdquo; install of Node-RED which puts your userDir as &lt;code>~/.node-red&lt;/code> (and the equivalent on Windows). If you use an installation scheme like mine, it will be in a different place as noted by the command used to start NR.&lt;/p></div></description><author>Julian Knight</author><category domain="https://it.knightnet.org.uk/categories/development">Development</category><category domain="https://it.knightnet.org.uk/tags/node-red">Node-RED</category><category domain="https://it.knightnet.org.uk/tags/npm">npm</category><category domain="https://it.knightnet.org.uk/tags/node.js">Node.JS</category><category domain="https://it.knightnet.org.uk/tags/javascript">JavaScript</category><category domain="https://it.knightnet.org.uk/tags/debugging">Debugging</category></item><item><title>Using npm configuration environment variables in package.json</title><link>https://it.knightnet.org.uk/kb/node-js/npm-configuration-variables/</link><guid isPermaLink="true">https://it.knightnet.org.uk/kb/node-js/npm-configuration-variables/</guid><pubDate>Sun, 30 Dec 2018 22:03:07 +0000</pubDate><guid>https://it.knightnet.org.uk/kb/node-js/npm-configuration-variables/</guid><description><div>npm automatically makes parts of your package.json file available as environment variables. This can be useful when using npm as a run script service.</div><div>&lt;p>npm exposes all of the entries in &lt;code>package.json&lt;/code> as environment variables so that any script specified in the &lt;code>scripts&lt;/code>
section can make use of them.&lt;/p>
&lt;p>In the example below we can see both the advantage and the weakness of the approach.&lt;/p>
&lt;p>The advantage is that we can pass things like the module version number direct to another command.&lt;/p>
&lt;p>The weakness is that the consumption of environment variables is different depending on the operating
system in use.&lt;/p>
&lt;p>Every property in &lt;code>package.json&lt;/code> is exposed this way, simply prefix with &lt;code>npm_package_&lt;/code>
and replace subscripts (e.g. &lt;code>scripts.patch&lt;/code>) with an underscore (e.g. &lt;code>npm_package_scripts_patch&lt;/code>).&lt;/p>
&lt;h2 id="example-packagejson">Example package.json&lt;/h2>
&lt;p>We can use this for reference&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-json" data-lang="json">&lt;span class="line">&lt;span class="cl">&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;name&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;Config Example&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;version&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;1.2.3&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;description&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;Using config variables in package.json&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;author&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;Max Headroom&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;scripts&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;patch&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;npm version patch -m \&amp;#34;Patch version bump to %s\&amp;#34; &amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;minor&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;npm version minor -m \&amp;#34;Minor version bump to %s\&amp;#34; &amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;major&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;npm version major -m \&amp;#34;Major version bump to %s\&amp;#34;&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;postinstall&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;node ./postinstall.js&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;win-tag&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;git tag -a v%npm_package_version% -m \&amp;#34;Release v%npm_package_version%\&amp;#34;; git push origin --tags&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;tag&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;git tag -a v$npm_package_version -m \&amp;#34;Release v$npm_package_version\&amp;#34; &amp;amp;&amp;amp; git push origin --tags&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;devDependencies&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;eslint&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;^1.10.3&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="references">References&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://frontend.irish/npm-config-variables">Using config variables in package.json&lt;/a>&lt;/li>
&lt;/ul></div></description><author>Julian Knight</author><category domain="https://it.knightnet.org.uk/categories/development">Development</category><category domain="https://it.knightnet.org.uk/tags/node.js">node.js</category><category domain="https://it.knightnet.org.uk/tags/nodejs">nodejs</category><category domain="https://it.knightnet.org.uk/tags/npm">npm</category><category domain="https://it.knightnet.org.uk/tags/json">json</category></item><item><title>Creating GitHub Releases from git Tags</title><link>https://it.knightnet.org.uk/blog/creating-github-releases-from-git-tags/</link><guid isPermaLink="true">https://it.knightnet.org.uk/blog/creating-github-releases-from-git-tags/</guid><pubDate>Sun, 30 Dec 2018 21:23:20 +0000</pubDate><guid>https://it.knightnet.org.uk/blog/creating-github-releases-from-git-tags/</guid><description><div>The workflow for updating version numbers and doing git/GitHub/npm releases is far too complex to easily remember when you aren't doing it very often. This post is a reminder of the various steps.</div><div>&lt;p>Just a quick reminder for myself mainly.&lt;/p>
&lt;p>GitHub will automatically create a release from a git tag. Having had a request for this from an issue with node-red-contrib-uibuilder, I
decided that I&amp;rsquo;d better look into how to do it.&lt;/p>
&lt;p>Why is everything harder than it should be!? Anyway, here is the raw workflow for committing changes to a git managed repository of code,
creating a tag, pushing both master and the tag to GitHub and then publishing to npm.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># This could be minor or major instead of patch, it updates package.js&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">npm npm version patch -m &lt;span class="s2">&amp;#34;Patch version bump to %s&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># We might want to stage instead of committing everything at once&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">git commit -m &lt;span class="s2">&amp;#34;Commit message&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">git push
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Create a tag to match the npm version&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">git tag -a v1.4.1 -m &lt;span class="s2">&amp;#34;my version 1.4.1&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">git push origin --tags
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Publish to npm&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">npm publish
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Of course, the commit and push are easier done direct from VScode.&lt;/p>
&lt;p>It is annoying that I can&amp;rsquo;t link the tag to the npm version.&lt;/p>
&lt;h2 id="references">References&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://git-scm.com/book/en/v2/Git-Basics-Tagging">git docs 2.6 Git Basics - Tagging&lt;/a>&lt;/li>
&lt;/ul></div></description><author>Julian Knight</author><category domain="https://it.knightnet.org.uk/categories/development">Development</category><category domain="https://it.knightnet.org.uk/tags/git">git</category><category domain="https://it.knightnet.org.uk/tags/github">github</category><category domain="https://it.knightnet.org.uk/tags/npm">npm</category><category domain="https://it.knightnet.org.uk/tags/node.js">node.js</category><category domain="https://it.knightnet.org.uk/tags/nodejs">nodejs</category></item></channel></rss>