Skip to content

Only being able to use in nestjs with peerDependencies #41

@EBBach

Description

@EBBach

Problems

I and @yukinoda work on a nestjs project but when I import string-width it return Error [ERR_REQUIRE_ESM]: require() of ES Module

How to reproduce

I create a simple project for demo of the problem here.

import stringWidth from 'string-width';
console.log(stringWidth('a'));

When running nestjs I got this error

$ yarn start
yarn run v1.22.17
$ nest start

/home/bach/Workspace/tmp/demo-nest/dist/main.js:5
const string_width_1 = require("string-width");
                       ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/bach/Workspace/tmp/demo-nest/node_modules/string-width/index.js from /home/bach/Workspace/tmp/demo-nest/dist/main.js not supported.
Instead change the require of index.js in /home/bach/Workspace/tmp/demo-nest/dist/main.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/bach/Workspace/tmp/demo-nest/dist/main.js:5:24)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Workaround

The problem will be solved if I use peerDependencies instead of dependencies for adding string-width

...
  },
  "peerDependencies": {
    "string-width": "^5.1.2"
  },
  "devDependencies": {
...

Question

Is there anyways to add string-width to dependencies in this case? Because I want to avoid peerDependencies

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions