Write a custom plugin for a bundler like parcel in a web app - node.js

I have my own nodejs web application which is using Parcel 2 to bundle the resources.
{
"name": "acme-web-app",
"version": "0.0.1",
"description": "",
"keywords": [],
"license": "",
"author": "",
"scripts": {
"build": "parcel build index.html",
"dev": "parcel index.html --open",
"start": "npm run build && npm run dev",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"#parcel/packager-raw-url": "^2.6.0",
"#parcel/transformer-sass": "^2.6.0",
"#parcel/transformer-webmanifest": "^2.6.0",
"parcel": "^2.6.0",
"sass": "^1.52.2"
}
}
All works fine - the js bundles end up in a dist folder.
I am contemplating whether it is possible to write a parcel plug-in within this project is executed when the build script is run.
Is it possible - or does every reference need to go through npm channels via devDependencies?
I haven't figured a starting point for this - and have not been able to find what I am looking for on google so hopefully, the question makes sense.
NOTE: I have only really built web applications with nodejs.

Related

How to run a Node.Js backend project from another developer?

I'm new to Nodejs, normally when I make a project, I install the dependencies myself, and in order to run the project set something like this in the packaje.json:
"scripts": {
"dev": "nodemon server.js"
}
and then I just run my project with something like this npm run dev
But in a project made by someone else it doesn't seem to be that simple because it has the dependencies like this:
{
"name": "proyecto API",
"version": "1.0.0",
"description": "proyecto Main Backend Repo",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "cross-env NODE_ENV=production node --harmony index.js",
"dev": "cross-env NODE_ENV=development nodemon --harmony index.js",
"lint": "eslint --ext .js --ignore-path .gitignore .",
"lint:fix": "eslint --ext .js . --fix",
"superuser": "node --harmony createSuperUser.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/proyecto-Network/lolagato"
},
"author": "gatito",
"license": "MIT",
"bugs": {
"url": "https://github.com/proyecto-Network/lolagato/issues"
},
"homepage": "https://github.com/proyecto-Network/lolagato/erths",
"dependencies": {
"#google-cloud/storage": "^5.14.2",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongo-project": "^1.0.1",
"mongodb": "^4.1.0",
"morgan": "^1.10.0"
},
"devDependencies": {
"cross-env": "^7.0.3",
"nodemon": "^2.0.12"
}
}
Apparently here I can't just do npm run dev because as you can see in the packaje.json it comes out this:
"dev": "cross-env NODE_ENV=development nodemon --harmony index.js"
So my questions are, how can I run this project on my PC and what does that say "cross-env" and --harmony mean?
I have to install all that that appears in the packaje.json?
Just do,
npm install
This will install all the required dependencies. If you notice, in the devDependencies there is cross-env which is required for your npm run dev.
Once the dependencies are brought up, do npm run dev.
Or you could just do -
nodemon index.js
or
nodemon --harmony index.js
To see what happens.
cross-env NODE_ENV=development is just setting the environment variables. From their npm page -
cross-env makes it so you can have a single command without worrying about setting or using the environment variable properly for the platform.

Find unused modules in nodeJS outside package.json

