nest icon indicating copy to clipboard operation
nest copied to clipboard

Nest 9.1.3 version does not work with webpack config from the serverless page

Open alejandrombc opened this issue 2 years ago • 4 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current behavior

If you follow the tutorial about serverless + nest (https://docs.nestjs.com/faq/serverless) you will get a webpack.config.js file that works perfectly with 9.1.2 version but not on the 9.1.3.

When you do a fresh install and then run nest build --webpack you will get the following error:

Module not found: Error: Can't resolve 'cache-manager/package.json' in '/nest-project/node_modules/@nestjs/common/cache'

webpack file:

const TerserPlugin = require('terser-webpack-plugin');

module.exports = (options, webpack) => {
  const lazyImports = [
    '@nestjs/microservices/microservices-module',
    '@nestjs/websockets/socket-module',
  ];

  return {
    ...options,
    externals: [],
    optimization: {
      minimizer: [
        new TerserPlugin({
          terserOptions: {
            keep_classnames: true,
          },
        }),
      ],
    },
    output: {
      ...options.output,
      libraryTarget: 'commonjs2',
    },
    plugins: [
      ...options.plugins,
      new webpack.IgnorePlugin({
        checkResource(resource) {
          if (lazyImports.includes(resource)) {
            try {
              require.resolve(resource);
            } catch (err) {
              return true;
            }
          }
          return false;
        },
      }),
    ],
  };
};

Minimum reproduction code

May be not necessary

Steps to reproduce

No response

Expected behavior

To compile the code like 9.1.2 did

Package

  • [ ] I don't know. Or some 3rd-party package
  • [X] @nestjs/common
  • [ ] @nestjs/core
  • [ ] @nestjs/microservices
  • [ ] @nestjs/platform-express
  • [ ] @nestjs/platform-fastify
  • [ ] @nestjs/platform-socket.io
  • [ ] @nestjs/platform-ws
  • [ ] @nestjs/testing
  • [ ] @nestjs/websockets
  • [ ] Other (see below)

Other package

No response

NestJS version

9.1.3

Packages versions

  "dependencies": {
    "@nestjs/common": "9.1.3",
    "@nestjs/core": "9.1.3",
    "@nestjs/platform-express": "9.1.3",
    "@vendia/serverless-express": "^4.10.1",
    "aws-lambda": "^1.0.7",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "9.1.4",
    "@nestjs/schematics": "9.0.3",
    "@nestjs/testing": "9.1.3",
    "@types/aws-lambda": "^8.10.106",
    "@types/express": "^4.17.13",
    "@types/jest": "28.1.8",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "28.1.3",
    "prettier": "^2.3.2",
    "serverless": "^3.22.0",
    "serverless-jetpack": "^0.11.1",
    "serverless-offline": "^9.0.0",
    "serverless-plugin-warmup": "^8.1.0",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "terser-webpack-plugin": "^5.3.6",
    "ts-jest": "28.0.8",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.0",
    "typescript": "^4.7.4"
  },

Node.js version

16.12.0

In which operating systems have you tested?

  • [X] macOS
  • [ ] Windows
  • [ ] Linux

Other

No response

alejandrombc avatar Oct 05 '22 13:10 alejandrombc

cc @jmcdo29

kamilmysliwiec avatar Oct 05 '22 13:10 kamilmysliwiec

This should be fixed in 9.1.4

kamilmysliwiec avatar Oct 05 '22 13:10 kamilmysliwiec

Ah, yeah, this would be due to the new require I added in. @kamilmysliwiec does your loadRequire() bypass webpack's replacement, or is this something we'll need to update in the default webpack config in the @nestjs/cli repo? Or, does webpack replace "dynamic" requires? Like

const packageName = 'cache-manager/package.json'
const cacheManagerVersion = require(packageName).version

?

I'll give a couple of these a shot to see what webpack will do once I have the factory fixed up and no longer flaking in tests

jmcdo29 avatar Oct 05 '22 15:10 jmcdo29

I had the same error message when using @nestjs/ng-universal and @nestjs/common (both 9.1.3 and 9.1.4). Downgrade to @nestjs/[email protected] fixed it.

Markus-Ende avatar Oct 13 '22 19:10 Markus-Ende

I have this error also with 9.1.4.

nicod-pc avatar Oct 21 '22 10:10 nicod-pc

This problem still occurs with 9.1.5.

nicod-pc avatar Oct 24 '22 09:10 nicod-pc

Fixed (9.1.6)

kamilmysliwiec avatar Oct 24 '22 11:10 kamilmysliwiec