Build error with next.js

Hello, I’m trying to deploy a small next.js app on Netflify without success.
The app is basically a demo and consists of a welcome page and a protected area; I’m using next-auth (typeorm default adapter) for authorization with a remote postgres db on supabase and google as provider.

The app build correctly locally and on vercel but gives me this error on Netlify:

9:32:27 AM: ModuleNotFoundError: Module not found: Error: Can’t resolve ‘mongodb’ in ‘/opt/build/repo/node_modules/typeorm/platform’

I’m not using mongodb so the error seemed strange to me, I’ve installed it as a try and then I received the same error, this time with mysql package.

I checked my local node_modules/typeorm/platform/PlatformTools.js and I found this function:

PlatformTools.load = function (name) {
// if name is not absolute or relative, then try to load package from the node_modules of the directory we are currently in
// this is useful when we are using typeorm package globally installed and it accesses drivers
// that are not installed globally
try {
// switch case to explicit require statements for webpack compatibility.
switch (name) {
/**
* mongodb
/
case “mongodb”:
return require(“mongodb”);
/
*
* hana
/
case “@sap/hana-client”:
return require("@sap/hana-client");
case “hdb-pool”:
return require(“hdb-pool”);
/
*
* mysql
/
case “mysql”:
return require(“mysql”);
case “mysql2”:
return require(“mysql2”);
/
*
* oracle
/
case “oracledb”:
return require(“oracledb”);
/
*
* postgres
/
case “pg”:
return require(“pg”);
case “pg-native”:
return require(“pg-native”);
case “pg-query-stream”:
return require(“pg-query-stream”);
case “typeorm-aurora-data-api-driver”:
return require(“typeorm-aurora-data-api-driver”);
/
*
* redis
/
case “redis”:
return require(“redis”);
case “ioredis”:
return require(“ioredis”);
/
*
* better-sqlite3
/
case “better-sqlite3”:
return require(“better-sqlite3”);
/
*
* sqlite
/
case “sqlite3”:
return require(“sqlite3”);
/
*
* sql.js
/
case “sql.js”:
return require(“sql.js”);
/
*
* sqlserver
/
case “mssql”:
return require(“mssql”);
/
*
* react-native-sqlite
*/
case “react-native-sqlite-storage”:
return require(“react-native-sqlite-storage”);
}
}
catch (err) {
return require(path.resolve(process.cwd() + “/node_modules/” + name));
}
// If nothing above matched and we get here, the package was not listed within PlatformTools
// and is an Invalid Package. To make it explicit that this is NOT the intended use case for
// PlatformTools.load - it’s not just a way to replace require all willy-nilly - let’s throw
// an error.
throw new TypeError("Invalid Package for PlatformTools.load: " + name);
};

If I install mysql, the error is then about missing mysql2 and so on following the switch case order…

If I understand correctly the function is checking if the right package for the db I’m using is installed.

But I don’t understand why is matching every case, someone can point me in the right direction?

Thanks in advance.

Here the full build log:

