sindresorhus / filenamify

Convert a string to a valid safe filename
MIT License
489 stars 26 forks source link

[Question] not able to use this package from a typescript project #29

Closed cunhabruno closed 2 years ago

cunhabruno commented 2 years ago

Hello,

I am trying to use this package in a typescript project, using ts-node to execute, my code is literally only this:

import filenamify from "filenamify"
console.log(filenamify('<file123>'))

then I execute it. using the following command:

npx ts-node src/test.ts 

and then the following error get displayed:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/user1/test-projects/test-filenamify/node_modules/filenamify/index.js
require() of ES modules is not supported.
require() of /Users/user1/test-projects/test-filenamify/node_modules/filenamify/index.js from /Users/user1/test-projects/test-filenamify/src/test.ts 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 /Users/user1/test-projects/test-filenamify/node_modules/filenamify/package.json.

I have tried using dynamic import. and require. and I was not able to make it work, if you execute it using runkit, it also have a similar error https://npm.runkit.com/filenamify

sindresorhus commented 2 years ago

You have to use the previous major version or wait for TS 4.6. This is a TS issue.

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