sindresorhus / cli-truncate

Truncate a string to a specific width in the terminal
MIT License
85 stars 14 forks source link

Must use import to load ES Module #17

Closed IzioDev closed 3 years ago

IzioDev commented 3 years ago

Hello,

I just installed this package, which by the way, seems to be the only one that does truncation and is currently maintained.

However, I have an issue while using this package inside my Next.Js application (in Typescript).

Here is how I import the package :

import cliTruncate from "cli-truncate";
// I also tried
import * as cliTruncate from "cli-truncate";

And here is the error on runtime:

Error: Must use import to load ES Module: D:\workspace\my-app\node_modules\cli-truncate\index.js
require() of ES modules is not supported.
require() of D:\workspace\my-app\node_modules\cli-truncate\index.js from D:\workspace\my-app\packages\website\.next\server\pages\blog.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from D:\workspace\my-app\node_modules\cli-truncate\package.json.

Do you have any idea why is this happening?

sindresorhus commented 3 years ago

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

IzioDev commented 3 years ago

@sindresorhus As you can see, I'm not requiring. I'm using the import syntax to load ES module.

sindresorhus commented 3 years ago

Yes, but Next.js transpiles it down to require calls by default.

IzioDev commented 3 years ago

Alright. Will fork and adapt, thanks.