Web3.js: Cannot find module 'web3' in node console

Created on 8 Mar 2019  路  6Comments  路  Source: ChainSafe/web3.js

Description

Error: Cannot find module 'web3' in node console.

Actual behavior

const Web3 = require('web3');

Thrown:
{ Error: Cannot find module 'web3'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:613:15)
at Function.Module._load (internal/modules/cjs/loader.js:539:25)
at Module.require (internal/modules/cjs/loader.js:667:17)
at require (internal/modules/cjs/helpers.js:20:18) code: 'MODULE_NOT_FOUND' }

Steps to reproduce the behavior

mkdir test
cd test
npm init
npm install web3
(also tried npm install ethereum/web3.js )
node
const Web3 = require("web3");

ls outputs node_modules/, package.json
and this node_modules directory has web3.

Versions

more information needed support

Most helpful comment

Thanks for the reply!
But now I got it working I just did npm install --save web3. I don't know why or how but it somehow worked and now I can use web3 on my linux machine.

All 6 comments

Thanks for creating this support question! The described behavior is working in my environment and it looks like a support problem. Could you add a URL to a GitHub repository for having a bit more context?

Thanks for the reply!
But now I got it working I just did npm install --save web3. I don't know why or how but it somehow worked and now I can use web3 on my linux machine.

Im having the same exact problem but cannot fix it using --save web3 just that when I do it in a new folder like you said it does work. But in the project I am working on it doesnt.

This is my package.json

{
"name": "agent",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"dev": "lite-server"
},
"author": "",
"license": "ISC",
"dependencies": {
"solc": "^0.4.24",
"web3": "github:ethereum/web3.js"
},
"devDependencies": {
"lite-server": "^2.3.0"
}
}

And this is the error

Error: Cannot find module '/Users/pablo/Downloads/blockchain-for-healthcare-master/node_modules/web3/packages/web3/src/index.js'. Please verify that the package.json has a valid "main" entry
at tryPackage (internal/modules/cjs/loader.js:292:19)
at Function.Module._findPath (internal/modules/cjs/loader.js:523:18)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:767:27)
at Function.Module._load (internal/modules/cjs/loader.js:685:27)
at Module.require (internal/modules/cjs/loader.js:838:19)
at require (internal/modules/cjs/helpers.js:74:18) {
code: 'MODULE_NOT_FOUND',
path: '/Users/pablo/Downloads/blockchain-for-healthcare-master/node_modules/web3/package.json',
requestPath: 'web3'

I have the same problem with Create React App:

 yarn add web3

```js
import Web3 from 'web3'

```bash
./node_modules/web3/src/index.js
Module not found: Can't resolve '/home/cs/work/argn/node_modules/babel-loader/lib/index.js' in '/home/cs/work/argn'

have the same issue and also try this solution but in vain. still getting error of web3 not defined

Was this page helpful?
0 / 5 - 0 ratings

Related issues

praveengupta0895 picture praveengupta0895  路  3Comments

dhl picture dhl  路  3Comments

FradSer picture FradSer  路  3Comments

TinyWJL picture TinyWJL  路  3Comments

webersson picture webersson  路  3Comments