Search code examples
webpackserverless-frameworknrwl-nx

Nx serverless webpack: Module not found: Error: Can't resolve 'xxx'


I am using this template https://github.com/sudokar/nx-serverless to create nx monorepo with serverless freamework, I did not modify any configurations so you can look into it as a reference. it works fine but when I try to import anything from node_modules I get an error during building with webpack

this is a sample of the error

ERROR in ../../node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js 112:15-30
Module not found: Error: Can't resolve 'nock' in 'C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node-pre-gyp\lib\util'
resolve 'nock' in 'C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node-pre-gyp\lib\util'
  Parsed request is a module
  using description file: C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node-pre-gyp\package.json (relative path: ./lib/util)
    resolve as module
      C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node-pre-gyp\lib\util\node_modules doesn't exist or is not a directory
      C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node-pre-gyp\lib\node_modules doesn't exist or is not a directory
      looking for modules in C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node-pre-gyp\node_modules
        single file module
          using description file: C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node-pre-gyp\package.json (relative path: ./node_modules/nock)
            no extension
              C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node-pre-gyp\node_modules\nock doesn't exist
            .mjs
              C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node-pre-gyp\node_modules\nock.mjs doesn't exist
            .json
              C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node-pre-gyp\node_modules\nock.json doesn't exist
            .ts
              C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node-pre-gyp\node_modules\nock.ts doesn't exist
            .js
              C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node-pre-gyp\node_modules\nock.js doesn't exist
        C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node-pre-gyp\node_modules\nock doesn't exist
      C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\@mapbox\node_modules doesn't exist or is not a directory
      C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\node_modules doesn't exist or is not a directory
      looking for modules in C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules
        single file module
          using description file: C:\Users\kktam\Desktop\Dev\nx-serverless-t73\package.json (relative path: ./node_modules/nock)
            no extension
              C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\nock doesn't exist
            .mjs
              C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\nock.mjs doesn't exist
            .json
              C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\nock.json doesn't exist
            .ts
              C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\nock.ts doesn't exist
            .js
              C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\nock.js doesn't exist
        C:\Users\kktam\Desktop\Dev\nx-serverless-t73\node_modules\nock doesn't exist
      C:\Users\kktam\Desktop\Dev\node_modules doesn't exist or is not a directory
      C:\Users\kktam\Desktop\node_modules doesn't exist or is not a directory
      C:\Users\kktam\node_modules doesn't exist or is not a directory
      C:\Users\node_modules doesn't exist or is not a directory
      C:\node_modules doesn't exist or is not a directory
 @ ../../node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js 15:21-62
 @ ../../node_modules/bcrypt/bcrypt.js 3:17-48
 @ ./src/handlers/register/register-handler.ts 14:17-34

And here is the webpack config

import * as path from 'path';
import * as slsw from 'serverless-webpack';
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
import * as webpack from 'webpack';

export const baseWebpackConfig = (
  directory: string
): webpack.Configuration => ({
  context: directory,
  mode: 'production',
  entry: slsw.lib.entries,
  devtool: 'inline-cheap-module-source-map',
  optimization: {
    minimize: true,
    minimizer: [
      () => {
        return () => {
          return {
            terserOptions: {
              format: {
                comments: false,
              },
            },
            extractComments: false,
          };
        };
      },
    ],
  },
  resolve: {
    extensions: ['.mjs', '.json', '.ts', '.js'],
    symlinks: false,
    cacheWithContext: false,
    plugins: [
      new TsconfigPathsPlugin({
        configFile: 'tsconfig.app.json',
      }),
    ],
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(directory, '.webpack'),
    filename: '[name].js',
  },
  target: 'node',
  externals: [],
  module: {
    rules: [
      // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
      {
        test: /\.(tsx?)$/,
        loader: 'ts-loader',
        exclude: [
          [
            path.resolve(directory, 'node_modules'),
            path.resolve(directory, '.serverless'),
            path.resolve(directory, '.webpack'),
          ],
        ],
        options: {
          transpileOnly: true,
          experimentalWatchApi: true,
        },
      },
    ],
  },
  plugins: [],
});

the only thing that I am doing to get this error is import { hash } from 'bcrypt'; in the handler, both deploying or serving locally with serverless-offline are not working because of this webpack error


Solution

  • Your problem is not related to nx.

    The issue here underlies in using bcrypt.

    Alternatively, you can use bcryptjs instead of bcrypt