New node: scheme from Node 16 not handled

See original GitHub issue

Bug report

What is the current behavior?

The new node: prefix from Node v16 causes an UnhandledSchemeError:

ERROR in node:path
Module build failed: UnhandledSchemeError: Reading from "node:path" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /Users/k/p/webpack-babel-test/node_modules/webpack/lib/NormalModule.js:742:26
    at Hook.eval [as callAsync] (eval at create (/Users/k/p/webpack-babel-test/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/Users/k/p/webpack-babel-test/node_modules/tapable/lib/Hook.js:18:14)
    at Object.processResource (/Users/k/p/webpack-babel-test/node_modules/webpack/lib/NormalModule.js:739:9)
    at processResource (/Users/k/p/webpack-babel-test/node_modules/loader-runner/lib/LoaderRunner.js:220:11)
    at iteratePitchingLoaders (/Users/k/p/webpack-babel-test/node_modules/loader-runner/lib/LoaderRunner.js:171:10)
    at runLoaders (/Users/k/p/webpack-babel-test/node_modules/loader-runner/lib/LoaderRunner.js:397:2)
    at NormalModule.doBuild (/Users/k/p/webpack-babel-test/node_modules/webpack/lib/NormalModule.js:729:3)
    at NormalModule.build (/Users/k/p/webpack-babel-test/node_modules/webpack/lib/NormalModule.js:878:15)
    at /Users/k/p/webpack-babel-test/node_modules/webpack/lib/Compilation.js:1311:12
 @ ./src/index.tsx 1:0-29 2:12-16

webpack 5.36.0 compiled with 1 error in 977 ms

This will be backported to at least also Node v14

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

Minimal reproduction repo (run yarn start): https://github.com/karlhorky/webpack-node-16-schema

What is the expected behavior?

webpack should throw a “Module not found” error with the polyfills warning, just like when you import path from 'path':

ERROR in ./src/index.tsx 1:0-24
Module not found: Error: Can't resolve 'path' in '/Users/k/p/webpack-babel-test/src'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
        - install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "path": false }
resolve 'path' in '/Users/k/p/webpack-babel-test/src'
  Parsed request is a module
  using description file: /Users/k/p/webpack-babel-test/package.json (relative path: ./src)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      /Users/k/p/webpack-babel-test/src/node_modules doesn't exist or is not a directory
      looking for modules in /Users/k/p/webpack-babel-test/node_modules
        single file module
          using description file: /Users/k/p/webpack-babel-test/package.json (relative path: ./node_modules/path)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /Users/k/p/webpack-babel-test/node_modules/path doesn't exist
            *
              Field 'browser' doesn't contain a valid alias configuration
              /Users/k/p/webpack-babel-test/node_modules/path* doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/k/p/webpack-babel-test/node_modules/path.js doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              /Users/k/p/webpack-babel-test/node_modules/path.ts doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/k/p/webpack-babel-test/node_modules/path.tsx doesn't exist
        /Users/k/p/webpack-babel-test/node_modules/path doesn't exist
      /Users/k/p/node_modules doesn't exist or is not a directory
      /Users/k/node_modules doesn't exist or is not a directory
      /Users/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory

webpack 5.36.0 compiled with 1 error in 913 ms

Other relevant information: webpack version: 5.36.2 Node.js version: 16.0.0 Operating System: macOS Big Sur 11.3 Additional tools:

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:10
  • Comments:33 (14 by maintainers)

github_iconTop GitHub Comments

13reactions
EvHauscommented, Oct 11, 2021

Using webpack 5.58.1 I’m still getting this error:

ModuleBuildError: Module build failed: UnhandledSchemeError: Reading from "node:process" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /Users/evhaus/myproject/node_modules/webpack/lib/NormalModule.js:816:25

Does webpack need some kind of extra configuration to make node:* imports work?

8reactions
karlhorkycommented, Nov 23, 2022

Workaround (node: scheme in Next.js)

Fixed in my next.config.mjs with NormalModuleReplacementPlugin, similar to @alexander-akait’s comment above:

/** @type {import('next').NextConfig} */
const config = {
  reactStrictMode: true,
  webpack: (webpackConfig, { webpack }) => {
    webpackConfig.plugins.push(
      // Remove node: from import specifiers, because Next.js does not yet support node: scheme
      // https://github.com/vercel/next.js/issues/28774
      new webpack.NormalModuleReplacementPlugin(
        /^node:/,
        (resource) => {
          resource.request = resource.request.replace(/^node:/, '');
        },
      ),
    );

    return webpackConfig;
  },
};

export default config;
Read more comments on GitHub >

github_iconTop Results From Across the Web

UnhandledSchemeError: What is this node:buffer error and ...
node :buffer Module build failed: UnhandledSchemeError: Reading from "node:buffer" is not handled by plugins (Unhandled scheme). Webpack supports ...
Read more >
Node.js 15 release: Updated handling of rejections, npm 7, N ...
In the future, promise rejections that are not handled will terminate the Node. js process with a non-zero exit code.
Read more >
Node.js v19.3.0 Documentation
Indicates the failure of an assertion. All errors thrown by the node:assert module will be instances of the AssertionError class. new assert.AssertionError( ...
Read more >
How to resolve 'node' is not recognized as an internal or ...
Open the Environment Variables option in your Control Panel. · Select the variable named Path. · Restart the command prompt again and now...
Read more >
Node.js Built-in Modules - W3Schools
Node.js has a set of built-in modules which you can use without any further installation. ... To handle unhandled errors ... A character...
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