Node-mysql2: Make `cardinal` an optional dependency

Created on 1 Sep 2018  路  16Comments  路  Source: sidorares/node-mysql2

I am currently trying to reduce to package footprint of some of my projects:

cardinal is only used in 2 places to highlight code, which in turns requires redeyed which in turn requires esprima.

I suggest either removing it from dependencies or making it an optional dependency since it is only required in debug mode, you might print a message install cardinal for pretty debug formatting or something.

All 16 comments

@SimonSchick yeah, happy for this change. Would you like to volunteer? Print colored debug if installed, or 'install cardinal for pretty debug formatting' + uncolored if not

@sidorares done

fixed by #847

I can see cardinal is not a required dependency anymore.
Webpack will still emit a warning:

Module not found: Error: Can't resolve 'cardinal'

But it still bundles and seems to work.

iirc webpack parses require('cardinal') you should be able to suppress that warning.

@SimonSchick Yes, we're suppressing it via webpack node api.

    compiler.run((error, stats) => {
      if (error) {
        return reject(error);
      }

      // stats.toJson("minimal")
      // more options: "verbose", etc.
      const info = stats.toJson();

      if (stats.hasErrors()) {
        return reject(new Error(`Fix the following webpack build errors:\n${info.errors.map(error => '\n' + error + '\n')}`));
      }

      // There will be "Critical dependency: the request of a dependency is an expression" warnings.
      // No big deal for server side.
      // https://github.com/webpack/webpack/issues/196
      const warnings = info.warnings
        .filter(warning => warning.indexOf('Critical dependency: the request of a dependency is an expression') < 0)
        // `mysql2` has an optional `cardinal` dependency.
        // https://github.com/sidorares/node-mysql2/issues/846
        .filter(warning => warning.indexOf("Can't resolve 'cardinal'") < 0);

      if (warnings.length > 0) {
        console.warn(`Webpack build warnings (not critical):\n${warnings.map(warning => '\n' + warning + '\n')}`);
      }

      resolve(info);
    });

@sidorares Cardinal is back as dependency after re-adding of package.json. Should it be removed again?

Should it be removed again?

yes, not sure how this happened

@stasas could you make a PR?

@sidorares Done.

I use the mysql2 2.2.5 version which does not have cardinal but the helper.js requires for the cardinal as below. How to solve this?

Error: Can't walk dependency graph: Cannot find module 'cardinal' from 'node_modulesmysql2lib'
聽required by node_modulesmysql2libhelpers.js

@ashokkumarg in the latest versions require('cardinal') is wrapped in try/catch block and should not produce an error - https://github.com/sidorares/node-mysql2/blob/a640d471f043eb078c09ab0d6016030c315bd879/lib/helpers.js#L28

@sidorares

in the latest versions require('cardinal') is wrapped in try/catch block and should not produce an error

No, still the same error with the latest 2.2.5 version.

c:/dev/JITU/node_modules/mysql2/lib/helpers.js
Module not found: Error: Can't resolve 'cardinal' in 'c:\dev\JITU\node_modules\mysql2\lib'
resolve 'cardinal' in 'c:\dev\JITU\node_modules\mysql2\lib'
cat c:\dev\JITU\node_modules\mysql2\package.json
{
  "name": "mysql2",
  "version": "2.2.5",
Was this page helpful?
0 / 5 - 0 ratings

Related issues

c24w picture c24w  路  5Comments

DirkWolthuis picture DirkWolthuis  路  3Comments

magicxie picture magicxie  路  6Comments

juliandavidmr picture juliandavidmr  路  4Comments

AnyhowStep picture AnyhowStep  路  5Comments