简体   繁体   中英

Same version of node but different module versions

I have 2 typescript / node projects, both using the same version of node. However when I use import * as Crypto from 'crypto'; in each project, they point to two different versions of the crypto module:

在此处输入图像描述 在此处输入图像描述

I'm quite new to node and javascript / typescript and struggling to trying to understand why, hoping someone can enlighten me. My question is: why does import * as Crypto from 'crypto'; resolve to two different versions of crypto even though the projects are setup to use the same versions (I think?)? What determines which version of a built-in module is used? And how can I resolve this so both use the same?

Here's my setup:

===Project 1===

node -v
v14.15.2
npm -v
6.14.9

package.json

{
  "name": "functions",
  "scripts": {
    "lint": "eslint \"src/**/*\"",
    "build": "tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "12"
  },
  "main": "lib/index.js",
  "dependencies": {
    "@google-cloud/firestore": "^4.8.0",
    "@sentry/node": "^5.29.2",
    "@sentry/tracing": "^5.29.2",
    "axios": "^0.21.1",
    "bad-words": "^3.0.4",
    "firebase-admin": "^9.4.2",
    "firebase-functions": "^3.13.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^3.9.1",
    "@typescript-eslint/parser": "^3.8.0",
    "eslint": "^7.6.0",
    "eslint-plugin-import": "^2.22.0",
    "firebase-functions-test": "^0.2.0",
    "typescript": "^3.8.0"
  },
  "private": true
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

===Project 2===

node -v
v14.15.2
npm -v
6.14.9

package.json

{
  "name": "user-approval",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "lint": "eslint \"src/**/*\"",
    "build": "tsc",
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "engines": {
    "node": "12"
  },
  "license": "ISC",
  "dependencies": {
    "@sentry/node": "^5.29.2",
    "@sentry/tracing": "^5.29.2",
    "@slack/bolt": "^2.5.0",
    "@slack/web-api": "^5.14.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^3.9.1",
    "@typescript-eslint/parser": "^3.8.0",
    "eslint": "^7.6.0",
    "eslint-plugin-import": "^2.22.0",
    "typescript": "^3.8.0"
  },
  "private": true
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "sourceMap": true,
    "strict": true,
    "target": "es2017"
  },
  "compileOnSave": true,
}

I suspect the projects have different versions of the @types/node package installed. If you run

npm update @types/node

in each project, it should update each of them to the latest.

I was confused at first because @types/node didn't appear in your package.json files, but it doesn't even if you install it unless you add --save-dev . Even though npm install defaults to --save normally, apparently not for @types packages — which makes sense, they're just a dev dependency, not a runtime dependency.

I'd probably add them as a dev dependency by doing:

npm install @types/node --save-dev

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM