serverless-heaven / serverless-webpack

Serverless plugin to bundle your lambdas with Webpack
MIT License
1.72k stars 416 forks source link

Terser Error when importing mongoose #529

Open timoangerer opened 5 years ago

timoangerer commented 5 years ago

This is a (Bug Report / Feature Proposal)

Bug Report

Description

Steps to reproduce:

  1. create new ts sls project: serverless create --template aws-nodejs-typescript && npm install
  2. install mongoose
  3. import mongoose and use it somewhere in your code

For bug reports:

const path = require("path");
const slsw = require("serverless-webpack");

module.exports = {
  mode: slsw.lib.webpack.isLocal ? "development" : "production",
  entry: slsw.lib.entries,
  devtool: "source-map",
  resolve: {
    extensions: [".js", ".jsx", ".json", ".ts", ".tsx"],
    alias: {
      "~": path.resolve(__dirname, "src")
    }
  },
  output: {
    libraryTarget: "commonjs",
    path: path.join(__dirname, ".webpack"),
    filename: "[name].js"
  },
  target: "node",
  module: {
    rules: [
      // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
      { test: /\.tsx?$/, loader: "ts-loader" }
    ]
  }
};
WARNING in ./node_modules/express/lib/view.js 81:13-25
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/express/lib/application.js
 @ ./node_modules/express/lib/express.js
 @ ./node_modules/express/index.js
 @ ./src/index.ts

WARNING in ./node_modules/mongoose/lib/index.js 11:28-64
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/mongoose/index.js
 @ ./src/rest/routes/index.ts
 @ ./src/index.ts

WARNING in ./node_modules/require_optional/index.js 82:18-42
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/mongodb-core/index.js
 @ ./node_modules/mongoose/lib/connection.js
 @ ./node_modules/mongoose/lib sync ^.*\/connection$
 @ ./node_modules/mongoose/lib/index.js
 @ ./node_modules/mongoose/index.js
 @ ./src/rest/routes/index.ts
 @ ./src/index.ts

WARNING in ./node_modules/require_optional/index.js 90:20-44
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/mongodb-core/index.js
 @ ./node_modules/mongoose/lib/connection.js
 @ ./node_modules/mongoose/lib sync ^.*\/connection$
 @ ./node_modules/mongoose/lib/index.js
 @ ./node_modules/mongoose/index.js
 @ ./src/rest/routes/index.ts
 @ ./src/index.ts

WARNING in ./node_modules/require_optional/index.js 97:35-67
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/mongodb-core/index.js
 @ ./node_modules/mongoose/lib/connection.js
 @ ./node_modules/mongoose/lib sync ^.*\/connection$
 @ ./node_modules/mongoose/lib/index.js
 @ ./node_modules/mongoose/index.js
 @ ./src/rest/routes/index.ts
 @ ./src/index.ts

ERROR in src/index.js from Terser
undefined

  Error --------------------------------------------------

  Webpack compilation error, see above

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Stack Trace --------------------------------------------

