Is `crypto` available in Glitch's version of node?


#1

When I try to access the crypto object within my server.js, I get “ReferenceError: crypto is not defined”.

However, when I open the console, launch node and type > crypto, I see that it is available.

I do see the following note on the node docs for crypto:

It is possible for Node.js to be built without including support for the crypto module. In such cases, calling require(‘crypto’) will result in an error being thrown.

…but that doesn’t explain its availability in the console.

Any thoughts as to what’s going on?


#2

Is there a Node version specified in your package.json? Does the version of Node on the console match the same version of Node in use by your app?


#3

I checked the versions, and tried using the exact version I’m seeing in the console, as well as the latest available version (per nvm). Same result each time.


#4

Hmm, I was able to reproduce the same behavior though calling var crypto = require('crypto') in my server.js resolved it for me. :ghost:


#5

Sure enough! That worked for me as well.

Software!


#6

According to the Node docs, all the built-in modules are automatically imported and assigned to global variables in the REPL.


#7

Also according to Node.js docs, it is possible to build nodejs without crypto. however crypto is included by default. Though, the modules aren’t assign to global variables, what are you talking about? The only global variable I’m aware of is process and console.


#8

Oops, I forgot to mention that that only happens in the REPL. Thanks for pointing that out.