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 'uuid/v4' #238

Closed
DEK4 opened this issue Nov 23, 2017 · 16 comments
Closed

Cannot find module 'uuid/v4' #238

DEK4 opened this issue Nov 23, 2017 · 16 comments

Comments

@DEK4
Copy link

DEK4 commented Nov 23, 2017

Hi,
I install uuid module on my Windows 10 64bit with WebStorm and NodeJS 8.8.1 but when I require the module I get this error:
error: uncaughtException: Cannot find module 'uuid/v4'

Any idea?

Thanks

@anchnk
Copy link

anchnk commented Nov 23, 2017

Hello DEK4, in a shell within your project source folder, could you run npm ls | grep uuid and paste the output of this ?

@DEK4
Copy link
Author

DEK4 commented Nov 23, 2017

image

@anchnk
Copy link

anchnk commented Nov 23, 2017

So great, it's a direct dependency of your project and that's the latest version.
Now, could you execute console.log(require('uuid')); from a test node file ?

@broofa
Copy link
Member

broofa commented Nov 23, 2017

ls <project folder>/node_modules/uuid to confirm uuid is installed locally:

kieffer@MacBook-Pro-3$ ls -l node_modules/uuid/
total 72
-rw-r--r--  1 kieffer  staff   169 Nov 17  2016 AUTHORS
-rw-r--r--  1 kieffer  staff   626 Nov 28  2016 HISTORY.md
-rw-r--r--  1 kieffer  staff  1109 Nov 17  2016 LICENSE.md
-rw-r--r--  1 kieffer  staff  4644 Nov 28  2016 README.md
drwxr-xr-x  3 kieffer  staff   102 Nov 15 09:03 bin
-rw-r--r--  1 kieffer  staff   120 Nov 28  2016 index.js
drwxr-xr-x  5 kieffer  staff   170 Nov 15 09:02 lib
-rw-r--r--  1 kieffer  staff  1775 Nov 15 09:03 package.json
drwxr-xr-x  4 kieffer  staff   136 Nov 15 09:02 test
-rw-r--r--  1 kieffer  staff  3235 Nov 28  2016 v1.js
-rw-r--r--  1 kieffer  staff   679 Nov 28  2016 v4.js

Also, try installing into a new, empty directory. This may be an issue with your project configuration.

kieffer@MacBook-Pro-3$ cd /tmp

kieffer@MacBook-Pro-3$ mkdir work

kieffer@MacBook-Pro-3$ cd work

kieffer@MacBook-Pro-3$ ls -F

kieffer@MacBook-Pro-3$ npm i uuid
npm WARN saveError ENOENT: no such file or directory, open '/private/tmp/work/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/private/tmp/work/package.json'
npm WARN work No description
npm WARN work No repository field.
npm WARN work No README data
npm WARN work No license field.

+ uuid@3.1.0
added 1 package in 1.003s

kieffer@MacBook-Pro-3$ node -e "console.log(require('uuid/v4'))"
[Function: v4]

@dsouzadyn
Copy link

dsouzadyn commented Dec 8, 2017

The global install also doesn't work
I'm using node v8.9.2

@broofa
Copy link
Member

broofa commented Dec 8, 2017

@dsouzadyn Are you trying to require('uuid') when it's installed globally? Node discourages this practice. Any require()ed module should be installed locally. Global installs should be reserved for utilities run from the command line.

If you really want to require the global installed version, set your NODE_PATH property accordingly.

@broofa
Copy link
Member

broofa commented Dec 8, 2017

No response from @anchnk. Closing

@broofa broofa closed this as completed Dec 8, 2017
@corymcdonald
Copy link

Just ran into this issue on my computer, it was because I was using the newer version of the library than my code was written for.

The original code was

const uuidV4 = require('uuid/v4')

Newer versions of this library requires it to be

const { v4: uuidV4 } = require('uuid');

Hope this helps anyone else searching in the future 👍

@corteshvictor
Copy link

The problem with this change is that it has greater weight when required.

const { v4: uuidV4 } = require('uuid');

@TrySound
Copy link
Member

@victorhcortes Not significant in node. Bundlers are able to treeshake unused modules. Though you have to use es modules syntax. Commonjs is not reliable and does not work in a lot of cases.

@broofa
Copy link
Member

broofa commented May 24, 2020

Commonjs tree shaking is not reliable and does not work in a lot of cases.

FTFY. (... to clarify that CommonJS works fine as a whole. It's only when it comes to tree-shaking - unused code removal - that it becomes problematic.)

@victorhcortes: As the person responsible for the previous incarnation of uuid that allowed for deep imports, I'll just chime in to say this was a much-needed change. The deep-import solution avoided the need for tree-shaking but was problematic when it came to support in some packager tools (Angular, specifically) and CDNs.

@corteshvictor
Copy link

corteshvictor commented May 24, 2020

@TrySound By accumulating this cost with other dependencies you will know how heavy it can be.

@broofa I understand the point that was necessary to maintain compatibility and avoid problems.
But if I highlight that the change is much greater.

const uuidV4 = require ('uuid/v4'); //1.9K (gzipped: 818)
const { v4: uuidV4 } = require ('uuid'); //7.9K (gzipped: 3.3K))

@TrySound
Copy link
Member

FTFY

Right, I meant this in tree-shaking context.

By accumulating this cost with other dependencies you will know how heavy it can be.

Yes, I know. This is why the change was considered carefully. There is not cost for bundlers and it's not significant for node as reading from disk even large scripts is fast enough. Memory usage is a problem of script execution not the size.

We have a lot of really bloated libraries like lodash which is used in three forms (lodash.*, lodash/*, require('lodash')), btw sometimes even in the same project.

@broofa
Copy link
Member

broofa commented May 24, 2020

If code size is a primary consideration for people, there are options:

  1. Stick with uuid@3.4 and the deep-import syntax
  2. Bite the bullet and convert to ES6 imports and a packager that does tree-shaking. (Will yield benefits beyond just pruning uuid code!)
  3. Use a "one-liner" implementation such as https://stackoverflow.com/a/2117523/109538. (Not actually recommending this, but if code footprint is paramount... well... 😆)

@temitopealabi
Copy link

@corteshvictor thanks your solutions works for me

@mateus-sartorio
Copy link

Thanks people it solved my problem.

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

9 participants