Skip to content

error _stringWidth is not a function #155

@DopamineDriven

Description

@DopamineDriven

I use this package in an internal package I wrote for my org. While executing the pipeline, I ran into an error stating that _stringWidth is not a function in file node_modules/cli-progress/lib/formatter.js at line 57:

    const fullMargin = Math.max(0, params.maxWidth - _stringWidth(s) -2);

Upon looking into this file, I noticed that the following import isn't destructured/doesn't key-into the default object

const _stringWidth = require('string-width');

possible workarounds:
(1)

const { default: _stringWidth } = require('string-width');

(2)

const _stringWidth = require("string-width").default;

Instead, I only changed line 57 as follows:

const fullMargin = Math.max(0, params.maxWidth - _stringWidth.default(s) -2);

Which resolved the error.


Type note:

The required _stringWidth const has an alias of

(alias) const _stringWidth: {
    (string: string): number;
    default: typeof stringWidth;
}
import _stringWidth

Contents of output patch file from running yarn patch-package cli-progress using the patch-package npm package

diff --git a/node_modules/cli-progress/lib/formatter.js b/node_modules/cli-progress/lib/formatter.js
index 8ca662d..c59287a 100644
--- a/node_modules/cli-progress/lib/formatter.js
+++ b/node_modules/cli-progress/lib/formatter.js
@@ -54,7 +54,7 @@ module.exports = function defaultFormatter(options, params, payload){
     });
 
     // calculate available whitespace (2 characters margin of error)
-    const fullMargin = Math.max(0, params.maxWidth - _stringWidth(s) -2);
+    const fullMargin = Math.max(0, params.maxWidth - _stringWidth.default(s) -2);
     const halfMargin = Math.floor(fullMargin / 2);
 
     // distribute available whitespace according to position

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions