gpt4 book ai didi

javascript - NodeJS 包困惑

转载 作者:行者123 更新时间:2023-12-03 01:43:44 25 4
gpt4 key购买 nike

在我的 package.json 文件中,我有:

  "devDependencies": {
"chai": "^4.1.2",
"chai-as-promised": "^7.0.0",
"chai-bignumber": "^2.0.0",
"decimal.js": "^10.0.0",
"ethereumjs-testrpc-sc": "6.1.2",
"ganache-cli": "6.1.0",
"solidity-coverage": "0.4.14",
"truffle": "4.1.3"
}

我创建了一个 NodeJS 脚本文件 getWeb3Version.js:

let web3 = require("web3");
console.log(web3.version);

当我在 npm install 之前运行 node getWeb3Version.js 时,我得到 1.0.0-beta.30

当我在 npm install 之后运行 node getWeb3Version.js 时,我得到 undefined

为了更深入地调查,我将 console.log(web3.version) 更改为 console.log(web3),看来 web3 npm install 之后的 对象是 npm install 之前的 web3 对象的一个​​非常小的子集。

为了更深入地调查,我从项目文件夹的内部和外部调用了 npm list --depth=0 (其中 package.json 文件是位于)。

当我从项目文件夹内部调用 npm list --depth=0 时,我得到:

+-- chai@4.1.2
+-- chai-as-promised@7.1.1
+-- chai-bignumber@2.0.2
+-- decimal.js@10.0.0
+-- ethereumjs-testrpc-sc@6.1.2
+-- ganache-cli@6.1.0
+-- solidity-coverage@0.4.14
`-- truffle@4.1.3

如您所见,web3 甚至不存在(这很明显,因为我没有将其导入到我的 package.json 文件中)。

尽管如此,require("web3") 似乎有效,正如问题开头所提到的。

当我从项目文件夹外部调用 npm list --depth=0 时,我得到:

+-- chai@4.1.2
+-- decimal.js@9.0.1
+-- ethereumjs-testrpc-sc@6.0.7
+-- ganache-cli@6.1.0
+-- log4js@2.5.2
+-- pug@2.0.0-rc.4
+-- solc@0.4.19
+-- solidity-coverage@0.4.14
+-- solidity-parser-sc@0.4.4
+-- sprintf-js@1.1.1
+-- testrpc@0.0.1
+-- truffle@4.1.7
`-- web3@1.0.0-beta.30

后面是一堆以 npm ERR! 开头的行。无关的:.

这里到底发生了什么?

我的 package.json 文件中导入的包之一是否有可能导出我在执行 console.txt 操作时看到的 web3 对象的子集。日志(web3)

最佳答案

is a very small subset of the web3 object is obtained after the npm install

是因为,您安装的模块之一正在使用不同版本的 web3。当您没有执行 npm install 时,web3 包将从全局包中获取,即 web3@1.0.0-beta.30 >。

The key thing to remember here is that that local package overrides the global package.

关于javascript - NodeJS 包困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50733108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com