javascriptnode.jswebpackvscode-extensions

Webpack: Module not found: Error: Can't resolve <node.js-package>


I am trying to package my first vscode extension that I have tested under debugger.

In package.json, in scripts list, I have added the line

"package": "webpack --mode production --devtool hidden-source-map"

I used the recommended webpack.config.js file with minimal modifications (entry: the top JS file, extensions: I added '.js'). I have NOT changed the module part that I don't understand the role and which is not explained in the tuto.

I succeeded to invoke the webpack command under vscode. All sources have correctly built. But then I get error for all required node.js modules (my own sub-modules seem to be correctly found). The errors say:

Module not found: Error: Can't resolve '<the node.js module name>' in '<source directory>'

The line pointed by the error reads:

const <the node.js module name> = require('<the node.js module name>');

Lot of people have similar errors but not in same context so I found no solution that I feel applicable in my case.


Solution

  • Finally I found how to avoid the error with webpack. The solution is in the part I was not understanding. It seems that webpack does not want to include in the package the node.js modules to avoid that the package, when used with node.js, conflicts with already installed modules.

    But it wants to be instructed to not include them. Else it generates the error that was annoying me.

    The way to avoid the error is too add the following lines for all the node.js modules to avoid including. The example below shows the result for vscode, fs and path modules:

        config.resolve = { 
            fallback: {
                'vscode' : false,
                'path'   : false,
                'fs'     : false,
            },
        };
    

    Note that config is a variable you must use to define what the module will export:

    module.exports = config;
    

    And because you need to have more export data it may be more convenient to initialize config with an object literal:

    const config = {
        .
        .
        .
        resolve: {
          fallback: {
            'vscode'  : false,
            'path'    : false,
            'fs'      : false,
          },
        },
    };
    

    Now I have solved my issue I really don't know what to do with the built package. It is unclear why vscode documentation recommends to use webpack. But that's another issue... I will investigate in the direction given by Mike Lischke.

    PS: After using vsce I realize that it calls webpack so succeeding in configuring and running webpack is actually useful. The hours spent reading the manual were not lost.