Error: Webpack compilation error, see above
    at _.forEach.compileStats (/home/timo/dev/mms/mss_serverlessbackend/node_modules/serverless-webpack/lib/compile.js:38:19)
    at arrayEach (/home/timo/dev/mms/mss_serverlessbackend/node_modules/lodash/lodash.js:516:11)
    at Function.forEach (/home/timo/dev/mms/mss_serverlessbackend/node_modules/lodash/lodash.js:9342:14)
    at BbPromise.fromCallback.then.stats (/home/timo/dev/mms/mss_serverlessbackend/node_modules/serverless-webpack/lib/compile.js:31:11)
    at finalCallback (/home/timo/dev/mms/mss_serverlessbackend/node_modules/webpack/lib/Compiler.js:257:39)
    at hooks.done.callAsync.err (/home/timo/dev/mms/mss_serverlessbackend/node_modules/webpack/lib/Compiler.js:273:13)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/timo/dev/mms/mss_serverlessbackend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/home/timo/dev/mms/mss_serverlessbackend/node_modules/tapable/lib/Hook.js:154:20)
    at onCompiled (/home/timo/dev/mms/mss_serverlessbackend/node_modules/webpack/lib/Compiler.js:271:21)
    at hooks.afterCompile.callAsync.err (/home/timo/dev/mms/mss_serverlessbackend/node_modules/webpack/lib/Compiler.js:671:15)
    at _err0 (eval at create (/home/timo/dev/mms/mss_serverlessbackend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at /home/timo/dev/mms/mss_serverlessbackend/node_modules/ts-loader/dist/after-compile.js:28:9
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/timo/dev/mms/mss_serverlessbackend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:7:1)
    at AsyncSeriesHook.lazyCompileHook (/home/timo/dev/mms/mss_serverlessbackend/node_modules/tapable/lib/Hook.js:154:20)
    at compilation.seal.err (/home/timo/dev/mms/mss_serverlessbackend/node_modules/webpack/lib/Compiler.js:668:31)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/timo/dev/mms/mss_serverlessbackend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/home/timo/dev/mms/mss_serverlessbackend/node_modules/tapable/lib/Hook.js:154:20)
    at hooks.optimizeAssets.callAsync.err (/home/timo/dev/mms/mss_serverlessbackend/node_modules/webpack/lib/Compilation.js:1385:35)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/timo/dev/mms/mss_serverlessbackend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/home/timo/dev/mms/mss_serverlessbackend/node_modules/tapable/lib/Hook.js:154:20)
    at hooks.optimizeChunkAssets.callAsync.err (/home/timo/dev/mms/mss_serverlessbackend/node_modules/webpack/lib/Compilation.js:1376:32)
    at _err0 (eval at create (/home/timo/dev/mms/mss_serverlessbackend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at taskRunner.run (/home/timo/dev/mms/mss_serverlessbackend/node_modules/terser-webpack-plugin/dist/index.js:321:9)
    at step (/home/timo/dev/mms/mss_serverlessbackend/node_modules/terser-webpack-plugin/dist/TaskRunner.js:87:9)
    at done (/home/timo/dev/mms/mss_serverlessbackend/node_modules/terser-webpack-plugin/dist/TaskRunner.js:98:30)
    at boundWorkers (/home/timo/dev/mms/mss_serverlessbackend/node_modules/terser-webpack-plugin/dist/TaskRunner.js:103:13)
    at /home/timo/dev/mms/mss_serverlessbackend/node_modules/worker-farm/lib/farm.js:199:19
    at process._tickCallback (internal/process/next_tick.js:61:11)
From previous event:
    at PluginManager.invoke (/usr/lib/node_modules/serverless/lib/classes/PluginManager.js:446:22)
    at PluginManager.spawn (/usr/lib/node_modules/serverless/lib/classes/PluginManager.js:464:17)
    at ServerlessWebpack.BbPromise.bind.then.then (/home/timo/dev/mms/mss_serverlessbackend/node_modules/serverless-webpack/index.js:101:51)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
    at process.topLevelDomainCallback (domain.js:126:23)

For feature proposals:

Similar or dependent issue(s):

Additional Data

timoangerer commented 5 years ago

A workaround for the error is to add the following to your webpack.config.js

optimization: {
    minimize: false
}

This will prevent Terser from trying to minify your code

dough654 commented 5 years ago

I'm getting a similar issue when deploying with serverless in our bitbucket pipeline using docker image node:10.15.3. I also have mongoose installed. Although it previously had not been an issue, and we have not changed any package versions.


 Serverless Warning --------------------------------------

  A valid SSM parameter to satisfy the declaration 'ssm:/swamp-filter/swamp-conn-str/test~true' could not be found.

 Serverless Warning --------------------------------------

  A valid SSM parameter to satisfy the declaration 'ssm:/swamp-filter/swamp-conn-str/staging~true' could not be found.

 Serverless Warning --------------------------------------

  A valid SSM parameter to satisfy the declaration 'ssm:/swamp-filter/subj-area-conn-str/test~true' could not be found.

 Serverless Warning --------------------------------------

  A valid SSM parameter to satisfy the declaration 'ssm:/swamp-filter/subj-area-conn-str/staging~true' could not be found.

 Serverless Warning --------------------------------------

  A valid SSM parameter to satisfy the declaration 'ssm:/swamp-filter/quarantined-conn-str/test~true' could not be found.

 Serverless Warning --------------------------------------

  A valid SSM parameter to satisfy the declaration 'ssm:/swamp-filter/quarantined-conn-str/staging~true' could not be found.

Serverless: Bundling with Webpack...
Time: 17563ms
Built at: 08/09/2019 8:54:11 PM
                                     Asset      Size  Chunks  Chunk Names
    src/function-handlers/queue/handler.js   3.9 MiB       0  src/function-handlers/queue/handler
src/function-handlers/queue/handler.js.map  4.79 MiB       0  src/function-handlers/queue/handler
Entrypoint src/function-handlers/queue/handler = src/function-handlers/queue/handler.js src/function-handlers/queue/handler.js.map
 [50] external "fs" 42 bytes {0} [built]
 [59] external "path" 42 bytes {0} [built]
 [79] ./config/config.ts 6.32 KiB {0} [built]
 [80] ./node_modules/mongoose/index.js 87 bytes {0} [built]
[161] external "module" 42 bytes {0} [built]
[249] multi ./source-map-install.js ./src/function-handlers/queue/handler.ts 40 bytes {0} [built]
[250] ./source-map-install.js 41 bytes {0} [built]
[251] ./node_modules/source-map-support/source-map-support.js 17.7 KiB {0} [built]
[252] ./node_modules/source-map/source-map.js 405 bytes {0} [built]
[259] ./node_modules/buffer-from/index.js 1.56 KiB {0} [built]
[260] ./src/function-handlers/queue/handler.ts 1.01 KiB {0} [built]
[261] ./src/runner.ts 3.64 KiB {0} [built]
[436] ./src/swamp/extract.ts 1.5 KiB {0} [built]
[465] ./src/quarantine/populate.ts 1.66 KiB {0} [built]
[466] ./src/subject-areas/retrieve.ts 630 bytes {0} [built]
    + 453 hidden modules
WARNING in ./node_modules/mongoose/lib/index.js 8:26-62
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/mongoose/index.js
 @ ./config/config.ts
 @ ./src/runner.ts
 @ ./src/function-handlers/queue/handler.ts
 @ multi ./source-map-install.js ./src/function-handlers/queue/handler.ts
WARNING in ./node_modules/require_optional/index.js 82:18-42
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/mongodb-core/index.js
 @ ./node_modules/mongodb/index.js
 @ ./src/util/db.ts
 @ ./src/swamp/extract.ts
 @ ./src/runner.ts
 @ ./src/function-handlers/queue/handler.ts
 @ multi ./source-map-install.js ./src/function-handlers/queue/handler.ts
WARNING in ./node_modules/require_optional/index.js 90:20-44
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/mongodb-core/index.js
 @ ./node_modules/mongodb/index.js
 @ ./src/util/db.ts
 @ ./src/swamp/extract.ts
 @ ./src/runner.ts
 @ ./src/function-handlers/queue/handler.ts
 @ multi ./source-map-install.js ./src/function-handlers/queue/handler.ts
WARNING in ./node_modules/require_optional/index.js 97:35-67
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/mongodb-core/index.js
 @ ./node_modules/mongodb/index.js
 @ ./src/util/db.ts
 @ ./src/swamp/extract.ts
 @ ./src/runner.ts
 @ ./src/function-handlers/queue/handler.ts
 @ multi ./source-map-install.js ./src/function-handlers/queue/handler.ts
ERROR in src/function-handlers/queue/handler.js from Terser
undefined
Unhandled rejection Error: Webpack compilation error, see above
    at _.forEach.compileStats (/opt/atlassian/pipelines/agent/build/node_modules/serverless-webpack/lib/compile.js:38:19)
    at arrayEach (/opt/atlassian/pipelines/agent/build/node_modules/lodash/lodash.js:516:11)
    at Function.forEach (/opt/atlassian/pipelines/agent/build/node_modules/lodash/lodash.js:9342:14)
    at BbPromise.fromCallback.then.stats (/opt/atlassian/pipelines/agent/build/node_modules/serverless-webpack/lib/compile.js:31:11)
    at tryCatcher (/opt/atlassian/pipelines/agent/build/node_modules/serverless-webpack/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/opt/atlassian/pipelines/agent/build/node_modules/serverless-webpack/node_modules/bluebird/js/release/promise.js:517:31)
    at Promise._settlePromise (/opt/atlassian/pipelines/agent/build/node_modules/serverless-webpack/node_modules/bluebird/js/release/promise.js:574:18)
    at Promise._settlePromise0 (/opt/atlassian/pipelines/agent/build/node_modules/serverless-webpack/node_modules/bluebird/js/release/promise.js:619:10)
    at Promise._settlePromises (/opt/atlassian/pipelines/agent/build/node_modules/serverless-webpack/node_modules/bluebird/js/release/promise.js:699:18)
    at Promise._fulfill (/opt/atlassian/pipelines/agent/build/node_modules/serverless-webpack/node_modules/bluebird/js/release/promise.js:643:18)
    at /opt/atlassian/pipelines/agent/build/node_modules/serverless-webpack/node_modules/bluebird/js/release/nodeback.js:42:21
    at finalCallback (/opt/atlassian/pipelines/agent/build/node_modules/webpack/lib/Compiler.js:257:39)
    at hooks.done.callAsync.err (/opt/atlassian/pipelines/agent/build/node_modules/webpack/lib/Compiler.js:273:13)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/atlassian/pipelines/agent/build/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/opt/atlassian/pipelines/agent/build/node_modules/tapable/lib/Hook.js:154:20)
    at onCompiled (/opt/atlassian/pipelines/agent/build/node_modules/webpack/lib/Compiler.js:271:21)
    at hooks.afterCompile.callAsync.err (/opt/atlassian/pipelines/agent/build/node_modules/webpack/lib/Compiler.js:671:15)
    at _err0 (eval at create (/opt/atlassian/pipelines/agent/build/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at /opt/atlassian/pipelines/agent/build/node_modules/ts-loader/dist/after-compile.js:27:9
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/atlassian/pipelines/agent/build/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:7:1)
    at AsyncSeriesHook.lazyCompileHook (/opt/atlassian/pipelines/agent/build/node_modules/tapable/lib/Hook.js:154:20)
    at compilation.seal.err (/opt/atlassian/pipelines/agent/build/node_modules/webpack/lib/Compiler.js:668:31)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/atlassian/pipelines/agent/build/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/opt/atlassian/pipelines/agent/build/node_modules/tapable/lib/Hook.js:154:20)
    at hooks.optimizeAssets.callAsync.err (/opt/atlassian/pipelines/agent/build/node_modules/webpack/lib/Compilation.js:1385:35)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/opt/atlassian/pipelines/agent/build/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/opt/atlassian/pipelines/agent/build/node_modules/tapable/lib/Hook.js:154:20)
    at hooks.optimizeChunkAssets.callAsync.err (/opt/atlassian/pipelines/agent/build/node_modules/webpack/lib/Compilation.js:1376:32)
    at _err0 (eval at create (/opt/atlassian/pipelines/agent/build/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at taskRunner.run (/opt/atlassian/pipelines/agent/build/node_modules/terser-webpack-plugin/dist/index.js:321:9)
    at step (/opt/atlassian/pipelines/agent/build/node_modules/terser-webpack-plugin/dist/TaskRunner.js:87:9)
    at done (/opt/atlassian/pipelines/agent/build/node_modules/terser-webpack-plugin/dist/TaskRunner.js:98:30)
    at boundWorkers (/opt/atlassian/pipelines/agent/build/node_modules/terser-webpack-plugin/dist/TaskRunner.js:103:13)
    at /opt/atlassian/pipelines/agent/build/node_modules/worker-farm/lib/farm.js:199:19
    at process._tickCallback (internal/process/next_tick.js:61:11)

serverless version: 1.49.1 serverless-webpack version: ^5.1.1 webpack version: ^4.5.1

I can also confirm that the workaround proposed by timoangerer works for us as well.

Strangely enough, I cannot reproduce the problem locally on my mac. I've made sure to use the same version of node as well as all npm packages, and nothing. Maybe there is an ubuntu component to the issue as well?

richarddd commented 5 years ago

I already created an issue and the author of Terser is working very hard to fix it: https://github.com/terser-js/terser/issues/432.

As a temporary workaround, use this config to still have minification enabled:

new TerserPlugin({
        //... your other options if you have any
        terserOptions: {
          //... your other options if you have any
          compress: {
            reduce_vars: false, //when the bug is fixed this could be changed to true
          },
        },
      }),