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.

Unable to resolve 'os' with ESM module dependency

See original GitHub issue

Bug report

This bug report relates to [this] StackOverflow question and a bug report filed on the Pisma repo: https://github.com/prisma/prisma/issues/10058

What is the current behavior?

Bundling a module made using the ESM module system with a ‘node’ protocol import does not correctly resolve ‘os’ when the package requires it.

Error: Cannot find module 'os'
    at webpackEmptyContext (/home/joseph/Projects/cs-portfolio/CIS3950/agineek-website/dist/agineek-website/server/main.js:511630:10)
    at ../../node_modules/.pnpm/supports-color@7.2.0/node_modules/supports-color/index.js (/home/joseph/Projects/cs-portfolio/CIS3950/agineek-website/dist/agineek-website/server/main.js:193372:15)
    at __require2 (/home/joseph/Projects/cs-portfolio/CIS3950/agineek-website/dist/agineek-website/server/main.js:191265:45)
    at ../../node_modules/.pnpm/chalk@4.1.2/node_modules/chalk/source/index.js (/home/joseph/Projects/cs-portfolio/CIS3950/agineek-website/dist/agineek-website/server/main.js:193715:9)
    at __require2 (/home/joseph/Projects/cs-portfolio/CIS3950/agineek-website/dist/agineek-website/server/main.js:191265:45)
    at Object.59062 (/home/joseph/Projects/cs-portfolio/CIS3950/agineek-website/dist/agineek-website/server/main.js:231640:33)
    at __webpack_require__ (/home/joseph/Projects/cs-portfolio/CIS3950/agineek-website/dist/agineek-website/server/main.js:534735:42)
    at Object.76345 (/home/joseph/Projects/cs-portfolio/CIS3950/agineek-website/dist/agineek-website/server/main.js:190859:5)
    at __webpack_require__ (/home/joseph/Projects/cs-portfolio/CIS3950/agineek-website/dist/agineek-website/server/main.js:534735:42)
    at Object.28999 (/home/joseph/Projects/cs-portfolio/CIS3950/agineek-website/dist/agineek-website/server/main.js:191103:16) {
  code: 'MODULE_NOT_FOUND'
}

The present error results from, in my case, the module prisma which depends on supports-color

Relevant portions of the outputted bundle:

var require_supports_color2 = __commonJS2({
  "../../node_modules/.pnpm/supports-color@7.2.0/node_modules/supports-color/index.js"(exports2, module2) {
    "use strict";

    var os2 = __require("os");

    var tty = __require("tty");

    var hasFlag = require_has_flag2();
    var {
      env
    } = process;
    var forceColor;
...

The issue is reported in the prisma repo as hitting both NestJS, NextJS, and NX workspace applications.

If the current behavior is a bug, please provide the steps to reproduce.

Minimal reproduction repos:

https://github.com/quinnjr/webpack-os-error-issue

Steps to observer:

  1. Install dependencies and setup Prisma correctly
  2. Generate Prisma client
  3. run npm run build:ssr
  4. Wait for webpack to finish bundling frontend and backend.
  5. Run npm run serve:ssr

https://github.com/tonivj5/prisma-issue-10058

What is the expected behavior?

The expected behavior is for a Webpack bundle set with a node target to be able to resolve the os module.

Other relevant information: webpack version: v5.62.1 Node.js version: v16.13.0 Operating System: ArchLinux Additional tools: @angular/custom-webpack @ ^12.1.3

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
alexander-akaitcommented, Nov 8, 2021

You need to create alias on real os module, {} just create empty modules, but I think you want to use os when you use target: 'node', try:

'node:os': 'commonjs2 os',
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't resolve node builtin modules in esm build dependency
Can't resolve node builtin modules in esm build dependency #15574 ... Operating System: macOS 12.3. Additional tools:.
Read more >
"Failed to resolve module specifier "vue"" - Stack Overflow
I get the following error in the browser console: Uncaught TypeError: Failed to resolve module specifier "vue". Relative references must start ...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
Provides a module-relative resolution function scoped to each module, returning the URL string. const dependencyAsset = await import.meta.resolve('component-lib ...
Read more >
@rollup/plugin-node-resolve - npm
A Rollup plugin which locates modules using the Node resolution algorithm, for using third party modules in node_modules ...
Read more >
Module not found: Error: Can't resolve 'os' [Solved] | bobbyhadz
To solve the error, set the browser.os property to false in your package.json file. shell.
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