Eric
Eric

Reputation: 433

Node - require('crypto') resolves to { }

I'm using NPM for dependency management and Angular5 with TypeScript.

EDIT I haven't installed the crypto package from npm, I am referencing the node inbuilt package.

No matter what I do, the "crypto" package resolves as an empty object. I have installed the node @typings package and can see the definition for @typings definition crypto in the node_modules/@typings folder.

What I've tried:

import * as cryp from 'crypto'

declare var crypto:any

declare module crypto

var cryp = require('crypto')

I tried deleting the modules folder and reinstalling.

I tried updating everything to the most recent version.

I tried creating a new project and only using crypto with no additional dependencies. No luck.

The biggest issue here is we are trying to use libraries which depend on this 'crypto' module and they do not function without it.

What's most confusing about this is it doesn't throw any errors, and it's not coming as undefined, but an empty object. It doesn't work in the project or outside of it. And other Node JS/TS modules are working fine, it's literally (so far) just 'crypto' which is having this issue. Any guidance or thoughts here would be great.

Primarily using ng serve to run the application but tried packaging and running using node directly as well.

Per @kendor 's suggestion, I ran a tsc --traceResolution. This is the part regarding crypto. Interestingly it says "node_modules" does not exist, but it clearly does, and other packages are resolving from it correctly.

======== Resolving module 'crypto' from '[...]/src/app/app.component.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
Loading module 'crypto' from 'node_modules' folder, target file type 'TypeScript'.
Directory '[...]/src/app/node_modules' does not exist, skipping all lookups in it.
Directory '[...]/src/node_modules' does not exist, skipping all lookups in it.
File '[...]/node_modules/crypto.ts' does not exist.
File '[...]/node_modules/crypto.tsx' does not exist.
File '[...]/node_modules/crypto.d.ts' does not exist.
File '[...]/node_modules/@types/crypto.d.ts' does not exist.
Directory '[...]/node_modules' does not exist, skipping all lookups in it.
Directory '[...]node_modules' does not exist, skipping all lookups in it.
Directory '/home/node_modules' does not exist, skipping all lookups in it.
Directory '/node_modules' does not exist, skipping all lookups in it.
Loading module 'crypto' from 'node_modules' folder, target file type 'JavaScript'.
Directory '[...]/src/app/node_modules' does not exist, skipping all lookups in it.
Directory '[...]/src/node_modules' does not exist, skipping all lookups in it.
File '[...]/node_modules/crypto.js' does not exist.
File '[...]/node_modules/crypto.jsx' does not exist.
Directory '[...]/node_modules' does not exist, skipping all lookups in it.
Directory '[...]node_modules' does not exist, skipping all lookups in it.
Directory '/home/node_modules' does not exist, skipping all lookups in it.
Directory '/node_modules' does not exist, skipping all lookups in it.
======== Module name 'crypto' was not resolved. ========

Upvotes: 1

Views: 12773

Answers (2)

255kb - Mockoon
255kb - Mockoon

Reputation: 6974

To use crypto NodeJS library with Typescript (Angular >= 2 for example) follow these steps:

  1. npm install @types/node --save-dev to install NodeJS definitions
  2. in tsconfig.ts file add the following:

    "files": [ "./node_modules/@types/node/index.d.ts" ]

  3. Import the library where you want to use it with import * as crypto from 'crypto';

Upvotes: 2

kentor
kentor

Reputation: 18514

In order to answer your question I was trying to find the libraries code. While searching for it I noticed that this library has been deprecated and hence you don't have any luck either most likely. See here: https://www.npmjs.com/package/crypto

If you want to make sure that this is the case just check the node_modules/crypto folder and it's code inside of it. If it's available please share it with us :-).

Edit: As @Jaromanda X pointed out in the comments (and written on the npm page) it has been deprecated as npm package and added to nodejs itself (https://nodejs.org/docs/latest-v7.x/api/crypto.html#crypto_crypto). However they still block it's name to avoid malicious use.

Edit 2: This answer assumed he installed the crypto npm package (on the workspace or globally) which could interfere with the builtin crypto package.

Upvotes: 3

Related Questions