How to dockerize or build puppeteer js? - puppeteer

I have a simple puppeteer code that uses some ES6 and is run by the help of Babel.
Currently my Package.json points to
"scripts": {
"dev": "node --experimental-modules --es-module-specifier-resolution=node --no-warnings ./src/index.js"
}
and I wonder if there is a way I can build all files into one final JS or somehow package it instead of having to deal with all the files to run or typing everything allover again.

Related

Gulp "no command gulp found". PATH changed

I got my one website on WordPress and had no code at all to be written myself. And now I decided to move one, write my own website, but stuck with some issues with Gulp after installing ("no command 'gulp' found") The most common reason met for Windows is the wrong PATH, and I've changed it. Any other thoughts? That's supposed to be just a starter kit for an auto page reload when HTML, CSS, JS is changed.
this sounds like you only have gulp installed inside your current project.
First Solution:
Simply install gulp-cli globally. This will allow you to use gulp command
note: gulp still need to be installed per project basis.
npm install -g gulp-cli
Second Solution:
in case you don't want to install gulp-cli globally, you can add it as your build script inside your package.json. And then you can run npm run build from your terminal.
{
"script": {
"build": "gulp"
}
}

cytoscape.js and Angular6

I want to import cytoscape.js library in my angular project, but I don't know how to do it. As far as I know, there is no official cytoscape module for angular, is there any way to import cytoscape manually?
Philipp's answer actually works but it looks like an improper inclusion of a library using both npm install AND an actual JS file. You can skip the npm install step from the instruction and it will work the same.
To actually use npm installed package I advise the following:
Run npm install cytoscape
Run npm install --save #types/cytoscape for proper IDE tips.
In your component add import * as cytoscape from 'cytoscape';
Use it like expected e.g. let cy = cytoscape({...})
UPDATE: Please see the answer of D.C. Joo, this should be the accepted answer.
Actually, it's no problem to import JS libraries in Angular, thats how I do it using npm and Angular6:
Run npm install cytoscape
Download Cytoscape from https://github.com/cytoscape/cytoscape.js/blob/master/dist/cytoscape.min.js
Create a directory scripts in your Angular root directory and place the downloaded file there
Include your downloaded script in your angular.json:
"projects": {
"myproject": {
...
"architect": {
"build": {
...
"scripts": [
"src/scripts/cytoscape.min.js"
]
...
(NOTE: Sorry about the formatting, I tried but it does not want to work)
In the Angular component where you want to use Cytoscape, add declare var cytoscape: any; to the imports
Now you can use Cytoscape like you would normally, have fun :)

Nodejs package.json defining scripts with the same name as a dependency

I've seen a lot of package.json files where there are scripts that doesn't do anything but call a dependency with the same name. For example :
{
"scripts": {
"lint": "npm run tslint \"src/**/*.ts\"",
"tslint": "tslint"
},
"devDependencies": {
"tslint": "~4.4.2",
"tslint-loader": "^3.3.0"
}
}
Here we have the script tslint that just calls the dependency tslint. I guess that is some kind of a way to make the lint script shorter but how would it look like if there was no script called tslint.
I don't believe NPM has this kind of functionality built in. Yarn (the third-party NPM client built by Facebook, Google, Exponent and Tilde) on the other hand, does - you can just use yarn run and it will pick up the executable from your dependencies, even if you don't have a script for it defined in your package.json:
yarn run tslint
yarn run tslint "src/**/*.ts"

node.js noobie trying to follow a tutorial - need to change jade reference to pug

I'm trying to follow this tutorial to learn about node.js:
http://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/
When I run "npm install" some of the messages I see include this:
npm WARN deprecated jade#1.11.0: Jade has been renamed to pug, please install the latest version of pug instead of jade
npm WARN deprecated transformers#2.1.0: Deprecated, use jstransformer
And then it goes ahead and seems to set up the application anyways.
My package.json file currently looks like this:
{
"name": "testapp",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.13.2",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.13.1",
"jade": "~1.11.0",
"morgan": "~1.6.1",
"serve-favicon": "~2.3.0",
"mongodb": "^1.4.4",
"monk": "^1.0.1"
}
}
Questions:
(these questions apply to both packages that I got warned about, but for discussion purposes, I'm just going to pick on jade / pug)
If I wanted to change jade to pug, do i need to specify a version number in this package.json file? Or can I just tell it to get latest somehow?
Also, do I need to blow away my folder structure and then rerun the npm install command? Or can I just edit the package.json file and retry npm install?
Lastly, based on your experience, how critical is it for me to change from jade to pug if i'm just trying to learn how node works? I'm tempted to just leave as is... but then again, if this app works, i know it's going to be rolled out into production..
so... i guess i should make the right decisions up front.
Thanks and sorry if my questions are really remedial.
looks like you have a few questions so I'll go through them in order.
If you want to change jade to pug you can run the following from the command line:
npm uninstall jade --save
then
npm install pug --save
unless you specify the version when installing by saying pug#1.1.0 for example you will get the current version. Here is the documentation for how you can specify versions in your JSON https://docs.npmjs.com/files/package.json but you can specify a specific version or a specify major or minor version. It really depends on what you want to do.
in order to remove modules that are not in your package.json file use the prune command:
npm prune
This should remove modules not listed in your json (as long as they aren't dependencies)
I believe Jade was forced to change their name in npm due to copyright issue. I think it would be a good idea to use the current name so you can stay up to date if there are changes to the package
nb:make sure to change the extension to .pug from .jade
If you use jade/pug files with Node.js/Express, change the template engine of your app to:
app.set('view engine', 'pug')
Also
npm install pug --save
will install the latest version of pug#2.0.0-beta11 as of Mar 2017. This may require some changes in your old .jade files, for example you should simplify:
a(href="/#{link}")
to
a(href=link)
More about Breaking Changes #2305
For future references: Express authors now use express myapp --view=pug in their tutorial here. That way Pug is installed instead of deprecated Jade.

NPM: make package.json work under both Unix (Mac OSX) and Windows

I've gotten some NPM package from a third party who is developing under Mac OSX. Their build can split into either development or production using "scripts" object in package.json. For example:
"scripts": {
"build": "NODE_ENV=dev node make.js --build",
"build-prod": "NODE_ENV=prod node make.js --build",
}
Under Unix, one can run either "npm run build" or "npm run build-prod" to build either directory (naturally, there are some conditional statements in make.js).
Of course, it does not work under Windows - I had to change the commands similar to this:
"scripts": {
"build": "set NODE_ENV=dev&& node make.js --build",
"build-prod": "set NODE_ENV=prod&& node make.js --build",
}
(Please note that it was important not to put a space before the '&&' - otherwise the environment variable was created with an extra white space in it which ruined all those comparisons in make.js).
However, I would like to have some universal source tree which would work under either Unix or Windows without editing. Could you please give some ideas on how to conditionally split the build depending on the OS?
The question is pretty old, but for these who faces the problem nowadays, npm starting from version >=5.1.0 supports setting shell for processing scripts. By default on Windows, npm internally uses cmd.exe for running scripts, even if npm command itself is typed in git-bash. After setting git-bash as shell, scripts that use bash syntax work normally on Windows:
npm config set script-shell "C:\\Program Files\\Git\\bin\\bash.exe"
Here one needs to substitute his correct path to git-bash executable.
I have been thinking for a while, but I doubt there is any aesthetic solution using these tools, to get the desired effect.
If you are able to influence the change in make.js, I would rather change this file to accept prod or dev as argument, example: node make.js --build=dev. With default value, to ensure backwards compatibility.
Using only npm and not modifying make.js, I could think of only running another JavaScript code, which would change environment variable, and then call make.js.
That will look something like:
"build": "node middleman.js"
Middleman.js file could then use child_process or another module to set variable and execute node make.js file.
If you do not want to create an extra file, you can then embed all the JavaScript inside the package.json using:
"build": "node -e 'my code'"
Be warned, that running "node -e 'process.env[\'NODE_ENV\']=\'dev\' && node make.js" will not work, as process.env sets variable in local process, not global (i.e. does not export to the system).
Not the direct solution, but for sake of best practices, make it work different.