Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module 'filenamify/browser' or its corresponding type declarations.ts #26

Closed
sagar-gavhane opened this issue Aug 30, 2021 · 5 comments

Comments

@sagar-gavhane
Copy link

Hey @sindresorhus,

I'm new to this library and want to use filenamify in the TypeScript based React project. As per README.md file, I'm importing filenamify from filenamify/browser but it throwing a Cannot find module 'filenamify/browser' error.

Is there any workaround to this issue?

@sindresorhus
Copy link
Owner

This is a problem with your build tool. It needs to support the exports property:

filenamify/package.json

Lines 14 to 17 in 43f3f32

"exports": {
".": "./index.js",
"./browser": "./filenamify.js"
},

@DavidSchillinger
Copy link

I don't intend to resurrect this issue, I just want to provide some more context for anyone visiting, which I hope is ok:

TypeScript is working on support for exports: microsoft/TypeScript#33079

That said, as a workaround, the only way I've been able to get it to work in my TS projects is to:
import filenamify from 'filenamify/filenamify';

I only do this because I understand the risks.
e.g. I don't get to complain if the path to it changes in a non-major version bump and breaks my setup!

Thank you for filenamify 😃

@crucialfelix
Copy link

Granted, this may be something not correctly configured with my build tool or with ts-node, but I am unable to import this in a Node TypeScript project

❯ /usr/bin/env ts-node "/Users/crucialfelix/github/scanner/cli.ts"
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/crucialfelix/github/scanner/node_modules/filenamify/index.js from /Users/crucialfelix/github/scanner/src/lib/storage.ts not supported.
Instead change the require of index.js in /Users/crucialfelix/github/scanner/src/lib/storage.ts to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/crucialfelix/github/scanner/src/lib/storage.ts:10:38)
    at Module.m._compile (/Users/crucialfelix/.nvm/versions/node/v16.6.2/lib/node_modules/ts-node/dist/index.js:704:29)

@Primajin
Copy link

Primajin commented Aug 30, 2022

I could get it to run dirtily 🙈

with

// @ts-ignore TS2307: Cannot find module 'filenamify/browser' or its corresponding type declarations.
import filenamify from "filenamify/browser";

and for the tests:

// src/__mocks__/filenamify/browser/index.js
module.exports = {
  filenamify: () => jest.fn(),
};

@hseager
Copy link

hseager commented Nov 3, 2023

You can add this to your tsconfig to point the import to the correct type definitions:

{
  "compilerOptions": {
    "paths": {
      "filenamify/browser": ["./node_modules/filenamify/filenamify.d.ts"]
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants