<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jim Cummins</title>
    <description>The latest articles on DEV Community by Jim Cummins (@jimthedev).</description>
    <link>https://dev.to/jimthedev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2892%2F7163278acf8c1876e831117929233b89.jpeg</url>
      <title>DEV Community: Jim Cummins</title>
      <link>https://dev.to/jimthedev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jimthedev"/>
    <language>en</language>
    <item>
      <title>TypeError: Cannot read property 'filename' of undefined in ES Modules in Node 14 and greater</title>
      <dc:creator>Jim Cummins</dc:creator>
      <pubDate>Fri, 01 May 2020 02:31:39 +0000</pubDate>
      <link>https://dev.to/jimthedev/typeerror-cannot-read-property-filename-of-undefined-in-es-modules-in-node-14-and-greater-6ia</link>
      <guid>https://dev.to/jimthedev/typeerror-cannot-read-property-filename-of-undefined-in-es-modules-in-node-14-and-greater-6ia</guid>
      <description>&lt;p&gt;&lt;strong&gt;The problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When importing a Node.js module that uses some code similar to the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You might get an error similar to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;TypeError: Cannot &lt;span class="nb"&gt;read &lt;/span&gt;property &lt;span class="s1"&gt;'filename'&lt;/span&gt; of undefined. 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The error can seem cryptic and might make you think there is a problem with the module you are importing. As it turns out, the problem is that you are using ES Modules in Node 14+ and using &lt;code&gt;import&lt;/code&gt; to load a CommonJS module. While using this &lt;code&gt;import&lt;/code&gt; syntax may sometimes work with CommonJS, some will not.&lt;/p&gt;

&lt;p&gt;Luckily with a quick change you can successfully import the CommonJS module while still using &lt;code&gt;import&lt;/code&gt; and ES Modules for the rest of your app/library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it happens&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/MylesBorins/status/1255950295806488576"&gt;module.parent simply doesn't exist&lt;/a&gt; in Node ES Modules. It was removed because ES Modules are not a tree, but rather a graph. Thus the word 'parent' doesn't actually make sense when thinking about the module structures. Instead of thinking about hierarchy we're just at a point in a graph. Anyhow you don't need to worry about this distinction if you just fix this error. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A solution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of using the following code to import the module called &lt;a href="https://npm.im/meow"&gt;meow&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;meow&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;meow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can use the following code to import the meow module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createRequire&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;module&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;meow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createRequire&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;meow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is simply a way to tell Node to use the older require / CommonJS module loading while inside an ES Module. When you do this, module.parent.filename will be available again for the subtree of modules that get loaded.  &lt;/p&gt;

&lt;p&gt;Special thanks to &lt;a href="https://twitter.com/MylesBorins"&gt;Myles Borins&lt;/a&gt; for his input on solving this.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>esm</category>
    </item>
  </channel>
</rss>