9:31:16 AM: Now using node v12.18.0 (npm v6.14.4)
9:31:16 AM: Started restoring cached build plugins
9:31:16 AM: Finished restoring cached build plugins
9:31:16 AM: Attempting ruby version 2.7.1, read from environment
9:31:17 AM: Using ruby version 2.7.1
9:31:18 AM: Using PHP version 5.6
9:31:18 AM: Started restoring cached node modules
9:31:18 AM: Finished restoring cached node modules
9:31:18 AM: Installing NPM modules using NPM version 6.14.4
9:31:30 AM: npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules/fsevents):
9:31:30 AM: npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {“os”:“darwin”,“arch”:“any”} (current: {“os”:“linux”,“arch”:“x64”})
9:31:30 AM: added 505 packages from 413 contributors and audited 506 packages in 11.871s
9:31:31 AM: 98 packages are looking for funding
9:31:31 AM: run npm fund for details
9:31:31 AM: found 0 vulnerabilities
9:31:31 AM: NPM modules installed
9:31:31 AM: Started restoring cached go cache
9:31:31 AM: Finished restoring cached go cache
9:31:31 AM: go version go1.14.4 linux/amd64
9:31:31 AM: go version go1.14.4 linux/amd64
9:31:31 AM: Installing missing commands
9:31:31 AM: Verify run directory
9:31:32 AM: ​
9:31:32 AM: ────────────────────────────────────────────────────────────────
9:31:32 AM: Netlify Build
9:31:32 AM: ────────────────────────────────────────────────────────────────
9:31:32 AM: ​
9:31:32 AM: ❯ Version
9:31:32 AM: @netlify/build 11.17.0
9:31:32 AM: ​
9:31:32 AM: ❯ Flags
9:31:32 AM: deployId: 60a21bbd1c4277000830fa01
9:31:32 AM: ​
9:31:32 AM: ❯ Current directory
9:31:32 AM: /opt/build/repo
9:31:32 AM: ​
9:31:32 AM: ❯ Config file
9:31:32 AM: No config file was defined: using default values.
9:31:32 AM: ​
9:31:32 AM: ❯ Context
9:31:32 AM: production
9:31:32 AM: ​
9:31:32 AM: ❯ Installing plugins
9:31:32 AM: - @netlify/plugin-nextjs@3.3.0
9:31:48 AM: ​
9:31:48 AM: ❯ Loading plugins
9:31:48 AM: - @netlify/plugin-nextjs@3.3.0 from Netlify app
9:31:49 AM: ​
9:31:49 AM: ────────────────────────────────────────────────────────────────
9:31:49 AM: 1. onPreBuild command from @netlify/plugin-nextjs
9:31:49 AM: ────────────────────────────────────────────────────────────────
9:31:49 AM: ​
9:31:49 AM: info - Using webpack 5. Reason: no custom webpack configuration in next.config.js webpack5 | Next.js
9:31:49 AM: No Next.js cache to restore.
9:31:49 AM: ​
9:31:49 AM: (@netlify/plugin-nextjs onPreBuild completed in 715ms)
9:31:49 AM: ​
9:31:49 AM: ────────────────────────────────────────────────────────────────
9:31:49 AM: 2. Build command from Netlify app
9:31:49 AM: ────────────────────────────────────────────────────────────────
9:31:49 AM: ​
9:31:49 AM: $ npm run build
9:31:50 AM: > uma-blog@0.1.0 build /opt/build/repo
9:31:50 AM: > next build
9:31:50 AM: info - Using webpack 5. Reason: no custom webpack configuration in next.config.js webpack5 | Next.js
9:31:50 AM: warn - No build cache found. Please configure build caching for faster rebuilds. Read more: no-cache | Next.js
9:31:50 AM: info - Checking validity of types…
9:31:50 AM: info - Creating an optimized production build…
9:32:04 AM: (node:1526) [DEP_WEBPACK_MODULE_UPDATE_HASH] DeprecationWarning: Module.updateHash: Use new ChunkGraph API
9:32:19 AM: [webpack.cache.PackFileCacheStrategy] Serializing big strings (763kiB) impacts deserialization performance (consider using Buffer instead and decode when needed)
9:32:19 AM: [webpack.cache.PackFileCacheStrategy] Serializing big strings (248kiB) impacts deserialization performance (consider using Buffer instead and decode when needed)
9:32:19 AM: [webpack.cache.PackFileCacheStrategy] Serializing big strings (637kiB) impacts deserialization performance (consider using Buffer instead and decode when needed)
9:32:27 AM: Failed to compile.
9:32:27 AM:
9:32:27 AM: ModuleNotFoundError: Module not found: Error: Can’t resolve ‘mongodb’ in ‘/opt/build/repo/node_modules/typeorm/platform’
9:32:27 AM: > Build error occurred
9:32:27 AM: Error: > Build failed because of webpack errors
9:32:27 AM: at /opt/build/repo/node_modules/next/dist/build/index.js:17:924
9:32:27 AM: at async Span.traceAsyncFn (/opt/build/repo/node_modules/next/dist/telemetry/trace/trace.js:6:584)
9:32:27 AM: npm ERR! code ELIFECYCLE
9:32:27 AM: npm ERR! errno 1
9:32:27 AM: npm ERR! uma-blog@0.1.0 build: next build
9:32:27 AM: npm ERR! Exit status 1
9:32:27 AM: npm ERR!
9:32:27 AM: npm ERR! Failed at the uma-blog@0.1.0 build script.
9:32:27 AM: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
9:32:27 AM: npm ERR! A complete log of this run can be found in:
9:32:27 AM: npm ERR! /opt/buildhome/.npm/_logs/2021-05-17T07_32_27_907Z-debug.log
9:32:27 AM: ​
9:32:27 AM: ────────────────────────────────────────────────────────────────
9:32:27 AM: “build.command” failed
9:32:27 AM: ────────────────────────────────────────────────────────────────
9:32:27 AM: ​
9:32:27 AM: Error message
9:32:27 AM: Command failed with exit code 1: npm run build
9:32:27 AM: ​
9:32:27 AM: Error location
9:32:27 AM: In Build command from Netlify app:
9:32:27 AM: npm run build
9:32:27 AM: ​
9:32:27 AM: Resolved config
9:32:27 AM: build:
9:32:27 AM: command: npm run build
9:32:27 AM: commandOrigin: ui
9:32:27 AM: environment:
9:32:27 AM: - DB_HOST
9:32:27 AM: - DB_NAME
9:32:27 AM: - DB_PASSWORD
9:32:27 AM: - DB_PORT
9:32:27 AM: - DB_USERNAME
9:32:27 AM: - DOMAIN
9:32:27 AM: - GOOGLE_CLIENT_ID
9:32:27 AM: - GOOGLE_CLIENT_SECRET
9:32:27 AM: - NEXTAUTH_URL
9:32:27 AM: publish: /opt/build/repo/out
9:32:27 AM: plugins:
9:32:27 AM: - inputs: {}
9:32:27 AM: origin: ui
9:32:27 AM: package: ‘@netlify/plugin-nextjs’
9:32:28 AM: Caching artifacts
9:32:28 AM: Started saving node modules
9:32:28 AM: Finished saving node modules
9:32:28 AM: Started saving build plugins
9:32:28 AM: Finished saving build plugins
9:32:28 AM: Started saving pip cache
9:32:28 AM: Finished saving pip cache
9:32:28 AM: Started saving emacs cask dependencies
9:32:28 AM: Finished saving emacs cask dependencies
9:32:28 AM: Started saving maven dependencies
9:32:28 AM: Finished saving maven dependencies
9:32:28 AM: Started saving boot dependencies
9:32:28 AM: Finished saving boot dependencies
9:32:28 AM: Started saving rust rustup cache
9:32:28 AM: Finished saving rust rustup cache
9:32:28 AM: Started saving go dependencies
9:32:28 AM: Finished saving go dependencies
9:32:30 AM: Build failed due to a user error: Build script returned non-zero exit code: 2
9:32:30 AM: Creating deploy upload records
9:32:30 AM: Failing build: Failed to build site
9:32:30 AM: Failed during stage ‘building site’: Build script returned non-zero exit code: 2
9:32:30 AM: Finished processing build request in 1m19.066392905s

hi there, sorry to be a little slow to reply.

Could you look through the issues here:

and see if there is anything existing that matches what you are seeing? if not, please do file a new issue so the next plugin team can evaluate. thanks!

Sadly I can’t see anything related to my problem, I will file a new issue asap.
Thanks for your help perry,
Cheers

1 Like