Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New node: scheme from Node 16 not handled #13290

Closed
karlhorky opened this issue May 2, 2021 · 39 comments
Closed

New node: scheme from Node 16 not handled #13290

karlhorky opened this issue May 2, 2021 · 39 comments

Comments

@karlhorky
Copy link
Contributor

karlhorky commented May 2, 2021

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:

@alexander-akait
Copy link
Member

Yep, we should improve this, you can help, here place https://github.com/webpack/webpack/blob/master/lib/node/NodeTargetPlugin.js

@karlhorky
Copy link
Contributor Author

Sure, opened a quick PR here: #13311

Asked for some feedback from you over there.

@karlhorky
Copy link
Contributor Author

karlhorky commented May 6, 2021

Ok, @sokra mentioned there's another PR open at #12693

@karlhorky
Copy link
Contributor Author

So this feature is about to get more usage: the node: scheme / prefix / protocol was backported to Node.js 14.13.1 / 12.20.0 🎉

https://nodejs.org/api/esm.html -> link "node: Imports" (links with anchors don't scroll properly right now)

Screen Shot 2021-05-30 at 11 04 39

@karlhorky
Copy link
Contributor Author

An alternative to the approaches in #13311 and #12693 would be to use is-core-module (which was split from resolve in browserify/resolve@7c26483), so that this list doesn't need to be maintained and tested by webpack itself.

@dervaux
Copy link

dervaux commented Jun 16, 2021

I can confirm this issue is still relevant. I'm getting the following error:

Failed to compile.
node:fs/promises
Module build failed: UnhandledSchemeError: Reading from "node:fs/promises" 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.
> Build error occurred
Error: > Build failed because of webpack errors

@alexander-akait
Copy link
Member

Fixed by #12693

@karlhorky
Copy link
Contributor Author

Nice, really cool! Looking forward to using this! (also with our students in Next.js!)

Will this be in a new patch (5.39.2) or minor (5.40.0)?

@alexander-akait
Copy link
Member

@karlhorky I think we will have minor release in near future

@alexander-akait
Copy link
Member

We want to finish some critical issues and support import something from './test.json' assert { type: "json" } https://github.com/tc39/proposal-import-assertions

@karlhorky
Copy link
Contributor Author

Oh nice!! I've seen that syntax floating around, wanting to try it for a while 🙌

@karlhorky
Copy link
Contributor Author

I've requested that the compiled version of webpack be upgraded in Next.js too: vercel/next.js#26914

@EvHaus
Copy link

EvHaus commented 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?

@xptodevx
Copy link

xptodevx commented Dec 3, 2021

why you close issues if not solved?

@alexander-akait
Copy link
Member

You need to use externals, with absolute URLs, i.e. node:/http:/etc aliases do not work, you need to say it is external and provide path on polyfill if you need or set false if you don't need these modules

@alexander-akait
Copy link
Member

If you provide example with the problem I will show you how to setup it

@cindyloo
Copy link

cindyloo commented Dec 6, 2021

here is my same failure. @alexander-akait can you explain what should be done with externals and absolute URLs? Where/how?
I set my webpack config params in next.config.js... The polyfill doesn't appear to be working..
eg

