I'm trying to use mysql module in a node js project (node 18.12, mysql 2.18.1)
in my project I must use
"type": "module"
in my package.json because the app is constructed with modules. for example i use this imports in the head of my app.js
import { createServer } from 'node:http';
import fs from 'node:fs/promises';
import { createTodo, findTodos } from './functions/storage.js';
import { index,create } from './functions/api/api.js';
then I add the line, like in the doc
var mysql = require('mysql');
but when I launch the app i have this error
mysql node module "require is not defined in ES module scope, you can
use import instead"
I have tried to change type to commonjs but get the same error in the other way, tried to change the extensions of the js file for cjs or mjs . every time same type of error.
How can I use mysql with "native" module node app ?
I'm a beginner in node so, sorry if it is a dumb question, but I can't find any answer by the web.
Here is my package.json
{
"name": "project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"type": "module",
"keywords": [],
"author": "",
"license": "ISC",
"volta": {
"node": "18.12.1"
},
"dependencies": {
"mysql": "^2.18.1"
}
}
Try to remove "type":"module" from package.json.
From How to fix reference error require is not defined in javascript
I finally found the answer here
const require = createRequire(import.meta.url);
var mysql = require('mysql');
I hope this will be useful for other people ;)
Related
So as far as I know I need to ignore the node_modules folder
and create a new database in PHPMyAdmin in Cpanel and place the credentials in the .env file
then upload the zip file to the folder of the subdomain then extract it
export the database from local and import from Cpanel
create a new nodejs application in Cpanel as follow:
run npm install
and when I test it using postman as follows:
Method: POST
URL: {url}/createUser
payload: json
I receive: 503 Service Unavailable
but when I change the URL to local it works
this is my project structure:
and this is my package.json:
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.0",
"express": "^4.18.2",
"jsonwebtoken": "^8.5.1",
"mysql": "^2.18.1"
},
"devDependencies": {
"dotenv": "^16.0.3",
"nodemon": "^2.0.20"
}
}
.env file:
DB_HOST = 127.0.0.1
DB_USER = {{user}} <- private
DB_PASSWORD = {{password}} <- private
DB_DATABASE = {{database}} <- private
DB_PORT = 3306
PORT = 3000
In localhost, I'm using this command to run the API: nodemon dbServer
so what seems to be the problem here?
First Please Remove .env File From Your Project
And Copy and Paste all variable with value on text editor
and Then Click on add Environment Variable in cPanel
After That Fill Each Variable and Value one By One
And Please Also Add this code in package.json file
"scripts": {
"test": "nodemon app.js",
"start": "node app.js"
},
Then Start Your App
Please, Don't Forget To Select Script in Run JS Script and Chose Option Start
Now Your App Will Be Install and Work on That Particular Domain
All I can find for this are solutions that require installing an npm package that will start up an http server for hosting the file. My only requirement however is opening a very simple generated html file from the local computer into the browser via a npm script, no server required, is this doable without a package?
I tried the Daniel's answer, but it does not works to me.
Based on his answer, I found the open-cli package.
npm i -D open-cli
and use it (open-cli) in package.json script object like so
{
"name": "somename",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"open-my-html": "open-cli path/to/your/index.html",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies: {
"open-cli": "^6.0.1"
},
"author": "",
"license": "ISC"
}
then run
npm run open-my-html
Now it works opening the html file on default browser.
Found that I could create a bash script with contents
#!/bin/bash
start chrome "$(realpath "./jest/report.html")"
And then run that using
"test": "jest && bash ./open-browser.sh"
Supposing that your node script and index.html are in the same folder
const open = require('open');
(async () => {
await open('index.html', {"wait": true });
})();
Take a look at this package: https://www.npmjs.com/package/open
the easiest way to do this is to install the open package
npm i open --save-dev
and use it in package.json script object like so
{
"name": "somename",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"open-my-html": "open path/to/your/index.html",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies: {
"open": "^7.3.0"
},
"author": "",
"license": "ISC"
}
then run
npm run open-my-html
{ "start": "start http://localhost:8000 & npm run dev", }
just use above script this works for me. first it will open url in browser and then launch the server. issue is first url will throw error and then after runing sever page wil be auto reload.
This is better if you dont need to use any external package.
I am trying to optimize the ng2-smart-table package, which we are using as a dependency, as I have noticed it has a big unnecessary dependency on lodash, even though it only uses a tiny portion of lodash.
However, I'm running into some issues when trying to get webpack bundle analyzer to work with their angular.json workspace file.
If I just try to run ng build I get this:
❯ ng build --statsJson
Unknown option: '--statsJson'
❯ ng build --stats-json
Unknown option: '--stats-json'
Now, the project uses an angular.json workspace file with the following contents:
{
"$schema": "./node_modules/#angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"ng2-smart-table": {
"projectType": "library",
"root": "projects/ng2-smart-table",
"sourceRoot": "projects/ng2-smart-table/src",
"prefix": "lib",
"architect": {
"build": {
"builder": "#angular-devkit/build-ng-packagr:build",
"options": {
"tsConfig": "projects/ng2-smart-table/tsconfig.lib.json",
"project": "projects/ng2-smart-table/ng-package.json"
}
},
...
If I try to add "statsJson": true to the options I get this error:
❯ ng build ng2-smart-table
Schema validation failed with the following errors:
Data path "" should NOT have additional properties(statsJson).
I suspect it has something to do with the project using #angular-devkit/build-ng-packagr:build rather than the standard #angular-devkit/build-angular:browser. However, since the packagr apparently doesn't support analysis I just don't know how I can get my analysis file generated.
You need to specify project if you are using a workspace such as
ng build myproject --statsJson
Go to angular.json file and check "projects" section.
"projects": {
"template": { <--- here
"projectType": "application",
"schematics": {},
Then run ng build template --statsJson
In your case it's ng build ng2-smart-table --statsJson
I am studying node js and I created a package.json like this:
{
"name": "NodeExpress",
"version": "0.1.0",
"description": "My Simple Express",
"main": "app.js",
"author": {
"name": "My Name",
"email": "name#email.com"
},
"dependencies": {
"express": "*",
"jade": "*",
}
}
When I checked my node_modules it has all the dependencies I need but in my console it's just loading. What does it mean? Is it still downloading?
When I checked the time it is almost 20 minutes past away since I install the dependencies.
Is it fine if I stop the execution of node packager installer?
I created a new Node Package to start sharing a project that I'm working on, but I'm having a bit of trouble getting my require statement to work.
Project: https://github.com/kcjonson/indigo
The issue that I'm having is that requiring my module this way:
var indigo = require('indigo');
Does not work, but requiring it by more explicit path like:
var indigo = require('indigo/lib/indigo');
works just fine.
I assume this is an issue with my package.json file which is as follows:
{
"author": {
"name": "Kevin Jonson",
"email": "kcjonson#gmail.com",
"url": "http://kevinjonson.com"
},
"name": "indigo",
"description": "Node.js Facade for Perceptive Home Automations Indigo home automation servers python REST API",
"version": "0.0.7",
"repository": {
"type": "git",
"url": "git://github.com/kcjonson/indigo.git" },
"directories": {
"lib": "./lib"
},
"main:": "lib/indigo.js",
"license": "MIT",
"private": false
}
I've successfully added it to NPM and running npm install on the project that is using it does download the correct latest version and places it in the node_modules directory as expected.
Any help would be appreciated, thanks in advance.
I am not sure if that is going to work but still worth a try I guess.
Try removing 'directories'