Mongo DB driver "Cannot find module 'bson'." and no intellisense - node.js

I'm setting up a server, and want to use Mongo DB as my Data storage but I am not getting any intellisence from Visual Studio Code whatsoever and adding and removing flow. When I "CTRL + Click" MongoClient I get 2 same errors i.e. Cannot find module 'bson'. when I hover over bson
it shows the location to AppData/Local/Microsoft/TypeScript/3.4/node_modules/#types/bson/index
could not find module bson on line :
`import { ObjectID, Timestamp } from 'bson';`
and line"
export { Binary, DBRef, Decimal128, Double, Long, MaxKey, MinKey, ObjectID, ObjectId, Timestamp } from 'bson';
What I've tried is installing mode and various npm packages you'll see from my package.json file and running npm install, npm update and installing node - gyp for global but still the error has persisted
package.json
{
"name": "MadLibs_SERVER",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#types/bson": "^4.0.0",
"#types/mongodb": "^3.1.22",
"#types/node": "^11.13.0",
"bson": "^4.0.2",
"express": "^4.16.4",
"mongodb": "^3.2.3",
"node": "^11.13.0"
}
}
Update 1 :
After re installing all modules i.e express and mongodb and removing all others now I still don't get intellisense but opening the file I am getting more errors Cannot find type definition file for 'node'.
now my package has only mongodb and express but still no fix.

Related

"/" is not recognized as an internal or external command

Today, I'm coding a few npm packages and a few things that need to be prepared repeatedly.
So I wanted to code a CLI to get those things done quickly.
Here is the src/cli.js code:
export function cli(args){
console.log(args);
}
Here is the package.json code:
{
"name": "my-project",
"version": "1.0.0",
"description": "A CLI to bootstrap new project",
"main": "src/index.js",
"bin": {
"#kensoni/my-project": "bin/my-project",
"my-project": "bin/my-project"
},
"publishConfig": {
"access": "public"
},
"keywords": [
"cli"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ken Nguyen",
"license": "MIT",
"dependencies": {
"arg": "^5.0.0",
"esm": "^3.2.25",
"inquirer": "^8.1.1"
}
}
Here is the bin/my-project code:
#!/usr/bin/env/ node
require = require('esm')(module /*, options*/);
require('../src/cli').cli(process.argv);
After I execute the command npm link and open a new cmd type my-project, I get the following message:
'"/"' is not recognized as an internal or external command,
operable program or batch file.
I am using these versions:
node: 14.17.1
npm: 7.18.1
Any ideas how it might work.
Thanks in advance.
Remove "/" after env
#!/usr/bin/env node
//...

Node modules are not exporting

my node modules are not importing. I tried in export default and export const and each and every way. every each times I get either reference error or a syntax error.
I get reference error when i update my code like below.(ReferenceError: require is not defined)
const viewEngine = require( './config/viewEngine');
I get syntax error when i update my code like below.(SyntaxError: The requested module './config/viewEngine' does not provide an export named 'default')
import viewEngine from './config/viewEngine';
I get syntax error when i update my code like below.(SyntaxError: The requested module './config/viewEngine' does not provide an export named 'viewEngine')
import {viewEngine} from './config/viewEngine';
and the ways i tried to export:
module.exports = configViewEngine;
module.exports = {configViewEngine};
and my dotenv is not getting defined. I tried every each ways to import dotenv too.
here is my package.json.
{
"name": "chatbot",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon --experimental-modules --es-module-specifier-resolution=node src/server.js"
},
"type": "module",
"author": "",
"license": "ISC",
"dependencies": {
"#babel/core": "^7.14.3",
"#babel/node": "^7.14.2",
"#babel/preset-env": "^7.14.2",
"body-parser": "^1.19.0",
"browserify": "^17.0.0",
"dotenv": "^10.0.0",
"ejs": "^3.1.6",
"express": "^4.17.1",
"localtunnel": "^2.0.1",
"nodemon": "^2.0.7",
"npm-upgrade": "^3.0.0"
}
}
I use windows 10.
please help thanks!
After upgrading npm version it worked.

Nodejs modularized aws-sdk v3 size getting increased

