Module not found: Error: Can't resolve 'net' in 'node_modules/stompjs/lib'

前端 未结 6 1289
夕颜
夕颜 2021-02-18 20:38

Got an issue where a stompJS-lib was not found, upon which I got the following error message:

Module not found: Error: Can\'t resolve \'net\' in \'/../../../.../         


        
相关标签:
6条回答
  • 2021-02-18 21:17

    solved it with the following command:

     npm i net -S
    
    0 讨论(0)
  • 2021-02-18 21:21

    To expand a bit on Arthur Costa answer, if you're using NextJS, you can add a configuration in your configuration file to prevent this issue for specific imports.

    Add those lines in a file named next.config.js in your root project folder:

    module.exports = {
        webpack: (config, { isServer }) => {
            if (!isServer) {
                config.node = {
                    net: 'empty'
                };
            }
    
            return config;
        }
    }
    

    In case the problem appears with other built-in modules, you can add those alongside net too.

    Source: https://github.com/vercel/next.js/issues/7755#issuecomment-508633125

    0 讨论(0)
  • 2021-02-18 21:22

    That's because Net is part of NodeJS, so it doesn't exist in the browser. https://nodejs.org/api/net.html#net_net

    Better update your Webpack configuration with

    node: {
      net: 'empty',
    },
    
    0 讨论(0)
  • 2021-02-18 21:28

    webpack v5 (add to webpack.config.js in module.exports):

    resolve: {
        fallback: {
            net: false
        }
    },
    
    0 讨论(0)
  • 2021-02-18 21:31

    To avoid installing random packages, you could add this into your Webpack configuration.

    node: {
      net: 'empty',
    },
    

    The issue is caused by expecting the net package which is a package from Node.JS and does not exist in the browser.

    If you want more information and explanations you can find it here.

    0 讨论(0)
  • 2021-02-18 21:31
    ERROR in ./node_modules/@google/maps/node_modules/https-proxy-agent/index.js
    Module not found: Error: Can't resolve 'net' in 'C:\.....\...\node_modules\@google\maps\node_modules\https-proxy-agent'
    ERROR in ./node_modules/@google/maps/node_modules/https-proxy-agent/index.js
    Module not found: Error: Can't resolve 'tls' in 'C:\...\....\node_modules\@google\maps\node_modules\https-proxy-agent'
    

    This is resolved by installing: npm i tls -S and then install npm i net -S

    0 讨论(0)
提交回复
热议问题