in node's package.json I would like to reuse a command that I already have in a 'script'.
Here is the practical example
instead of (note the extra -w on the watch script):
"scripts": {
"test" : "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list",
"watch": "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list -w",
}
I would like to have something like
"scripts": {
"test" : "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list",
"watch": "npm run script test" + "-w",
}
which doesn't work (can't do string concats in json), but you should get what I would like
I know that npm scripts support:
- & (parallel execution)
- && (sequencial execution)
so maybe there is another option?
This can be done in npm#2.1.17. You don't specify your OS and the version of npm that you are using, but unless you have done something to update it, you are probably running npm#1.4.28 which does not support the syntax below.
On Linux or OSX you can update npm with sudo npm install -g npm#latest. See https://github.com/npm/npm/wiki/Troubleshooting#try-the-latest-stable-version-of-npm for a guide to updating npm on all platforms.
You should be able to do this by passing an additional argument to your script:
"scripts": {
"test": "mocha --compilers coffee:coffee-script/register --recursive -R list",
"watch": "npm run test -- -w"
}
I verified this using the following, simplified package.json:
{
"scripts": { "a": "ls", "b": "npm run a -- -l" }
}
Output:
$ npm run a
> # a /Users/smikes/src/github/foo
> ls
package.json
$ npm run b
> # b /Users/smikes/src/github/foo
> npm run a -- -l
> # a /Users/smikes/src/github/foo
> ls -l
total 8
-rw-r--r-- 1 smikes staff 55 4 Jan 05:34 package.json
$
Related
Let's assume that we have AWS CodeBuild project which buildspec file contains the following:
"phases": {
"install": {
"runtime-versions": {
"nodejs": 14
}
},
"build": {
"commands": [
"echo Build started on `date`",
"npm ci && npm audit --audit-level=critical",
"node -r esm index.js --env=CLOUDTEST"
]
},
"post_build": {
"commands": [
"echo Build completed on `date`"
]
}
It runs successfully, but if, for example, env value is wrong, node command will fail, but the build still succeeds.
Question: what should I do to make CodeBuild project to fail when node command fails?
The build phase transitions to post_build by default, regardless whether the build succeeds or fails. To override this behaviour, explicitly set the phase's on-failure behaviour:
"build": {
"on-failure": "ABORT",
"commands": ["node BOOM"]
},
The bad node BOOM command causes the execution to fail immediately. Logs tail:
[Container] 2022/04/29 11:21:10 Command did not exit successfully node BOOM exit status 1
[Container] 2022/04/29 11:21:10 Phase complete: BUILD State: FAILED_WITH_ABORT
[Container] 2022/04/29 11:21:10 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: node BOOM. Reason: exit status 1
I have an index_template.html file which creates a timestamp when built then creates a duplicate index.html file in this timestamp.sh file
#!/bin/bash
timestamp="$(date +%s)"
echo Adding cache busting timestamp $timestamp
sed "s/%%timestamp%%/?ts=${timestamp}/g" ./build/index_template.html > ./build/index.html
Here is the body of my index_template.html file
<body class="">
<div id="app"></div>
<script src="./assets/js/client.bundle.js%%timestamp%%"></script>
<script src="./assets/js/vendor.js"></script>
<!-- <script>
setInterval(function() {
window.location.reload();
}, 300000);
</script> -->
</body>
I have commented out the script to refresh the page every 5 minutes and entered that into a few javascript files in my react app so only 3 specific pages will reload but every page is still reloading so I assume the index.html file is still running this script. I have run the commands to rebuild it in my package.json file but it is still not working. Does anyone know why?
Here are the scripts in my package.json file
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"serve": "http-server ./build -c-1",
"watch": "watchify -e ./src/index.js -o ./build/assets/js/client.bundle.js -t [ babelify ] -
t [ envlocalify ]",
"build": "npm run compile && npm run minify && ./timestamp.sh",
"compile": "browserify -e ./src/index.js -o ./build/assets/js/client.bundle.js -t [ babelify
] -t [ envlocalify --envfile .env.prod ]",
"minify": "uglifyjs ./build/assets/js/client.bundle.js -o ./build/assets/js/client.bundle.js
-c -m",
"start": "npm-run-all --parallel watch serve",
"sync": "npm run build && aws s3 sync build/ s3://spark.generaltraffic.co.uk --
profile=genta-s3 --acl public-read --exclude 'node_modules/*|.env'"
},
I am trying to use webpack to analyze my bundles, but I am unable to do so. Get the following errors. How can I fix this?
The contents of my JSON are as follows. I am posting the errors that am receiving on git
"scripts": {
"build": "webpack --mode production",
"start": "webpack-dev-server --mode development --disable-host-check",
"start:local": "BACKEND=http://localhost:3000 webpack-dev-server --mode development",
"test": "jest",
"lint": "eslint --cache --ext mjs,jsx,js --format codeframe \".*.js\" src test",
"coverage": "nyc --all jest --collectCoverage",
"codecov": "rm -rf .nyc_output/ && yarn coverage && nyc report --reporter=text-lcov && codecov",
"heroku-postbuild": "yarn build"
},
"
The error is pretty clear: webpack requires that you use unix style new lines, but it found windows style new lines.
This error is when compiling index.jsx, so the json is irrelevant.
I have searched all the answers related to this on stackoverflow but none seems to solve my issue. I was following this link to dockerize my nodejs-mysql app:
https://github.com/varunon9/getting-started-docker-mysql-nodejs#launching-nodejs-app-in-a-container
When I come to step-8 and do docker ps the container is not showing. I have checked the logs and it says "Error: Cannot find module 'express'". My run command is:
docker run -t -d \
--publish 7000:7000 \
--link testmysqlservice:db \
--name=test-nodejs-microservice patient-nodejs
Docker Error Log shown here
After step-8 the sql service seems to get stuck or running not sure:
package.json file
{
"name": "paloit",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"arraylist": "^0.1.0",
"avl": "^1.4.4",
"body-parser": "^1.19.0",
"cheerio": "^1.0.0-rc.3",
"ejs": "^3.0.1",
"express": "^4.17.1",
"hashmap": "^2.4.0",
"hashtable": "git+https://github.com/fabrice102/node-hashtable.git",
"jquery": "^3.4.1",
"jsdom": "^15.2.1",
"morgan": "^1.9.1",
"multimap": "^1.1.0",
"mysql": "^2.17.1",
"request": "^2.88.0",
"sort-map": "^1.0.0",
"sorted-map": "^0.1.8",
"yargs": "^15.0.2"
}
}
Docker file for Mysql
Pull the mysql:5.7 image
FROM mysql:5.7
## The maintainer name and email
LABEL gaga maga <gagad#gmail.com>
# database = test and password for root = password
ENV MYSQL_DATABASE=patientdb \
MYSQL_ROOT_PASSWORD=password
# when container will be started, we'll have `test` database created with this schema
COPY ./patientschem_patient.sql /docker-entrypoint-initdb.d/
Dockerfile for Nodejs
# Use Node v8 as the base image.
FROM node:8
# create and set app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 7000
# Run app
CMD ["npm", "start"]
Part of server.js file
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const mysql = require('mysql');
const request = require('request');
const argv = require('yargs').argv;
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs')
app.get('/', function(req, res) {
........
request(url, function(error, response, body, next) {
var illness = JSON.parse(body)
var ill = illness._embedded.illnesses
//var link = _links.self
res.render(....);
});
});
function getConnection(){
return mysql.createConnection(
{
host: 'localhost',
user: 'root',
password: 'password',
database: 'patientschem'
}
)
}
app.post('/displayhospital', (req, res) => {
console.log("trying to get form data");
.......
const queryString = "INSERT INTO patient (e, d, p) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE d = VALUES(dl), p = VALUES(pl)"
});
const server = app.listen(7000, () => {
console.log(`Express running → PORT ${server.address().port}`);
});
I have two ejs files - index and result
Solved:
The problem was in my package.json indeed. I was missing express. Also arraylist was with small 'a' and in my server.js it was require('Arraylist') which created the conflict.
But surprisingly when I was running the app without docker it was running successfully.
Thank u.
I'm looking for a way to minify the white space in template literals. Since regular js minification wouldn't remove white space on a template literal I was expecting that maybe polymer-cli had a way to minify those.
An example of the result of minification:
import{PolymerElement,html}from'../node_modules/#polymer/polymer/polymer-element.js';export default class MyApp extends PolymerElement{static get is(){return'my-app'}static get template(){return html`
<style>
:host {
display: block;
height: 100vh;
}
.app {
height: 100vh;
}
</style>
<div class="app">
My App
</div>
`}}customElements.define(MyApp.is,MyApp);
polymer-cli currently doesn't support minification of tagged template strings. Internally, it uses Babel plugins to minify JavaScript, so ideally we'd be able to insert the babel-plugin-minify-template-strings plugin into the pipeline when minification is enabled.
For now, we could use babel-cli along with that plugin to process the build output of polymer-cli:
Start with a Polymer 3 template project, e.g., PolymerLabs/start-polymer3.
git clone https://github.com/PolymerLabs/start-polymer3.git
cd start-polymer3
Install babel-cli and the babel-plugin-minify-template-strings plugin. Your project may need other Babel plugins. In this case, this template project needs babel-plugin-syntax-dynamic-import for Babel to handle the dynamic imports in the code.
yarn add -D babel-cli \
babel-plugin-minify-template-strings \
babel-plugin-syntax-dynamic-import
Add a .babelrc config file with the following file contents:
{
"compact": true,
"ignore": [
"node_modules"
],
"plugins": [
"minify-template-strings",
"syntax-dynamic-import"
]
}
Add a build NPM script to package.json to run babel-cli (with .babelrc above) on the JavaScript output of polymer build:
"scripts": {
"build": "polymer build && $(npm bin)/babel -d . build/**/src/**/*.js"
}
Run npm run build (or yarn build). The command output (running with polymer-cli (1.7.0-pre.13), zsh, macOS High Sierra) should look similar to this:
➜ start-polymer3 git:(master) ✗ yarn build
yarn run v1.3.2
$ polymer build && $(npm bin)/babel -d . build/**/src/**/*.js
info: [cli.command.build] Clearing build/ directory...
info: [cli.build.build] (es6-unbundled) Building...
info: [cli.build.build] (es6-unbundled) Build complete!
build/es6-unbundled/src/lazy-element.js -> build/es6-unbundled/src/lazy-element.js
build/es6-unbundled/src/start-polymer3.js -> build/es6-unbundled/src/start-polymer3.js
✨ Done in 8.66s.
➜ start-polymer3 git:(master) ✗
See GitHub repo with the above changes, and its sample output
Did you try to setup your polymer.json with the following config?:
"builds": [{
"bundle": true,
"js": {"minify": true},
"css": {"minify": true},
"html": {"minify": true}
}],