Yarn workspace with webpack: "Module not found: Error: Can't resolve <package>"

Viewed 1934

I am using yarn workspace. I have my root node_modules, then some apps.. For one of the apps, I use webpack to build it. I need webpack to ignore this root "node_modules" folder.

However, that's just not happening.

This is my webpack config file:

const path = require('path');

const nodeExternals = require('webpack-node-externals');
const webpack = require('webpack');

module.exports = {
    resolve: {
        extensions: ['.ts', '.js'],
    },
    devtool: 'source-map',
    entry: path.resolve(__dirname, 'src/main.ts'),
    target: 'electron-main',
    externals: [
        nodeExternals({
            allowlist: ['webpack/hot/poll?1'],
            //modulesFromFile: true,
        }),
    ],
    plugins: [
        //new webpack.HotModuleReplacementPlugin(),
        new webpack.NormalModuleReplacementPlugin(/.\[BUNDLE_OS_TARGET\]./, (resource) => {
            resource.request = resource.request.replace(/\[BUNDLE_OS_TARGET\]/, process.env.OS_TARGET || 'darwin');
        }),
    ],
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
        ],
    },
    node: {
        __dirname: false,
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js',
    },
};

When I try to compile the app, I get:

ERROR in ../../node_modules/@nestjs/microservices/server/server-rmq.js 31:87-121
Module not found: Error: Can't resolve 'amqp-connection-manager' in 'C:\Users\Tal\Desktop\copyHC\node_modules\@nestjs\microservices\server'
 @ ../../node_modules/@nestjs/microservices/server/index.js 10:21-44
 @ ../../node_modules/@nestjs/microservices/index.js 21:21-40
 @ ./src/main.ts 16:24-56

And when I building the application without yarn workspaces (so the node_modules is in the same level as webpack.config.js), but as single repository, It successfully compiles.

So basically webpack does not ignore it.

I also did: "exclude": ["./dist", "../../node_modules"], in my tsconfig.json file (the relative path points to the root node_modules).

Then I tried adding this to externals in webpack config:

nodeExternals({
    modulesDir: path.resolve(__dirname, '../../node_modules'),
}),

It compiled, but I got the error: enter image description here

1 Answers

So this Q has basically 2 answers. This is an edge case which I hope helps other ones.

The first problem was indeed because of yarn workspace issue, needed to ignore out node_modules folder.

Second issue was Electron js related. I was missing "main": "./dist/main.js", entry in my package.json file

Related