How to make Node Webkit work with express generator - node.js

im trying to make an App with Node and Express using express generator and packing everything with node-webkit. express generator creates an extremely basic app , but it works running npm start on localhost:30000.
the manifest
{
"name": "app",
"version": "0.0.0",
"private": true,
"node-main": "./app.js",
"main": "http://localhost:8080",
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.15.1",
"bower": "^1.7.9",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"express": "~4.13.4",
"morgan": "~1.7.0",
"pug": "^2.0.0-beta4",
"serve-favicon": "~2.3.0"
}
}
the folder structure
When i run the app with nwjs the window displays that the page can not be loaded, but the console doesn't show any error. I tried changing
"main": "http://localhost:8080" to "main": "http://localhost:3000"
which is how the app runs usually, but it didnt work either.
is this possible?
what am i missing?
thanks in advance

This can be a bit late, but anyway.
Here is the source: https://github.com/nwjs/nw.js/wiki/NW.js-use-express-and-similar-web-frameworks
node-main should have path to file, which sets up the server. In this case I assume it is located in bin directory and is named www. Therefore, the package.json should be like:
{
...
"node-main": "./bin/www",
"main": "http://localhost:3000",
...
}
Also check the port number if it is 3000 or something else.

Related

Error on Heroku: `SyntaxError: Cannot use import statement outside a module`

I encountered a very strange bug when I tried to deploy my node.js backend written in TypeScript to Heroku. The same set of code run perfectly in my local environment (and in all of my teammate's local environment as well) but it failed on Heroku. The Heroku log gives me this error:
(node:4) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
import dotenv from "dotenv";
^^^^^^
SyntaxError: Cannot use import statement outside a module
I looked up other stackoverflow post and they said I need to include "type": "module" in my package.json file in order to enable the import. However, doing that would give me another error:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
Essentially lost the ability to recognize .ts files if I want to have the import statements. I tried many things in different stackoverflow posts but they didn't really work. So I stopped "fixing" my code, since my code run perfectly on local environments. I wonder if this is an issue with Heroku, is there a setting on Heroku that I can change so this code would work like my local environment?
Or is there a better way to fix the import statement issue while using TypeScript?
For your reference, here is my package.json file:
{
"name": "booknbucks-backend",
"version": "1.22.15",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "node app.ts",
"dev": "nodemon app.ts",
"build": "tsc",
"production": "node dist/app.js"
},
"dependencies": {
"#types/dotenv": "^8.2.0",
"#types/express": "^4.17.13",
"#types/mongodb": "^4.0.7",
"bcryptjs": "^2.4.3",
"compression": "^1.7.4",
"cors": "^2.8.5",
"dotenv": "^16.0.0",
"express": "^4.17.2",
"firebase-admin": "^10.0.2",
"helmet": "^5.0.2",
"jsonwebtoken": "^8.5.1",
"moment": "^2.29.1",
"mongoose": "^6.2.0",
"mongoose-paginate-v2": "^1.6.3",
"node-cron": "^3.0.0",
"typescript": "^4.6.3"
}
}
And here is my tsconfig.json file:
{
"compilerOptions": {
"module": "commonjs",
"target": "esnext",
"sourceMap": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"outDir": "dist",
"baseUrl": "."
},
"exclude": ["node_modules"],
"include": ["**/*.ts"]
}
Your build script is running tsc, but your start script still tries to run the uncompiled .ts file instead of the compiled .js file.
Point it to the compiled file instead:
"start": "node dist/app.js",
Note that your Procfile will take precedence over your start script if it exists, so make sure to check there, too.

Error - Can't find Jest - Describe , It ect

