1

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. ========
Eric
  • 433
  • 5
  • 12
  • `can see the definition for cryto` - try removing it ... crypto is a "built in" module in latest nodejs -see [documentation](https://www.npmjs.com/package/crypto) – Jaromanda X Dec 05 '17 at 23:46
  • Oh that's interesting @JaromandaX. What would someone with an older node version do? – kentor Dec 05 '17 at 23:47
  • do you have an older version? if not, then the question is a cows opinion :p – Jaromanda X Dec 05 '17 at 23:48
  • Should have been more specific, haven't installed the crypto dependency. Just the typescript definitions library for node packages. – Eric Dec 05 '17 at 23:51
  • you said you `can see the definition for cryto in the node_modules folder` ... so, remove that folder – Jaromanda X Dec 05 '17 at 23:51
  • 1
    @Eric are you talking about a typing package from the definetely typed repo? If so what @types package have you installed? I can't find a `@types/crypto` package and if they are part of node.js the typings will be part of `@types/node`. – kentor Dec 05 '17 at 23:56
  • @kentor Yes, that's the package. It is part of that one. – Eric Dec 06 '17 at 00:03
  • @Eric If you haven't installed the NPM package in your workspace (or globally) I don't have a better idea than checking how typescript resolves the crypto module for you. You can pass the `--moduleResolution` flag to the typescript compiler which will then output information how it resolved each package. I recommend you to do it on a new project, because the output is quite long otherwise. Actually it can be a typescript config issue if you are using baseUrl. – kentor Dec 06 '17 at 00:18

2 Answers2

3

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.

kentor
  • 16,553
  • 20
  • 86
  • 144
  • Specifically, the code I'm references lives under /node_modules/@types/node/index.d.ts#5007. It's a typescript interface that allows you to import the modules using typescript's "import" and give you such niceness as autocompletion. `$ npm list | grep crypto │ │ │ ├── crypto-browserify@3.12.0 deduped ├── @types/crypto-js@3.1.37 │ ├── crypto-browserify@3.12.0 deduped ├─┬ crypto-browserify@3.12.0 │ │ │ └── minimalistic-crypto-utils@1.0.1 deduped │ │ └── minimalistic-crypto-utils@1.0.1` – Eric Dec 06 '17 at 00:00
1

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';

255kb - Mockoon
  • 6,657
  • 2
  • 22
  • 29