I am working on a tool and I have installed some modules locally through NPM, and I get errors when I try to require these modules through NodeJS. I am working in Windows 10, I have already tried setting up NODE_PATH etc.
Below is the structure of my files:
->project
---->node_modules
---->src
-------->css
-------->js
----------->index.js
---->package.json
---->index.html
I populate the index.html by using index.js etc.
Below is the code of my package.json:
{
"name": "bip39",
"version": "1.0.0",
"description": "A tool for converting BIP39 mnemonic phrases to addresses and private keys.",
"directories": {
"test": "tests"
},
"dependencies": {
"bip39": "^2.6.0",
"bigi": "^1.4.2",
"create-hmac": "^1.1.7",
"nem-sdk": "^1.6.7"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/PavlosTze/bip39.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/PavlosTze/bip39/issues"
},
"homepage": "https://github.com/PavlosTze/bip39#readme"
}
Below is my require() code:
var createHmac = require("create-hmac");
var BigInteger = require("bigi");
const bip39 = require("bip39");
const nem = require("nem-sdk").default;
I have done the following steps on NPM:
1) npm init
2) npm install bip39
3) npm install nem-sdk
4) npm install bigi
5) npm install create-hmac
All these files are inside the node_modules, but still, whenever I run the code in the browser I get an 'Error: Cannot find module "bip39"', etc. for all modules.
EDIT: On another directory that I have cloned the same tool, before I merged it with another one, I get no error and everything works correctly, including the require(). etc. Do you need any of those files as well?
Can someone help me please?
try this :
rm -rf node_modules
npm install
Related
Problem
When trying to build my bundle.js file, I get the following:
Error: Can't walk dependency graph: ENOENT: no such file or directory, lstat 'C:\[filepath&filename]\stream'
required by C:\[filepath]\script.js
My package.json file is as follows:
{
"name": "[projectName]",
"version": "1.0.0",
"description": "",
"main": "script.js",
"scripts": {
"build": "browserify script.js -o bundle.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"browserify": "^17.0.0",
"crypto-js": "^4.1.1",
"stream-consumers": "^1.0.2"
}
}
High level, the code I'm trying to run is as follows:
var CryptoJS = require('crypto-js');
const { json } = require('stream/consumers');
function doSomething() {
}
doSomething();
What I've tried
I've tried a number of different recommended ways to fix this, including:
Adding './' - as recommended here which gives 'Cannot find module'
Deleting package-lock.json file and then installing the packages I wanted to install - as recommended here
This unblocked the issue for me.
It turned out that:
const { json } = require('stream/consumers');
wasn't required, so when I commented it out, it seemed to resolve the issue...
In my Playwright test, I am trying to run a cucumber feature using npm run cucumber --profile dev, however no scenarios are being picked up:
Here is my index.ts file:
const common = `./src/features/**/*.feature \
--require-module ts-node/register \
--require ./src/step-definitions/**/**/*.ts \
-f json:./reports/report.json \
--format progress-bar `;
const dev = `${common} --tags '#dev'`;
const smoke = `${common} --tags '#smoke'`;
const regression = `${common} --tags '#regression'`;
export { dev, smoke, regression }
And here is my folder structure as that may be useful:
Also, my home-page.feature file:
Feature: As a user I expect to be able to navigate to the home page
#dev
#regression
Scenario: As a user I expect to be able to see contacts
Given I am on the 'home' page
Then the 'contacts header' should contain the text 'Contacts'
And my package.json:
{
"name": "personalised-offers-platform-e2e-pw",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"transpile": "rimraf dist && babel --extensions .ts --out-dir dist src",
"cucumber": "npm run transpile && cucumber-js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/cli": "^7.18.9",
"#babel/preset-env": "^7.18.9",
"#babel/preset-typescript": "^7.18.6",
"#cucumber/cucumber": "^8.5.0",
"#playwright/test": "^1.23.4",
"playwright": "^1.23.4",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
}
}
I have tried running just npm run cucumber to see if the tags were the issue, but it gives the same output as above.
Originally, I thought the problem was with the common paths in my index.ts, as the feature wasn't being picked up, but they look OK to me now.
Can someone please explain what I'm doing wrong, & how I can fix it?
I can provide further code to help shed some light on the issue.
Try with "cucumber" option -p or --profile:
npm run transpile && cucumber-js -p
how to fix it ,
I'm doing App chat .
Sorry if the language is difficult to read, I'm Thai.
PS C:\Users\ADMIN\Desktop\chat\server> node server.js
C:\Users\ADMIN\Desktop\chat\server\server.js:4
const { nanoid } = require("nanoid");
^
[ERR_REQUIRE_ESM]: require() of ES Module
C:\Users\ADMIN\Desktop\chat\server\node_modules\nanoid\index.js from
at Object.<anonymous> (C:\Users\ADMIN\Desktop\chat\server\server.js:4:20) {
code: 'ERR_REQUIRE_ESM'
{
"name": "chat",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.1",
"nanoid": "^4.0.0",
"socket.io": "^4.5.1"
}
}
The problem is that you are using nanoid Ver.4.0.0 .
It seems that a new feature in V.4 (support for ESM) is a braking change.
The full documentation is in this link to issue#365 in the nanoid GitHub repo.
The comment that helped me and I base my solution on was from #salyndev0
To fix the problem follow these steps:
Uninstall nanoid: npm uninstall nanoid
Install Version 3 supporting all 3.x.x: npm install nanoid#^3.0.0
This means the library you're trying to require cannot be imported using the require syntax.
I think the version of the nanoid you're using needs to be imported using import
You could try downgrading nanoid or switching to the import keyword.
Trying to populate a mongo db with Employee data with the help of mongoose. When running as: node populate_db.js it throws an error saying
module.js:471
throw err;
^
Error: Cannot find module 'mongodb/node_modules/bson'
not able to check the database value as the database also seems to be empty.
here is my package.json file :
{
"name": "hr",
"version": "1.0.0",
"description": "first node app server",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/pereiraryan/domains.git"
},
"keywords": [
"domains"
],
"author": "ryan pereira",
"license": "ISC",
"bugs": {
"url": "https://github.com/pereiraryan/domains/issues"
},
"homepage": "https://github.com/pereiraryan/domains#readme",
"dependencies": {
"async": "^2.5.0",
"mongoose-post-find": "0.0.2",
"colors": "^1.1.2",
"debug": "0.7.4",
"express": "4.2.0",
"mongoose": "^3.8.11"
} ,
"scripts": {
"populate": "node ./bin/populate_db"
}
}
You can change your mongoose to version 4.2.9 right in you package json file.
Power up your json file in an editor and change the version to 'this'. The type 'npm install' in that directory you have your project folder.
npm install -g node-gyp
cd /to/your/project-folder
rm -rf node_modules
npm install
try doing this
also
do npm install inside node_modules/mongodb.
I have a small node.js app "doto" that I want to npm link, so that I can just call doto anywhere. As of my understanding all I need to do is:
mkdir doto
cd doto
npm init #call the project doto and entry point doto.js
touch doto.js #fill with some code
npm link
node doto.js works just fine, but when I link the package and try to call doto, the command is not found. The linking went fine, I had to use sudo (yes I know I should setup node a way that I do not need sudo, but for now I just want to get my feet wet)
Whenever I install a package globally, I can call it just fine.
I am running mac os 10.10.
doto.js
#!/usr/bin/env node
var path = require('path');
var pkg = require( path.join(__dirname, 'package.json') );
var program = require('commander');
program
.version(pkg.version)
.option('-p, --port <port>', 'Port on which to listen to (defaults to 3000)', parseInt)
.parse(process.argv);
console.log(program.port);
package.json
{
"name": "doto",
"version": "0.0.1",
"description": "",
"main": "doto.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"commander": "~2.7.1"
}
}
What am I missing?
I think your package.json is missing the bin section, according to the docs it should become something like:
{
"name": "doto",
"version": "0.0.1",
"description": "",
"main": "doto.js",
// specify a bin attribute so you could call your module
"bin": {
"doto": "./doto.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"commander": "~2.7.1"
}
}
So after you've run sudo npm link you can run doto from anywhere, if you want to change the name of the executable just change the key under "bin" to whatever you prefer.
I tried npm link and it still was not working from my test package.
My package.json in the linked package had "directories": { "bin": "./bin" } instead of "bin": { "etc": "./etc.js" }.
Once I changed it back to "bin": {...} it started to work in the test package.
So, although this directories: { bin: ... } setting is documented it doesn't seem to work correctly with npm link.