config.resolve.fallback = {
              assert: require.resolve('assert'),
              buffer: require.resolve('buffer'),
               fs: require.resolve('fs'),
node:fs
Module build failed: UnhandledSchemeError: Reading from "node:fs" 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/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54718:25
    at Hook.eval [as callAsync] (eval at create (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:141201:10), <anonymous>:6:1)
    at Object.processResource (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54715:8)
    at processResource (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140174:11)
    at iteratePitchingLoaders (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140125:10)
    at runLoaders (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140351:2)
    at NormalModule._doBuild (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54705:3)
    at NormalModule.build (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54849:15)
    at /Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:29353:12
    at NormalModule.needBuild (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:55136:32)

Import trace for requested module:
./node_modules/fetch-blob/from.js
./node_modules/node-fetch/src/utils/multipart-parser.js
./node_modules/node-fetch/src/body.js
./node_modules/node-fetch/src/index.js
./public/js/face-api.js
./public/js/setUpWorker.js
./components/pages/SessionView3/TwilioDynamic.tsx
./components/pages/SessionView3/SessionConnectionWrapper.tsx
./components/pages/SessionView3/index.tsx
./pages/sessions3/[id]/index.tsx

node:https
Module build failed: UnhandledSchemeError: Reading from "node:https" 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/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54718:25
    at Hook.eval [as callAsync] (eval at create (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:141201:10), <anonymous>:6:1)
    at Object.processResource (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54715:8)
    at processResource (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140174:11)
    at iteratePitchingLoaders (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140125:10)
    at runLoaders (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140351:2)
    at NormalModule._doBuild (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54705:3)
    at NormalModule.build (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54849:15)
    at /Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:29353:12
    at NormalModule.needBuild (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:55136:32)

Import trace for requested module:
./node_modules/node-fetch/src/index.js
./public/js/face-api.js
./public/js/setUpWorker.js
./components/pages/SessionView3/TwilioDynamic.tsx
./components/pages/SessionView3/SessionConnectionWrapper.tsx
./components/pages/SessionView3/index.tsx
./pages/sessions3/[id]/index.tsx

node:http
Module build failed: UnhandledSchemeError: Reading from "node:http" 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/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54718:25
    at Hook.eval [as callAsync] (eval at create (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:141201:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:141003:14)
    at Object.processResource (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54715:8)
    at processResource (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140174:11)
    at iteratePitchingLoaders (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140125:10)
    at runLoaders (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140351:2)
    at NormalModule._doBuild (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54705:3)
    at NormalModule.build (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54849:15)
    at /Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:29353:12

Import trace for requested module:
./node_modules/node-fetch/src/index.js
./public/js/face-api.js
./public/js/setUpWorker.js
./components/pages/SessionView3/TwilioDynamic.tsx
./components/pages/SessionView3/SessionConnectionWrapper.tsx
./components/pages/SessionView3/index.tsx
./pages/sessions3/[id]/index.tsx

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/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54718:25
    at Hook.eval [as callAsync] (eval at create (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:141201:10), <anonymous>:6:1)
    at Object.processResource (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54715:8)
    at processResource (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140174:11)
    at iteratePitchingLoaders (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140125:10)
    at runLoaders (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140351:2)
    at NormalModule._doBuild (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54705:3)
    at NormalModule.build (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54849:15)
    at /Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:29353:12
    at NormalModule.needBuild (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:55136:32)

Import trace for requested module:
./node_modules/fetch-blob/from.js
./node_modules/node-fetch/src/utils/multipart-parser.js
./node_modules/node-fetch/src/body.js
./node_modules/node-fetch/src/index.js
./public/js/face-api.js
./public/js/setUpWorker.js
./components/pages/SessionView3/TwilioDynamic.tsx
./components/pages/SessionView3/SessionConnectionWrapper.tsx
./components/pages/SessionView3/index.tsx
./pages/sessions3/[id]/index.tsx

node:process
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/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54718:25
    at Hook.eval [as callAsync] (eval at create (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:141201:10), <anonymous>:6:1)
    at Object.processResource (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54715:8)
    at processResource (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140174:11)
    at iteratePitchingLoaders (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140125:10)
    at runLoaders (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:140351:2)
    at NormalModule._doBuild (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54705:3)
    at NormalModule.build (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:54849:15)
    at /Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:29353:12
    at NormalModule.needBuild (/Users/cb/Documents/GitHub/bongo-focus/node_modules/next/dist/compiled/webpack/bundle5.js:55136:32)

Import trace for requested module:
./node_modules/fetch-blob/streams.cjs
./node_modules/fetch-blob/index.js
./node_modules/node-fetch/src/body.js
./node_modules/node-fetch/src/index.js
./public/js/face-api.js
./public/js/setUpWorker.js
./components/pages/SessionView3/TwilioDynamic.tsx
./components/pages/SessionView3/SessionConnectionWrapper.tsx
./components/pages/SessionView3/index.tsx
./pages/sessions3/[id]/index.tsx


> Build failed because of webpack errors

@alexander-akait
Copy link
Member

alexander-akait commented Dec 6, 2021

can you explain what should be done with externals and absolute URLs?

Because they are URLs, so you need to use externals to use other modules for them

If you provide example I will show how to fix it

@cindyloo
Copy link

cindyloo commented Dec 6, 2021

I have this- it appears to compile?
this is inside my next.config.js file

webpack: (config, { buildId, isServer, webpack }) => {
        config.plugins.push(new Dotenv({ silent: true }));
        config.plugins.push(
          new webpack.DefinePlugin({
            "process.env.SENTRY_RELEASE": JSON.stringify(buildId),
          })
        );
        config.resolve.fallback = {    
              http: require.resolve('stream-http'),
              https: require.resolve('https'),
              path: require.resolve("path-browserify")
              //fs: require.resolve("node-fetch")
          };
    



        //config.resolve.alias["@"] = path.resolve(__dirname, 'src');
        config.resolve.alias["https"] = "https";
        config.resolve.alias["process"] = "process";
        config.resolve.alias["path"] = "@node/path";
        //config.resolve.alias["fs"] = "@node/fs";
        //config.resolve.alias["@node/fs"] = "@node/fs";
        config.resolve.alias["http"] = "stream-http";
        config.resolve.alias["fs"] = "node";
        config.resolve.alias["fs"] = "node-fetch";

@alexander-akait
Copy link
Member

Please use config.externals["node:fs"]

@niksy
Copy link

niksy commented Dec 6, 2021

@alexander-akait this still doesn’t work in combination with resolve.alias and resolve.fallback (see #14166)

@alexander-akait
Copy link
Member

You should not use resolve.alias for URLs, if you provide small example how you are using it I will show you how to fix it, node:something URLs are just external URLs (because node: is external protocol for web target)

@cindyloo
Copy link

cindyloo commented Dec 6, 2021

@alexander-akait could you please add more info - I am using next.config.js, so the docs don't really tell me the correct syntax
you mean like this:

config.externals["node:fs"],

or do you mean like this
config.externals["node:fs"]= "node",

@alexander-akait
Copy link
Member

alexander-akait commented Dec 7, 2021

If you need an empty module, you can use (works like config.resolve["path"] = false):

externals: {
   // You can use `false` or other values if you need something strange here,example will output `module.exports = {};`
  "node:path": "{}",
},

If you need polyfill please you:

plugins: [
  new webpack.NormalModuleReplacementPlugin(/node:/, (resource) => {
    const mod = resource.request.replace(/^node:/, "");

    switch (mod) {
      case "path":
        resource.request = "path-browserify";
        break;
      default:
        throw new Error(`Not found ${mod}`);
      }
  }),
]

@alexander-akait
Copy link
Member

For aliases we have an issue, but NormalModuleReplacementPlugin should works without any problem with any imports

niksy added a commit to niksy/node-stdlib-browser that referenced this issue Dec 8, 2021
niksy added a commit to niksy/node-stdlib-browser that referenced this issue Dec 8, 2021
@karlhorky
Copy link
Contributor Author

It seems like the compiled webpack version in Next.js 12.2.2 still doesn't support this, still getting the following error:

error - 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.

cc @sokra

@karlhorky
Copy link
Contributor Author

karlhorky commented Jul 19, 2022

Edit: No longer needed as of Next.js v13.1.5 🎉

Next.js v13.1.5 added support for the node: scheme here: vercel/next.js#45149

Workaround (node: scheme in Next.js < v13.1.5)

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;

@vankop
Copy link
Member

vankop commented Jul 21, 2022

@karlhorky please use externals for this. e.g. your webpack config

module.exports = {
  externals: { "node:path": "commonjs path" }
};

@karlhorky
Copy link
Contributor Author

karlhorky commented Jul 21, 2022

I tried the externals config, didn't work with the version of webpack in Next.js... 🤔

@colinrw
Copy link

colinrw commented Oct 10, 2022

@alexander-akait i am getting the error:
UnhandledSchemeError: Reading from "node:event" 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.

when I try to work with the discord.js package. But i keep getting the error mentioned above from this code:

import { WebhookClient } from 'discord.js'

const myWebhook = 'https://discord.com/api/webhooks/9028989097053016106/RIu4KtG0upgB6D0ppzB5wikSOxfk8sYZeWnhMTvBHqYBm1L6W4ab7d97auMq8nsAniEc'

export const discordWebhook = async () => {
    const webhookClient = new WebhookClient({ url: myWebhook })
}

These are my webpack configs:
main webpack

module.exports = {
  resolve: {
    extensions: ['.ts', '.js']
  },
  entry: './electron/main.ts',
  module: {
    rules: require('./rules.webpack'),
  },
  output: {
    environment: {
      // Disable arrow function to prevent bytenode from giving errors
      arrowFunction: false,
    }
  }
};

webpack rules

module.exports = [
  {
    test: /\.node$/,
    use: 'node-loader',
  },
  {
    test: /\.(m?js|node)$/,
    parser: { amd: false },
    use: {
      loader: '@marshallofsound/webpack-asset-relocator-loader',
      options: {
        outputAssetBase: 'native_modules',
      },
    },
  },
  {
    test: /\.(js|ts|tsx)$/,
    exclude: /node_modules/,
    use: {
      loader: 'babel-loader',
    },
  },
  {
    test: /\.(png|jpe?g|gif)$/i,
    loader: 'file-loader',
    options: {
      name: '[path][name].[ext]',
    },
  }
];

Hope you (or anyone else) can help me out, since I am stuck for a while now 😅

@alexander-akait
Copy link
Member

Please use externals, to provide polyfill for node: and other protocols

@colinrw
Copy link

colinrw commented Oct 11, 2022

@alexander-akait do you have an example on him to add node:events to externals in my webpack config?

@alexander-akait
Copy link
Member

externals: { 'node:events': pathToFileWhichContaintPolyfill }, it can be empty file with export {} if you need just mock

@timneutkens
Copy link
Contributor

To circle back here, I've opened a PR to Next.js to support node:. vercel/next.js#45149

@karlhorky
Copy link
Contributor Author

The PR above vercel/next.js#45149 by @timneutkens added support for the node: prefix in Next.js v13.1.5 🙌

Also updated my workaround post above: #13290 (comment)

@HL23252
Copy link

HL23252 commented Feb 12, 2023

@alexander-akait do you have an example on him to add node:events to externals in my webpack config?

Hey @colinrw I'd also just came across the same problem as you did, was wondering if you managed to solve the problem with adding externals to webpack.config? I tried doing it and managed to not get any error but when trying to run the Client, I get something that say "unexpected token : ..."

@colinrw
Copy link

colinrw commented Feb 13, 2023

@alexander-akait do you have an example on him to add node:events to externals in my webpack config?

Hey @colinrw I'd also just came across the same problem as you did, was wondering if you managed to solve the problem with adding externals to webpack.config? I tried doing it and managed to not get any error but when trying to run the Client, I get something that say "unexpected token : ..."

Hey! I don't know why, but my project was not able to run lots of different internal API calls from different packages. I mainly had to use regular REST calls to make these calls. Therefor I was not able to solve this webpack issue.

@HL23252
Copy link

HL23252 commented Feb 13, 2023

@alexander-akait do you have an example on him to add node:events to externals in my webpack config?

Hey @colinrw I'd also just came across the same problem as you did, was wondering if you managed to solve the problem with adding externals to webpack.config? I tried doing it and managed to not get any error but when trying to run the Client, I get something that say "unexpected token : ..."

Hey! I don't know why, but my project was not able to run lots of different internal API calls from different packages. I mainly had to use regular REST calls to make these calls. Therefor I was not able to solve this webpack issue.

Yeah having the same problem as well.. will look into how to use REST then, thank you!

@DutchPrince
Copy link

DutchPrince commented Oct 30, 2023

Hi, i've implemented the fix for Module build failed: UnhandledSchemeError: Reading from "node:fs/promises" is not handled by plugins (Unhandled scheme).

webpackConfig.plugins.push( new webpack.NormalModuleReplacementPlugin(/^node:/, (resource) => { resource.request = resource.request.replace(/^node:/, '') }) )

But now i am getting
⨯ node_modules/langchain/dist/vectorstores/hnswlib.js (209:55) @ join
⨯ path.join is not a function
null

does anyone have a clue why this is is happening?

below is what is causing the issue inside the node_modules/langchain/dist/vectorstores/hnswlib.js
static async load(directory, embeddings) { const fs = await import("node:fs/promises"); const path = await import("node:path"); const args = JSON.parse(await fs.readFile(path.join(directory, "args.json"), "utf8")); const index = await HNSWLib.getHierarchicalNSW(args); const [docstoreFiles] = await Promise.all([ fs .readFile(path.join(directory, "docstore.json"), "utf8") .then(JSON.parse), index.readIndex(path.join(directory, "hnswlib.index")), ]); args.docstore = new SynchronousInMemoryDocstore(new Map(docstoreFiles)); args.index = index; return new HNSWLib(embeddings, args); }

I am using:
Nodejs: v20.9.0
Nextjs: v14.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.