I had this problem going on for weeks and today I decided to investigate this matter. What I have noticed is that if I create a brand new project and install #types/jest, jest, supertest and just make a test folder with a dummy test file like dummy.test.js, I'm able to get describe, it ect ect.
when I compare this package.json file with my microservices package.json file, the only difference is that I have few dependencies in my microservices package.json.
here you can see the difference:
new node project
{
"name": "jest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "NODE_ENV=test jest --watchAll --verbose --coverage"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#types/jest": "^27.4.1",
"jest": "^27.5.1",
"supertest": "^6.2.2"
}
}
and this is my microservices package.json file:
{
"name": "auth",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "NODE_ENV=development nodemon src/index.js -watch",
"test": "NODE_ENV=test jest --watchAll --verbose --coverage"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.0.1",
"config": "^3.3.6",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.0.0",
"express": "^4.17.3",
"express-async-errors": "^3.1.1",
"helmet": "^3.21.1",
"http-status-codes": "^2.2.0",
"joi": "^17.6.0",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"moment": "^2.29.2",
"mongoose": "^6.2.10"
},
"devDependencies": {
"#types/jest": "^27.4.1",
"jest": "^27.5.1",
"supertest": "^6.2.2"
}
}
Honestly I'm not understanding what is going on here!
In my new project, I don't need to configure a tsconfig.json for this matter, so I don't see the logic with this problem.
I would appreciate any explanation and maybe with an example to this matter so I can understand why can't see jest properties in my microservices project and in my new project the properties appears without any problems.
After searching what is tsconfig.json and what it does, I finally understood that I had to include it in my microservices project so I can tell the typescript compiler of what file it should be aware of.
Because I have structure all my files in a src folder in the root, I had to tell the compiler to include ["./src/**/*] and exclude["./node_modules"] because if I don't exclude it, we will issue a compiling performance and finally had to make sure to inform the compiler to include #types/jest without being referenced in a source file, in my case: "types": ["jest"] and allowJs to true so my javascript files can be part of my program. This had done the trick.
Now I must figure out why when building a new project, I don't receive these errors without tsconfig.json file. If some knows the answer, I would appreciate it!!

Nodejs Express server Cannot find module 'pdf-fill-form'

I'm really new to NodeJS and ExpressJS.
This is nodeJS RestAPI service.
In my project, I have to populate a PDF form field, To do that I wanted to get help from a nodejs library "pdf-fill-form" link ->https://www.npmjs.com/package/pdf-fill-form
I ran 'npm install pdf-fill-form' on my project folder terminal
Import it as in the example -> const pdfFillForm = require('pdf-fill-form');
But it throws an error
internal/modules/cjs/loader.js:796
throw err;
^
Error: Cannot find module 'pdf-fill-form'
I have tried multiple libraries same issue came 'Cannot find module "Lib Name"'
Can someone who is an expert on Node JS help me?
Probably I have done something wrong, I did search for solutions but could not find any.
My Package json
{
"name": "pemsbackend",
"version": "1.0.0",
"description": "TB",
"main": "index.js",
"scripts": {
"start": "nodemon index.js"
},
"author": "MyTest TB",
"license": "ISC",
"dependencies": {
"#hapi/joi": "^17.1.0",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.9.2"
},
"devDependencies": {
"nodemon": "^2.0.2"
}
}
Thank you.
pdf-fill-form has other dependencies to be installed in you're system before installing it. Go through installing guide mentioned int it's repo and then try installing it.

Unable to start the server

I am trying to use nodemon for auto-starting the server whenever a change is made in the local file, but I get the following error.
(Click image to enlarge)
package.json
{
"name": "myapp",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "if [[ $NODE_ENV == 'production' ]]; then node ./bin/www; else nodemon ./bin/www; fi"
},
"dependencies": {
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.16.0",
"http-errors": "~1.6.2",
"morgan": "~1.9.0",
"pug": "^2.0.3"
},
"devDependencies": {
"nodemon": "^1.18.10"
}
}
It looks like the start script uses Bash scripting syntax, but the screenshot is of a Windows command terminal. The error is because Windows doesn't understand the Bash syntax.
To immediately get you running, you can just change the start script to just nodemon ./bin/www. You'll then want to either figure out the correct syntax, or add another script for the production start script, like "start-prod": "node ./bin/www"' and runnpm run start-prod` in production.

After deploying to Azure Web apps, some modules are not automatically installed

After deploying to Azure Web apps, some modules are not automatically installed.
After deploying, the following error is output.
Error: Can not find module 'Cookie-parser'
At that time, I run 'npm install cookie-parser --save'.
In the package.json, the dependencies of the cookie-parser is surely saved.
{
"name": "solo",
"description": "demo",
"version": "0.0.1",
"private": true,
"license": "MIT",
"author": "lostsupervisor",
"engines": {
"node": ">=6.9.1"
},
"dependencies": {
"body-parser": "^1.17.2",
"cookie-parser": "^1.4.3",
"ejs": "^2.5.6",
"express": "^4.15.4",
"express-session": "^1.15.4",
"mssql": "^4.0.4",
"tedious": "^2.0.0"
},
"scripts": {
"start": "node index.js"
}
}
However, the same event occur after deploying next time.
Could you teach me a solution?
You are using Cookie-parser which is wrong.
In NPM modules while downloading npm upper and lower cases matter. Change it to cookie-parser, upper C is causing the problem.
It was caused by package-lock.json being not being pushed.
I solved it by pushing it.
Thank you very much.

Resources