我有 2 个打字稿/节点项目,都使用相同版本的节点。但是,当我在每个项目中使用 import * as Crypto from 'crypto';
时,它们指向加密模块的两个不同版本:
我对 node 和 javascript / typescript 还很陌生,正在努力理解为什么,希望有人能启发我。我的问题是:为什么 import * as Crypto from 'crypto';
解析为两个不同版本的加密,即使项目设置为使用相同的版本(我认为!)?什么决定了使用哪个版本的内置模块?我该如何解决这个问题,以便两者都使用相同的内容?
这是我的设置:
===项目 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"
]
}
===项目 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,
}
答案 0 :(得分:1)
我怀疑这些项目安装了不同版本的 @types/node
软件包。如果你跑
npm update @types/node
在每个项目中,它应该将每个项目更新到最新。
起初我很困惑,因为 @types/node
没有出现在您的 package.json
文件中,但即使您安装它也不会出现,除非您添加 --save-dev
。尽管 npm install
通常默认为 --save
,但显然不适用于 @types
包 - 这是有道理的,它们只是开发依赖项,而不是运行时依赖项。
我可能会通过以下方式将它们添加为开发依赖项:
npm install @types/node --save-dev