Nest.js with yarn workspaces (not nest.js monorepo) - node.js

I'm trying to create a yarn workspace monorepo where some of the modules will be nest.js modules. The issue I'm facing is that nest.js monorepo has a particular way of setup that is not compatible with yarn workspaces, see the following example:
packages/
├─ entrypoint/
│ ├─ main.ts <~ here I'll create a nest instance using all other modules
│ ├─ lambda.ts <~ lambda express app for main
│ ├─ local.ts <~ local express app for main
├─ nestjs-module-1/
│ ├─ index.ts <~ here I want to export my module
├─ nestjs-module-2/
│ ├─ index.ts <~ here I want to export my module
├─ not-a-nestjs-module/
│ ├─ index.ts <~ just something else, not nest.js
├─ package.json <~ nest.js monorepo forces me to have this but this module does not exists on yarn workspaces as it's configured to /packges/*

Related

python: Is dot notation deprecated?

Today I got email with feedback from my recruitment task. One of the notices said:
There was usage of dot notation which is deprecated (from .bfs import BFS)
The code that is quoted comes from my init.py module, and the project tree goes like this:
my-app/
├─ main.py/
├─ src/
│ ├─ __init__.py
│ ├─ bfs.py
├─ .gitignore
├─ requirements.txt
├─ README.md
etc...
Question is: Is there a better way to use init.py file? Is "dot notation" really deprecated?

Debug npx environment selection for nested directories?

I have nested directories with different nodejs environments (package.json and node_modules/), and need to run npx under different environments. Something like this:
a/
├─ node_modules/
├─ b/
│ ├─ c/
│ │ ├─ node_modules/
│ │ ├─ package.json
├─ package.json
Recently, I created directory c/, and found that when running a command with npx under c/, it shows some warning related to a/node_modules/. Both environments have this command (package), to avoid confusion.
Thus, I would like to check why npx is unexpectedly looking at the upper levels of the directory structure.
Is there a way to output the relevant information?
For example, how do I check which (environment of) directory that npx is running commands in?

Why do I get this error message: Could not find a declaration file for module 'swagger-jsdoc'

This is my installation results:
success Saved 19 new dependencies.
info Direct dependencies
├─ bcrypt#5.0.1
├─ config#3.3.7
├─ cors#2.8.5
├─ dayjs#1.11.0
├─ dotenv#16.0.0
├─ jsonwebtoken#8.5.1
├─ lodash#4.17.21
├─ mongoose#6.3.0
├─ nanoid#3.3.2
├─ pino-pretty#7.6.1
├─ pino#7.10.0
├─ prom-client#14.0.1
├─ response-time#2.3.2
├─ swagger-jsdoc#6.2.0
├─ swagger-ui-express#4.3.0
└─ zod#3.14.4
info All dependencies
├─ bcrypt#5.0.1
├─ config#3.3.7
├─ cors#2.8.5
├─ dayjs#1.11.0
├─ dotenv#16.0.0
├─ jsonwebtoken#8.5.1
├─ lodash#4.17.21
├─ mongodb-connection-string-url#2.5.2
├─ prom-client#14.0.1
├─ socks#2.6.2
├─ swagger-jsdoc#6.2.0
├─ swagger-ui-express#4.3.0
└─ zod#3.14.4
success Saved 13 new dependencies.
info Direct dependencies
├─ #types/bcrypt#5.0.0
├─ #types/body-parser#1.19.2
├─ #types/config#0.0.41
├─ #types/cors#2.8.12
├─ #types/express#4.17.13
├─ #types/jsonwebtoken#8.5.8
├─ #types/lodash#4.14.181
├─ #types/nanoid#3.0.0
├─ #types/node#17.0.24
├─ #types/pino#7.0.5
├─ #types/response-time#2.3.5
├─ ts-node-dev#1.1.8
└─ typescript#4.6.3
info All dependencies
├─ #types/bcrypt#5.0.0
├─ #types/body-parser#1.19.2
├─ #types/config#0.0.41
├─ #types/cors#2.8.12
├─ #types/express#4.17.13
├─ #types/jsonwebtoken#8.5.8
├─ #types/lodash#4.14.181
├─ #types/nanoid#3.0.0
├─ #types/node#17.0.24
├─ #types/pino#7.0.5
├─ #types/response-time#2.3.5
├─ ts-node-dev#1.1.8
└─ typescript#4.6.3
But I still get these error messages:
Could not find a declaration file for module 'swagger-jsdoc'. 'C:/Users/A/Desktop/desktop/base_code/back_bc_node/node_modules/swagger-jsdoc/index.js' implicitly has an 'any' type.
Try `npm i --save-dev #types/swagger-jsdoc` if it exists or add a new declaration (.d.ts) file containing `declare module 'swagger-jsdoc';`
Could not find a declaration file for module 'swagger-ui-express'. 'C:/Users/A/Desktop/desktop/base_code/back_bc_node/node_modules/swagger-ui-express/index.js' implicitly has an 'any' type.
Try `npm i --save-dev #types/swagger-ui-express` if it exists or add a new declaration (.d.ts) file containing `declare module 'swagger-ui-express';`
And This is how I try to import it:
import swaggerJsdoc from "swagger-jsdoc";
import swaggerUi from "swagger-ui-express";
I don't know why do I get this error message because it was working on my previous application but doesn't work in the new application.I tried to reinstall swagger but didn't work.
I tried following suggested commands within the error message and it worked:
npm i --save-dev #types/swagger-jsdoc
npm i --save-dev #types/swagger-ui-express
But it also created a new file named package-lock.json inside my project that I don't know if is it a good thing or not, because I was using yarn and this was the first npm command I tried. Also I don't know if this new added file package-lock.json was necessary to my project or not?

