{"id":2139,"date":"2022-10-01T16:07:47","date_gmt":"2022-10-01T14:07:47","guid":{"rendered":"https:\/\/www.speich.net\/articles\/?p=2139"},"modified":"2022-12-30T13:36:31","modified_gmt":"2022-12-30T12:36:31","slug":"how-to-get-the-version-of-a-module-from-its-package-json","status":"publish","type":"post","link":"https:\/\/www.speich.net\/articles\/en\/2022\/10\/01\/how-to-get-the-version-of-a-module-from-its-package-json\/","title":{"rendered":"How to get the version of a module from its package.json"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">node.js &lt; 12<\/h2>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>VERSION=$(node -p \"require('some-module\/package.json').version\")<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">node.js 12+<\/h2>\n\n\n\n<p>In Node.js 12+ you can no longer get the version of a module dependency with the version property unless the path is explicitly exported in the module with:<\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code>{\n  \"exports\": {\n    \".\/package.json\": \".\/package.json\"\n  }\n}<\/code><\/pre>\n\n\n\n<p>What you can do instead, is to use a regular expression to parse the version from you own package.json dependency property with:<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>VERSION=$(node -p \"\/(\\d+\\.\\d+(?:\\.\\d+)?)\/.exec(require('.\/package.json').dependencies.some-module)[1]\") <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">package.json example<\/h2>\n\n\n\n<p>The following npm script example copies the <code>some-module<\/code> from the <code>.\/node_modules<\/code> folder to <code>.\/..\/some-module\/1.7.3<\/code> outside the node_modules folder (note: <a rel=\"noreferrer noopener\" href=\"https:\/\/gist.github.com\/jhorsman\/62eeea161a13b80e39f5249281e17c39\" target=\"_blank\">adapt the regular expression<\/a>, if the version of the some-module dependency contains other characters than numbers and the full stop):<\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code>{\n  \"dependencies\": {\n    \"some-module\": \"1.7.3\"\n  },\n  \"devDependencies\": {\n    \"copyfiles\": \"2.4.1\"\n  },\n  \"scripts\": {\n    \"some-module:copy\": \"VERSION=$(node -p \\\"\/(\\\\d+\\\\.\\\\d+(?:\\\\.\\\\d+)?)\/.exec(require('.\/package.json').dependencies.some-module)[1]\\\") &amp;&amp; copyfiles -u 3 \\\"node_modules\/some-module\/**\/*\\\" some-module\/$VERSION\"\n  }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>node.js &lt; 12 VERSION=$(node -p &#8220;require(&#8216;some-module\/package.json&#8217;).version&#8221;) node.js 12+ In Node.js 12+ you can no longer get the version of a module dependency with the version property unless the path is explicitly exported in the module with: { &#8220;exports&#8221;: { &#8220;.\/package.json&#8221;: &#8220;.\/package.json&#8221; } } What you can do instead, is to use a regular expression to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.speich.net\/articles\/en\/2022\/10\/01\/how-to-get-the-version-of-a-module-from-its-package-json\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to get the version of a module from its package.json&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[228,307],"class_list":["post-2139","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-node-js","tag-npm","entry"],"_links":{"self":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/2139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/comments?post=2139"}],"version-history":[{"count":17,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/2139\/revisions"}],"predecessor-version":[{"id":2275,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/2139\/revisions\/2275"}],"wp:attachment":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/media?parent=2139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/categories?post=2139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/tags?post=2139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}