I am trying to reduce size of nodejs lambda bundle which uses aws-sdk.
This is the original lambda package.json file:
{
"name": "lambdanodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.784.0",
"bluebird": "^3.7.2",
"ioredis": "^4.19.2",
"redis": "^3.0.2",
"redis-clustr": "^1.7.0"
}
}
Overall size is 57MB, 54 belongs to aws-sdk.
To reduce size I tried using specific client services (v3 sdk).
Followed : https://github.com/aws/aws-sdk-js-v3/blob/main/clients/client-secrets-manager/package.json
{
"name": "lambdanodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"#aws-sdk/client-dynamodb": "^3.7.0",
"#aws-sdk/client-secrets-manager": "^3.7.0",
"bluebird": "^3.7.2",
"ioredis": "^4.19.2",
"redis": "^3.0.2",
"redis-clustr": "^1.7.0"
}
}
Now npm install results in even larger size around 190+MBs.
Also in node_modules I see lot of directories which were not there when using previous package.json install.
This v3 aws-sdk is supposed to be lighter.
Am I missing something?
Thanks!
Don't push aws-sdk to the lambda function. If you want to use it in the local development environment install it globally.
Lambda will provide all necessary SDK modules(v2 & v3) during runtime, just import them in the code.
Don't bundle the aws-sdk in the zip.
If your zip size is getting increased, use layers in lambda.
Import the node_moudles to lambda layers.
Then upload your code to lambda.

Unable to run a node.js file with #babel/preset-env

I'm trying to run a simple node.js file which needs the #babel/preset-env preset. The moment I run the js file, I get a message saying
Requires Babel “7.0.0-0” but was loaded with “6.26.3”
To replicate the issue, please try the following in a new folder:
1. Run the following commands
npm init
npm install #babel/register
npm install #babel/core#^7.2.2
npm install #babel/preset-env
Create a .babelrc file with the following
{
"presets": ["#babel/preset-env"],
"plugins": []
}
Create a sample emp.jsx with the following
import React from "react";
class CommentBox extends React.Component {}
Create a parse.js file with the following
require('babel-register')({presets: ['env', 'react']});
let Emp = require('./emp.jsx');
Now run the parse.js file by running
node parse.js
You should see the error mentioned above. I have been trying to fix with for a long time now. Please help.
Many Thanks
followed your instructions and using #babel/register instead with
this package.json run with no issues
tasted on
node : v8.11.2
yarn : 1.12.3
paese.json
require('#babel/register')({});
let Emp = require('./emp.jsx');
console.log(Emp)
packge.json
{
"name": "sof",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.2.2",
"#babel/preset-env": "^7.2.3",
"#babel/register": "^7.0.0"
},
"dependencies": {
"react": "^16.7.0"
}
}
Found the problem. The .babelrc file that contained a reference to #babel/preset-env. Removed it and the js file compiled just fine.

How to start my globally installed node application?

I developed small application.
I did this to install developed module globally:
npm install . -g
I got this response:
$ npm install -g .
update-deps-dev#1.0.0 C:\Users\Vladislav.Sharikov\AppData\Roaming\npm\node_modul
es\update-deps-dev
I got my module in place, where other globally installed modules are stored. Also, my module is available by:
npm ls -g --depth=0
Now, I want to start this module from any place on my pc. How should I do this?
I try to run, but getting this:
Vladislav.Sharikov#PC /D/Dev
$ update-deps-dev
sh: update-deps-dev: command not found
I am using Windows 7 x64 + Git Bash.
How should I start my globally installed node application?
My package.json file contents:
{
"name": "update-deps-dev",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"adm-zip": "^0.4.7",
"fs": "0.0.2",
"https": "^1.0.0",
"q": "^1.4.1",
"shelljs": "^0.7.0"
}
}
Not sure about git bash. But in a cross platform way you can simply define a bin field on your package json to tell npm to generate a binary available on your command line later. It really works fine on windows, mac, linux.
Note that it is a per user binary. If you are working on linux it won t install it into /usr/bin, but depending on your system, more probably ~/node_modules/bin.
Suppose your binary is defined into bin.js file, you just need to add a new Object field bin, and foreach bin you want to define a key, the name of the binary, and its value must point to the path of your bin file relative to the package file:
{
"name": "update-deps-dev",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"update-deps-dev": "./bin.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"adm-zip": "^0.4.7",
"fs": "0.0.2",
"https": "^1.0.0",
"q": "^1.4.1",
"shelljs": "^0.7.0"
}
}
Read also,
https://docs.npmjs.com/files/package.json#bin
https://docs.npmjs.com/cli/bin

Resources