Using webpack bundle analyzer with angular workspace file - json

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

Related

mysql node module give me an error "require is not defined"

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 ;)

how to get turborepo to run the 'deploy' command in every application folder that I have?

I have two applications nested in my /apps folder with the latest version of turborepo on a brand new project created this week.
I have created a 'deploy' command in the package.json of both applications and running "pnpm run deploy" works locally, however, during this step only 1 workspace seems to be detected and work during the github action.
My command in the root package.json:
"deploy": " turbo run deploy --parallel",
turbo.json:
{
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**"]
},
"deploy": {
"cache": false
},
"lint": {
"outputs": []
},
"dev": {
"cache": false
}
}
}
Versions:
"turbo": "1.3.1"
"packageManager": "pnpm#7.4.1"
Locally:
But then in my github action:
I'm running ubuntu latest in my action and windows 10 on my machine.
The build step works in CI for both repositories, so this is a rather odd one.
Is there any way I can force that command to run?
I have also created a github issue in the turborepo repository on github.

npm run build with parcel throws an error

Installed node, ran npm init to create package.json, installed parcel. running the server with npx parcel index.html runs the server. Then I changed "scripts" to "start": "parcel index.html" in package.json, and run npm run start, it also runs the server without a problem. And then I added to "scripts" "build": "parcel build index.html" and run npm run build. But this does not work...
I get the error below...
> vjezba-17#1.0.0 build
> parcel build index.html
× Build failed.
#parcel/namer-default: Target "main" declares an output file path of "index.js" which does not match the compiled bundle type "html".
C:\Users\ijevr\Desktop\JavaScript\vjezba 17\package.json:4:11
3 | "version": "1.0.0",
> 4 | "main": "index.js",
> | ^^^^^^^^^^ Did you mean "index.html"?
5 | "scripts": {
6 | "start": "parcel index.html",
ℹ Try changing the file extension of "main" in package.json.
Of course, changing the main to index.html states that the file should be a .js file... index.js is what the npm init created in the package.json. In my folder, my main js file is named script.js, and it was named like this before I ran npm init. However changing the main to script.js also does not help, I get the same error as stated here...
I don't know what to do to npm build it...
I didn't find a fix, but I found out that with Parcel 2 the build process changed.
Works now.
Script should look like this:
"start": "parcel",
"build": "parcel build"
and main and source like this:
"main": "dist/index.js", //with dist being the path of the build
"source": "src/script.js", //and src the path of our project
To resolve these issues. Kindly remove the "main": "index.js", from the package.json files.
From this package.json::
{
"main": "index.js",
"scripts": {
"start": "parcel index.html",
"build": "parcel build index.html"
}
}
To this package.json::
{
"scripts": {
"start": "parcel index.html",
"build": "parcel build index.html"
}
}
https://parceljs.org/getting-started/migration/#package.json%23main
Remove the main property from package.json completely, or add this to the package.json:
"targets": {
"main": false
},
For me, this worked https://stackoverflow.com/a/71708198/20383654
I added the following as suggested and the build worked.
"targets": { "main":false },

angular2 ng-cli: unable to add twitter-bootstrap to index.html

I recently started a angular2 web app using ng-cli. i'm trying to add Twitter-bootstrap (which I installed with npm) to my index.html file. For some reason it doesn't seem to find bootstrap even though the path is correct. Am I missing something? Is ng-cli moving bootstrap somewhere else?
My code:
<link rel="stylesheet" href="./../node_modules/bootstrap/dist/css/bootstrap.min.css" type="text/stylesheet">
Angular cli does not work the way you are doing. it add third party library like below.
add install library using NPM
npm install bootstrap#next
add your script and style files in apps[0].scripts and apps[0].styles properties of angular-cli.json. like
apps": [
{
"root": "src",
"outDir": "dist",
......
"styles": [
"../node_modules/bootstrap/dist/css/bootstrap.css",
"styles.css"
],
"scripts": [
"../node_modules/bootstrap/dist/js/bootstrap.js",
],
"environments": {
"source": "environments/environment.ts",
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}
],
after adding like this. it will add all the files into index.html

Node NPM unable to require package by name only path

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'