简体   繁体   中英

Node: How Can I Import Web3 To Node.js?

I'm trying to import the web3 library in Node.js so that I can handle Ethereum smart contract events, however I can't get the import to work.

I'm running Babel on each file to convert to es5, and the import in Node's index.js file looks like this:

import Web3 from 'web3'; 

When I add that "import Web3 from 'web3'" statement I get this error:

webpack:///./node_modules/scrypt/index.js?:3
var scryptNative = __webpack_require__(!(function 
webpackMissingModule() { var e = new Error("Cannot find module 
'./build/Release/scrypt'"); e.code = 'MODULE_NOT_FOUND'; throw e; } . 
()))

^

Error: Cannot find module './build/Release/scrypt'
at webpackMissingModule 
(webpack:///./node_modules/scrypt/index.js?:3:84)
at eval (webpack:///./node_modules/scrypt/index.js?:3:181)
at Object../node_modules/scrypt/index.js 

This is my version of web3: (However, I have tried other versions which also don't work)

"web3": "^1.0.0-beta.26"

Does anyone know how I can import the web3 library to Node? Any help is greatly appreciated.

Name of the module should be quoted

import Web3 from 'web3';

Try using the require for this module:

const web3 = require('web3');

After install web3 using npm install web3, you can import the web3 module using the following line inside your.js file

var web3 = require('web3');

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