web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

Cannot find module 'web3' in node console

Open snifhex opened this issue 7 years ago • 8 comments

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

snifhex avatar Mar 08 '19 08:03 snifhex

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?

nivida avatar Mar 08 '19 13:03 nivida

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.

snifhex avatar Mar 08 '19 13:03 snifhex

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" } }

pabloiarriola avatar Oct 05 '19 00:10 pabloiarriola

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'

pabloiarriola avatar Oct 05 '19 00:10 pabloiarriola

I have the same problem with Create React App:

 yarn add web3
import Web3 from 'web3'
./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'

metamn avatar Oct 23 '19 09:10 metamn

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

SibghatUllah1997 avatar Dec 24 '20 06:12 SibghatUllah1997

I insert "import web3 from 'web3', but I have web3 error

solyasa avatar Apr 26 '22 20:04 solyasa

npm install --save web3

Thank you this worked for me also.

hetref avatar Sep 08 '23 14:09 hetref