最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node - require('crypto') resolves to { } - Stack Overflow

programmeradmin0浏览0评论

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 ing 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/appponent.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. ========

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 ing 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.ponent.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. ========
Share Improve this question edited Dec 6, 2017 at 1:11 Eric asked Dec 5, 2017 at 23:38 EricEric 4431 gold badge5 silver badges13 bronze badges 8
  • can see the definition for cryto - try removing it ... crypto is a "built in" module in latest nodejs -see documentation – Jaromanda X Commented Dec 5, 2017 at 23:46
  • Oh that's interesting @JaromandaX. What would someone with an older node version do? – kentor Commented Dec 5, 2017 at 23:47
  • do you have an older version? if not, then the question is a cows opinion :p – Jaromanda X Commented Dec 5, 2017 at 23:48
  • Should have been more specific, haven't installed the crypto dependency. Just the typescript definitions library for node packages. – Eric Commented Dec 5, 2017 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 Commented Dec 5, 2017 at 23:56
 |  Show 3 more ments

2 Answers 2

Reset to default 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./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 ments (and written on the npm page) it has been deprecated as npm package and added to nodejs itself (https://nodejs/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.

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

发布评论

评论列表(0)

  1. 暂无评论