{"id":227,"date":"2023-02-23T17:37:08","date_gmt":"2023-02-24T00:37:08","guid":{"rendered":"https:\/\/brie.com\/brian\/blog\/?p=227"},"modified":"2023-02-23T17:42:41","modified_gmt":"2023-02-24T00:42:41","slug":"import-module-node-js","status":"publish","type":"post","link":"https:\/\/brie.com\/brian\/blog\/?p=227","title":{"rendered":"Import Module Node JS"},"content":{"rendered":"\n<p>I just started working with Node JS and the <strong>ESM Standard <\/strong>with the <em>import<\/em> statement. I got the repeated error <em>SyntaxError: Cannot use import statement outside a module<\/em>. I searched the internet and <a href=\"https:\/\/nodejs.org\/api\/packages.html#packagejson-and-file-extensions\" data-type=\"URL\" data-id=\"https:\/\/nodejs.org\/api\/packages.html#packagejson-and-file-extensions\">this is the closest I came with finding information regarding modules<\/a>.<\/p>\n\n\n\n<p>Put the following in <code>b.js<\/code>. It has the value we want to export<\/p>\n\n\n<pre class=\"wp-block-code\">&lt;code&gt;export const myValue = 42;&lt;\/code&gt;<\/pre>\n\n\n<p>Put this in <code>a.js<\/code>. It will import the value from <code>b.js<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\">&lt;code&gt;import { myValue } from &#039;.\/b.js&#039;;\nconsole.log(&#039;The value of b is:&#039;, myValue); \/\/ use imports&lt;\/code&gt;<\/pre>\n\n\n\n<p>To make all this work, we need to create a <code>package.json<\/code> file, so that it will allow the <code>import<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\">&lt;code&gt;{\n  &quot;name&quot;: &quot;my-package&quot;,\n  &quot;version&quot;: &quot;1.0.0&quot;,\n  &quot;type&quot;: &quot;module&quot;\n}&lt;\/code&gt;<\/pre>\n\n\n\n<p>Now run it.<\/p>\n\n\n\n<pre class=\"wp-block-code\">&lt;code&gt;$ node a.js&lt;\/code&gt;<\/pre>\n\n\n\n<p>Output should look like this<\/p>\n\n\n\n<pre class=\"wp-block-code\">&lt;code&gt;The value of b is: 42&lt;\/code&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I just started working with Node JS and the ESM Standard with the import statement. I got the repeated error SyntaxError: Cannot use import statement outside a module. I searched the internet and this is the closest I came with &hellip; <a href=\"https:\/\/brie.com\/brian\/blog\/?p=227\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-227","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts\/227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=227"}],"version-history":[{"count":2,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts\/227\/revisions"}],"predecessor-version":[{"id":229,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts\/227\/revisions\/229"}],"wp:attachment":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}