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

Could not find a declaration file for module 'uuid' (Incompatibility with ES6 Import) #668

Closed
sulemanelahi opened this issue Dec 20, 2022 · 6 comments

Comments

@sulemanelahi
Copy link

I'm using TypeScript inside my project. If I try to use ES5 import style like
const { v4: uuidv4 } = require('uuid');
So I don't get any error while compilation but when I convert it into ES6 style like
import { v4 as uuidv4 } from 'uuid';
I get a compilation error like
Could not find a declaration file for module 'uuid'. 'C:/Users/project/dependencies/nodejs/node_modules/uuid/dist/index.js' implicitly has an 'any' type. Try 'npm i --save-dev @types/uuid' if it exists or add a new declaration (.d.ts) file containing 'declare module 'uuid';'

I tried the available solutions to fix this kind of error but nothing worked. In the end I created a new declaration file inside node_modules/uuid/ and named it index.d.ts

And add the following line to fix the issue:

declare module 'uuid';

And then the error is gone.

Can anyone fix this type of issue?

@sulemanelahi sulemanelahi changed the title Could not find a declaration file for module 'uuid' Could not find a declaration file for module 'uuid' (Incompatibility with ES6 Import) Dec 20, 2022
@LinusU
Copy link
Member

LinusU commented Dec 20, 2022

From the error message:

Try 'npm i --save-dev @types/uuid' [...]

Did you try this?

For now you need to add the @types/uuid package in order to get typings. Shipping with typings is on it's way in #654

@LinusU LinusU closed this as completed Dec 20, 2022
@sulemanelahi
Copy link
Author

As I already mentioned that I did tried all possible solution to fix the error message but nothing works. Yes, I already tried the types installation solution but its not working.

@LinusU
Copy link
Member

LinusU commented Dec 20, 2022

It's impossible for me to know what you mean with "all possible solution" 😅 If you mean that literally, then there is nothing I can do to help since all the possible solutions are already tried...

After installing @types/uuid, do you still get the exact same error message?

@sulemanelahi
Copy link
Author

sulemanelahi commented Dec 20, 2022

Yes, the same error occurs even after installing @types/uuid.

The only solution that works for me that I tried is to create a new declaration file inside node_modules/uuid/ like node_modules/uuid/index.d.ts

And add the following line to index.d.ts to fix the issue:

declare module 'uuid';

@LinusU
Copy link
Member

LinusU commented Dec 20, 2022

Hmm, I'm not able to reproduce this:

$ cd /tmp
$ mkdir jhdska
$ cd jhdska 
$ echo {} > package.json
$ npm add uuid @types/uuid typescript 

added 3 packages, and audited 4 packages in 676ms

found 0 vulnerabilities
$ cat > test.ts
import { v4 as uuidv4 } from 'uuid'

console.log(uuidv4())
<ctrl+d>
$ tsc test.ts
$ node test.js
b7c77fbb-95d0-4b88-b9ba-89dba2912d5d

@sulemanelahi
Copy link
Author

Yes, you're right I think the issue is within my project structure

$ mkdir project
$ cd project
$ touch tsconfig.json

add following json to tsconfig.json file:

{
  "compilerOptions": {
    "target": "es2017",                       
    "module": "commonjs",                     
    "outDir": "./dist",
    "strict": true,
    "noImplicitAny": true,
		"baseUrl": "./dependencies/nodejs/node_modules",
    "typeRoots": ["./dependencies/nodejs/node_modules/@types"],
    "esModuleInterop": true,
    "inlineSourceMap": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["dependencies", "**/*.spec.ts"]
}

$ mkdir dependencies/nodejs
$ mkdir src/models
$ cd dependencies/nodejs
$ npm init
$ npm i uuid
$ npm i -D @types/uuid typescript
$ cd ..
$ cd ..
$ cd src/models
$ touch index.ts
$ nano index.ts

import { v4 as uuid } from 'uuid'; // inline typescript error (same mentioned in the issue)
console.log(uuid()); 

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

2 participants