Node can't find Web3 js - node.js

I'm kind of new to Node. So, I installed Web3 like this:
npm install -g web3
Success. Then I run node and try to require web3. It doesn't work:
Welcome to Node.js v12.11.0.
Type ".help" for more information.
> Web3 = require('web3')
Thrown:
Error: Cannot find module 'web3'
Require stack:
- <repl>
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:780:15)
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',
requireStack: [ '<repl>' ]
}
Any ideas, I'm probably missing something obvious.

It looks like the web3 has problems when you globally install it using -g.
I suggest you give npm init to your project if you have not already given it and install it only for your project running:
npm install web3
inside of your project folder.

Related

generating new nestjs module results in Error: Failed to execute command

I'm using MacOS monterey with M1 chip as my operating system. installed NestJS cli using this command: sudo npm install -g #nestjs/cli
when creating new nest project using nest new message everything works fine, but when I try to create new module using this command nest generate module messages I face error.
why does this happen? I tried installing schematics using npm i -g #nestjs/schematics, I don't know if I should've installed it but this didn't help anyway.
The error I face is:
/Users/homayoun/training/messages/node_modules/#angular-devkit/schematics-cli/bin/schematics.js:338
throw new Error(`Unknown argument ${key}. Did you mean ${(0, yargs_parser_1.decamelize)(key)}?`);
^
Error: Unknown argument skipImport. Did you mean skip-import?
at parseArgs (/Users/homayoun/training/messages/node_modules/#angular-devkit/schematics-cli/bin/schematics.js:338:19)
at main (/Users/homayoun/training/messages/node_modules/#angular-devkit/schematics-cli/bin/schematics.js:122:49)
at Object.<anonymous> (/Users/homayoun/training/messages/node_modules/#angular-devkit/schematics-cli/bin/schematics.js:367:5)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
Failed to execute command: node #nestjs/schematics:module --name=messages --no-dry-run --no-skipImport --language="ts" --sourceRoot="src" --spec.
worth noting that I don't face any issues when I do the same thing on ubuntu 20 or 22
Quick solution:
add this to your nest-cli.json
"root": "src"
change into the src of your project folder
cd src
run
nest generate module messages
if the above does not work also try changing the version of packages in package.json to
"#nestjs/cli": "8.2.6",
"#nestjs/schematics": "8.0.11",
removing the node modules in the current project
reinstalling the nest cli with
npm uninstall -g #nestjs/cli
npm i -g #nestjs/cli#8.2.6
this is a bug on the latest version of #nestjs/cli (v8.2.7). Downgrade it to 8.2.6 and it will work (I tested it). It was reported here: https://github.com/nestjs/nest-cli/issues/1693
Also, I don't recommend using the global version of the CLI. Instead, use npx nest or yarn nest.

truffle init node:internal/modules/cjs/loader:936

I wanted to create my own NFT collection so I bought a course called "The Complete NFT Web Development Course - Zero To Expert
". In that course you need to use truffle. I installed all what I sopposed to need but when I try to type truffle init it gives me an error. I used npm install also in this process and it gaves me an error related to react, but I don't think that it's the problem. I have a package.json downloaded from the github repositorie that the teacher have.
Versions:
npm: '8.3.1',
node: '16.14.0'
Windows 10
Error:
truffle init
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module 'C:\Users\Usuario\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js'
[90m at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)[39m
[90m at Function.Module._load (node:internal/modules/cjs/loader:778:27)[39m
[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)[39m
[90m at node:internal/main/run_main_module:17:47[39m {
code: [32m'MODULE_NOT_FOUND'[39m,
requireStack: []
}
Try reinstalling truffle. If you reinstall truffle you will get upgraded version. And that might solve your problem.
Installing truffle version 5.4.29 install -g truffle#5.4.29 in the command prompt and got a message to run npm audit fix --force.
Then inside vscode inside terminal I ran truffle init and got the "sweet " message. I suppose the version matters.

errorr in node:internal/modules/cjs/loader:488

I'm trying to run react app I got this error:
☁ app [master] yarn start
yarn run v1.22.17
$ react-scripts start
node:internal/modules/cjs/loader:488
throw e;
^
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/tokenize' is not defined by "exports" in /Users/li/Documents/reacts/app/node_modules/postcss-safe-parser/node_modules/postcss/package.json
at new NodeError (node:internal/errors:371:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:416:9)
at packageExportsResolve (node:internal/modules/esm/resolve:669:3)
at resolveExports (node:internal/modules/cjs/loader:482:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/li/Documents/reacts/app/node_modules/postcss-safe-parser/lib/safe-parser.js:1:17) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
Node.js v17.0.1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
How do I download the app ?
npx create-react-app my-app
cd my-app
npm start
my stuff version
node -v v17.0.1
yarn -v 1.22.17
npm -v 8.1.4
I've tried every single thing in this but nothing worked for me
I was also getting this error, then I ran npm update and that seemed to clear up this error. But now I'm getting a new error about "no such file or directory, stat '/home//.steampath'"
Maybe that's just a separate issue with mine though, hopefully npm update works for you!
Edit: I was able to fix my "no such directory" error, it was caused by an issue with me trying to transfer a file from a different drive. I was able to resolve it. So unless you also have that same problem, npm update should fix it for you.
I had this issue, as well. I was able to get CRA to work by installing/using Node v16.13.1 via NVM. This is an issue with CRA depending on an old version of postcss-safe-parser referenced here.
First install NVM and then ...
nvm install 16
Make sure node 16 is being used by NVM and yarn start should work.

Trying to access node REPL produces Error: Cannot find module 'esm'

I'm using Kubuntu 20.04.1 and I just installed the current version of node trough nvm (14.8.0), now when trying to access the node REPL by just typing node, I got this error message:
$ node
internal/modules/cjs/loader.js:1083
throw err;
^
Error: Cannot find module 'esm'
Require stack:
- internal/preload
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1080:15)
at Function.Module._load (internal/modules/cjs/loader.js:923:27)
at Module.require (internal/modules/cjs/loader.js:1140:19)
at Module._preloadModules (internal/modules/cjs/loader.js:1397:12)
at loadPreloadModules (internal/bootstrap/pre_execution.js:446:5)
at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:74:3)
at internal/main/repl.js:19:1 {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'internal/preload' ]
}
Any advice about what's going on and how to fix it is welcome
I'm answering my own question in the event someone else faces the same problem: To access the node REPL just install the esm package globally:
npm install -g esm
Or if just require the packae for a project, whitin yout project folder:
npm i esm
For more informatión check this link.

Node module-alias Error: Cannot find module '#src/utils/constants'

I'm running mocha tests and I recently upgraded to the newest version of the ask-cli. I ran the tests again and I'm now receiving this module error. I've npm installed src and utils to no avail. From what I've read it's possible module-alias does not support #src paths.
I'm calling this command:
$ ask api simulate-skill -l en-US -t "start my day" -s amzn1.ask.skill.XXXXXXXX-4156-4ca0-b14e-XXXXXXXXXXXX
Update: this seems to be an issue with the ask-cli which uses
const CONSTANTS = require('#src/utils/constants');
The #src should find the source of the node package but this #src is not used in any other packages I could find. All other packages seem to use ../../ so that's likely while module-alias does not find anything bc it doesn't know how to route this new syntax
Does anyone know of this new #src syntax and if there is a npm module for helping route it?
Which worked for ask-cli 1.1.6 but I upgraded to 1.7.2 for new functionality.
Error: Cannot find module '#src/utils/constants'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._resolveFilename (/Users/calebgates/WebstormProjects/AutomatedUtteranceTesting/node_modules/module-alias/index.js:49:29)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/calebgates/WebstormProjects/AutomatedUtteranceTesting/node_modules/ask-cli/lib/commands/init/index.js:1:81)
at Module._compile (module.js:652:30)
at Module.replacementCompile (/Users/calebgates/WebstormProjects/AutomatedUtteranceTesting/node_modules/nyc/node_modules/append-transform/index.js:58:13)
at module.exports (/Users/calebgates/WebstormProjects/AutomatedUtteranceTesting/node_modules/nyc/node_modules/default-require-extensions/js.js:8:9)
at Object.<anonymous> (/Users/calebgates/WebstormProjects/AutomatedUtteranceTesting/node_modules/nyc/node_modules/append-transform/index.js:62:4)
module.js:549
I solved this by including the ask-sdk in my local project's dependencies.
This is acknowledged bug when ask-cli is installed locally. There are two work-arounds pointed out in the bug report.
Either install ask-cli globally:
$ npm install -g ask-cli
Or install the module-alias package:
$ npm install module-alias
and configure it to look for #src in the ask-cli/lib folder by adding the following to your package.json file:
"_moduleAliases": {
"#src": "./node_modules/ask-cli/lib",
"#root": "./node_modules/ask-cli",
"#test": "./node_modules/ask-cli/test"
}

Resources