I have to document and resume code from another developer which has been fired because of a lot a disciplinary trouble inside the team.
The application uses nodeJS and mongoDB and I'm a beginner at nodeJS, but webstorm help me a lot to understand how the application works.
(I precise the former dev did not leave me so much documentation, so I'm doing reverse engeeniring and cleaning here).
My question today is:
the node_modules looks like it is really huge to me, with 243 sub-repository. I'm suspecting than some of these are not usefull to the project but the package.json is not really helping here:
{
"name": "my_rotting_project",
"version": "1.0.0",
"description": "",
"main": "main.js",
"bin": "main.js",
"scripts": {
"start": "node --no-deprecation core/server",
"server": "nodemon --no-deprecation core/server",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"luna": "file:./core"
},
"pkg": {
"scripts": "plugins/**/*.js",
"assets": [
"static/**/*",
"core/static/**/*"
]
},
"nodemonConfig": {
"ext": "js,mjs,json,html,css,ejs"
}
}
I have launched npx check and npx npm_check commands but they show me no unused library which seems unlikely (but both of them have written than some of dependencies was missing in package.json)
Does someone know if theses plugins are reliable enough or should I try other methods ? (and what should I do in this case ?)
thank you !

build before restarting the node.js (express) server using Nodemon

I am using nodemon to watch the changes to the server files and restart the server. It works fine when the script is ec5
nodemon ./server.js
but I want to write the script in ec6 and compile using babel doing the same thing but building before the server is restarted.
package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "babel src --out-dir dist",
"serve": "npm run build && node dist/index.js",
"serve-dev": "nodemon dist/index.js"
},
"dependencies": {
"babel-preset-env": "^1.7.0",
"express": "^4.16.4"
},
"devDependencies": {
"#babel/cli": "^7.0.0-rc.1",
"#babel/core": "^7.0.0-rc.1",
"#babel/node": "^7.0.0-rc.1",
"#babel/preset-env": "^7.0.0-rc.1",
"nodemon": "^1.18.3"
}
Run with babel-node. (I can see it's already installed as dev dependency)
"serve-dev": "nodemon --exec babel-node src/index.js"
(assumed that src/index.js is your entry point of your app)
I think nodemon has built-in support for this now.
Create a config file: https://github.com/remy/nodemon#config-files
Watch for the restart event and put your build script stuff there: https://github.com/remy/nodemon#triggering-events-when-nodemon-state-changes

Heroku deployment fails with "missing script: start"

I am trying to upload my local MEAN stack based web application to my Heroku server. I have updated my code in Bitbucket and have successfully done the Heroku deployment part. But, the web app is still not live. When I hit the page, I am getting something like this:
I then checked the logs by firing 'heroku logs' but I am unable to understand what the logs are trying to ask for. I am sharing the logs below:
As I understand it, the application was unable to find "start" but I have later exclusively added "start" : "node app.js" but I still don't see any difference in the heroku logs or in the heroku link.
Can anyone help with what am I doing wrong here?
here's my package.json file
{
"start": "node app.js",
"name": "PerosnalPro",
"version": "1.0.0",
"description": "Personal website using dynamic routing and responsive design.",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Mikey",
"license": "ISC",
"dependencies": {
"dotenv": "^1.2.0",
"ejs": "^2.3.4",
"express": "^4.13.3",
"morgan": "^1.6.1",
"serve-favicon": "^2.3.0",
"slick-carousel": "^1.5.9"
},
"devDependencies": {},
"repository": {
"type": "git",
"url": "git+https://user#bitbucket.org/username/projectname"
},
"homepage": "https://bitbucket.org/username/projectname"
}
"scripts": {
"start" : "node app.js",
"test": "echo \"Error: no test specified \" && exit 1"
}
Add your start tag in scripts. :)

Why devDependencies is not installed

I developed a package for example: testlab, and its package.json is:
{
"devDependencies": {
"mocha": "^2.0.0"
},
"name": "#aab/testlab",
"version": "2.6.0",
"description": "example for npm",
"main": ".\\dest\\main.js",
"dependencies": {
"gulp": "^3.9.1",
"gulp-changed": "^1.3.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"test"
],
"author": "aab <aab#exmaple.com>",
"license": "ISC"
}
Say I included mocha in devDendencies, and then I created a local directory called e.g. c:\example and used
npm install #aab/testlab --only=dev
to get my package under c:\example, but when I looked at c:\example\node_modules, I did not find mocha package is installed. I also tried other command like
npm install #aab/testlab
still no luck. I used NodeJS v4.6.0 and npm 4.0.2. Although nodeJS seems a little old, could any one help me that?
I have seen this happen only when NODE_ENV is set to PRODUCTION. Something else might be setting it.

Resources