Posts

Shrinking node_modules for AWS Lambda (My First “npm shock” and a Practical Fix)

Image
I’m a newbie in Node.js and not really familiar with the whole npm ecosystem. I honestly didn’t expect that a single npm install would explode into ~10MB spread across ~30 different folders. After a few minutes of staring at it, I realized what’s actually inside those folders: not just runtime code. What shocked me Inside node_modules you get everything: README files source files tests examples docs And then the next level of madness: modules inside modules . Duplicated dependencies nested multiple levels deep (e.g. inherits , core-util-is , tedious …), even if they already exist elsewhere in the project tree. Here’s a typical example: I’m in node_modules → package bl , and there’s another node_modules folder inside it, plus test , README and other “junk”… all for a ~10KB JS file. The obvious assumption (that turned out wrong) Once I accepted the chaos, I assumed there must be a “build for production” command that would “compile” depende...

Node.js / npm: Fixing “npm ERR! cb.apply is not a function” on Windows

Image
Lately I ran into this issue and I want to share the exact way I got out of it: C:\>npm i npm -g npm WARN npm npm does not support Node.js v14.17.0 npm WARN  npm  You should probably upgrade to a newer version of node as we npm WARN  npm  can't make any promises that npm will work with this version. npm WARN  npm  Supported releases of Node.js are the latest release of 6, 8, 9, 10, 11. npm WARN  npm  You can find the latest version at https://nodejs.org/ npm ERR! cb.apply is not a function npm ERR! A complete log of this run can be found in: npm ERR!       %USERPROFILE% \AppData\Roaming\npm-cache\_logs\2021-05-30T14_58_58_367Z-debug.log It happened right after I installed a newer version of Node.js. But when I c...

AWS API Gateway + Lambda: How to Add a “Get By ID” Resource (Path Parameter)

Image
This thing is not trivial and I couldn’t find a simple explanation. Here is the exact sequence to add a Get By ID -style path parameter like /users/{userid} in API Gateway and wire it to Lambda. Goal Turn an existing resource like /users into: /users /users/{userid} (new resource) Step-by-step Select an existing resource (e.g. users ). Click Actions and select Create Resource . In Resource Name , put the parameter name in curly braces (e.g. {userid} ). In Resource Path , you should see read-only text like /users/ and a textbox for the parameter. Enter {userid} in the textbox and click Create Resource . You should now see a new branch in your API tree: /{userid} . Select it. Click Actions → Create Method and add ANY (or the specific method you need). ...

Automating DB Backup under SQL Server Express (Without SQL Agent)

SQL Server Express does not include SQL Server Agent. That means: No Jobs engine No Maintenance Plans (Management → Maintenance Plans does not exist) The same applies to many shared SQL Server environments. If you need automated backups, you must implement your own scheduling solution. The Solution Create a batch file and schedule it using Windows Task Scheduler . The script: Accepts database name as parameter Accepts backup folder as parameter Generates timestamped filename Executes BACKUP DATABASE via sqlcmd Batch File Script @echo off set databaseName=%1 echo %databaseName% set backupFolder=%2 echo %backupFolder% for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a" set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%" set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%...

Decimal number issue from server to client. Wow! Didn't expect this @_@

Image
Chapter 17   where a poor Decimal Number came from Server Side to JavaScript I worked on a page for days, adjusting the UI with CSS and HTML. Everything looked fine… until I started testing multi-lingual support. English — OK. Spanish — OK. Chinese — OK. Even right-to-left Hebrew (crooked and askew, but working). But Russian? Nope. In Russian the page stopped working completely: only static texts and images were visible. Nothing dynamic rendered. Of course it was a JavaScript error. Firefox DevTools showed this: That error led me to this JS code: I stared at those lines and saw nothing wrong — no missing variables, no typos, nothing obvious. I went to the original project code to find the exact non-rendered rows and noticed a server variable embedded into the client-side script: Which naturally led me to the server-side C# code responsible for generating that value: Nothing looked wrong there either. So I did what every developer does in ...

Configuring IIS to Allow CORS Requests (Fixing AJAX Calls to an API)

Image
The problem: sending AJAX requests from an HTML5 application running on my local machine to an API hosted under IIS (Google Cloud). The Setup Client: HTML/JS app on local machine Server: API under IIS (Google Cloud) The request looked like this: $.post({ url: apiEndpoint + "Init", contentType: "application/json; charset=utf-8", data: data, success: function (response) { logResponse("API response [Init]", response.d); }, error: function (xhr, status, error) { logResponse('Fail to call init API', data); } }); The error looked like this: Originally, there was crossDomain: true in the AJAX request, but it didn’t help. There were also crossdomain.xml and clientaccesspolicy.xml on the server — also useless for this case. What I Tried (And Why It Didn’t Help) I found many client-side “solutions” for AJAX, but they did not solve the issue....

TRIM Unwanted Characters from String in SQL (Quick Tip)

Small thing. Useful thing. I didn’t know that TRIM in SQL can remove specific characters — not only spaces. Example TRIM ( '.,! ' FROM '# test .' ) What It Does This removes any of the following characters from both sides of the string: Dot . Comma , Exclamation mark ! Space The result: '# test' Why It’s Useful Cleaning imported data Sanitizing user input Preparing text before comparison Normalizing values for indexing Short. Simple. Powerful.