question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Server doesn't work with `esm` package

See original GitHub issue

🐛 Bug Report

When using @loadable/server with the esm package, when you instantiate the ChunkExtractor you will get the following error:

TypeError: Cannot read property 'filename' of undefined
    at /Users/brandonp/Sites/myrepo/node_modules/esm/esm.js:1:252056
    at smartRequire (/Users/brandonp/Sites/myrepo/node_modules/@loadable/server/lib/util.js:23:32)
    at new ChunkExtractor (/Users/brandonp/Sites/myrepo/node_modules/@loadable/server/lib/ChunkExtractor.js:173:50)
    at renderApp (/Users/brandonp/Sites/myrepo/src/backend/renderApp.js:63:29)
    at process._tickCallback (internal/process/next_tick.js:68:7)

To Reproduce

Steps to reproduce the behavior:

Run the following with a valid nodeStats path:

      const nodeExtractor = new ChunkExtractor({
        statsFile: nodeStats,
        entrypoints: ['main']
      });

Expected behavior

ChunkExtractor successfully loads and uses the required nodeStats file path.

 const nodeStats = path.resolve('./dist/backend/app/loadable-stats.json');`

The fix (IMO)

You’re currently using module.require. If I tweak this line to read as follows:

  try {
    // This will fail with 'esm' because module not accessible
    return eval('module.require')(modulePath);
  } catch (err) {
    // Fallback on normal require
    return require(modulePath);
  }

More about why module.require is not defined here.

I am more than happy to open a PR for this, but unsure if this is the solution you want to take. Please let me know!

Run npx envinfo --system --binaries --npmPackages @loadable/component,@loadable/server,@loadable/webpack-plugin,@loadable/babel-plugin --markdown --clipboard

Paste the results here:

## System:
 - OS: macOS Mojave 10.14.6
 - CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
 - Memory: 966.62 MB / 16.00 GB
 - Shell: 5.3 - /bin/zsh
## Binaries:
 - Node: 10.15.1 - ~/.nvm/versions/node/v10.15.1/bin/node
 - Yarn: 1.17.3 - /usr/local/bin/yarn
 - npm: 6.4.1 - ~/.nvm/versions/node/v10.15.1/bin/npm
## npmPackages:
 - @loadable/babel-plugin: ^5.10.3 => 5.10.3 
 - @loadable/component: ^5.10.3 => 5.10.3 
 - @loadable/server: ^5.10.3 => 5.10.3 
 - @loadable/webpack-plugin: ^5.7.1 => 5.7.1 

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
brandon-pereiracommented, Oct 29, 2019

@theKashey PR opened which changes eval('module.require') to eval('require'). @neoziro if you have any insights as to why module.require was used I’d be interested to hear!

Let me know if I can change/improve anything else, happy to help. Thanks for making this package 😃

0reactions
brandon-pereiracommented, Oct 29, 2019

Okay, I can switch it to using a simple require. Should I add foolwebpack as a dependency or should I just copy the logic they use to require? Not sure if you want another dependency added.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What does it take to support Node.js ESM? – The Guild
I have worked on all The Guild's libraries and graphql-js to support ESM. Here is how you can do it too.
Read more >
node.js - I'm using ESM, but it doesn't recognize - Stack Overflow
I'm running a simple backend and I'm trying to host it on a linux VPS. But I get the error of ESM module...
Read more >
Getting Started with (and Surviving) Node.js ESM
The Node.js ecosystem is shifting towards ESM. We examine what lies in store for application and library authors, learn some of challenges ...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
Introduction#. ECMAScript modules are the official standard format to package JavaScript code for reuse. Modules are defined using a variety of import and ......
Read more >
ES Modules in Node.js - Maxim Orlov
If you add "type": "module" in your package.json you're signalling that all ... Additionally, in contrary with CJS, importing folders doesn't work in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found