AWS Lambda layer and local structure

I'm facing a problem on how configuring my local environment to reproduce the behaviour of the prod env.
A simplified view of my prod env :
├── λf A
│ └── layer L
│
├── λf B
│ └── layer L
│
└── λf C
A,B & C are classic node.js lambda functions. A & B share some common dependencies (let's say lodash.js) that I want to group under a lambda layer : L.
For my dev env, I'm using Lerna (but that's not mandatory) to work as a mono repo with this structure :
.
├── packages
│ ├── A
│ │ ├── node_modules
│ │ └── package.json
│ ├── B
│ │ ├── node_modules
│ │ └── package.json
│ ├── C
│ │ ├── node_modules
│ │ └── package.json
│ └── L
│ ├── node_modules
│ └── package.json
├── package.json
└── lerna.json
To ship L, all I have to do is installing it's dependencies and copying the content of node_modules folder into nodejs/node_modules path of my layer.
But what I'm currently unable to do it's making the module A & B to resolve their dependencies both in standard node_modules paths and the L node_modules folder.
Some solutions that are not acceptable :
Duplicating the dependencies
Adding the L dependencies globally in the lerna package.json (because in my real environment I have multiple layers)
Technically, that would be the complex case when you are making environment based on Lambda.
It has separate running environment, so it is difficult to regard same as traditional environment.
But there is easier path that AWS provides.
How about creating your lambda function from Container Image?
Basically, Lambda Layer is "Hidden" file system layer in Lambda Virtual Machine. So it would work very similar to containerized layered package.
If you build your serverless function with container package, you can build your development environment same as production environment.
Reference :
https://docs.aws.amazon.com/lambda/latest/dg/images-create.html
You can follow this out of the box example to start :
https://github.com/jinspark-lab/lambdakit

How to use global/common node_modules instead of local node_modules?

I want to use the global node_modules (C:\Users\<user_name>\AppData\Roaming\npm\node_modules) or common node_modules (D:\node_modules) instead of local node_modules (D:\angular\projects\project1\node_modules).
I want to use the node_modules for various projects.
This is my project structure.
D:\
├── node_modules (common for all projects)
│ ├── Module-N
├── projects
│ ├── project1
│ │ ├── src
│ │ ├── app
│ │ | ├── Module-N (importing from common)
|
│ ├── project2
│ │ ├── src
│ │ ├── app
│ │ | ├── Module-N (importing from common)
|
├── ....
try working with relative path like when you require you own modules for the global models
for example
assuming we want to get a module from the global node modules and the file we are working on is seats on projects/project1/src
we will do require('../../../node_modules/my_global_model')
try looking into tsconfigs paths: https://www.typescriptlang.org/docs/handbook/module-resolution.html

Resources