Can I rename the package.json file? - node.js

I'm running Postman tests via Newman using files that are in a Git repository. That works fine. My issue is I have multiple sets of tests, each test has it's own Jenkins job that triggers when a build is successful. I'm trying NOT to use multiple git repositories to host these files.
The tests are run using a package.json file like this:
{
"name": "postmanfiles",
"version": "1.0.0",
"description": "project to store postman collection",
"main": "index.js",
"scripts": { "api-tests": "newman run --verbose --timeout 8000000 --reporters 'cli,testrail' 'SearchAPI.postman_collection.json' -e 'My_Environment.postman_environment.json'" },
"keywords": [ "Postman", "CI" ],
"author": "Joe Smith",
"license": "ISC",
"dependencies": { "newman": "^6.14.10"
}
I have multiple Postman collections, for example, 'SearchAPI.postman_collection.json', 'ModifyAPI.postman_collection.json', etc for each set of tests.
In order to not use multiple Git repos for these tests I need multiple package.json files in one repository because I have five different Postman collections to run. Each Jenkins job triggers off of separate builds. An easy way to do this would be to have five different uniquely named package.json files in one Git repository.
But can I do that and if so how?
Or is there an easier way to do this?

I got this to work by specifying multiple tests inside package.json and then having unique Jenkins scripts in the repository, one for each set of tests.
"scripts": {
"api-tests": "newman run --verbose --timeout 8000000 --reporters 'cli' 'SearchAPI.postman_collection.json' -e 'CTV_Environment.postman_environment.json'",
"site-tests": "newman run --verbose --timeout 8000000 --reporters 'cli' 'SiteAPI.postman_collection.json' -e 'CTV_Environment.postman_environment.json'"
},

As far as I know you can't. Which is why we use tools like Grunt/Gulp or Webpack to get complex build workflows done.

Related

Keep running into the same deployment error (Exec Format Error) when pushing Node.js Docker from local (Apple M1) to Heroku

Error I get when releasing image
2021-04-07T06:30:58.443089+00:00 heroku[web.1]: Starting process with command `node index.js`
2021-04-07T06:31:01.899268+00:00 app[web.1]: Error: Exec format error
I went back to the most simple node.js code that only requires Express.
Can not get my head around it what could be wrong.
Setup is as following:
running Docker Desktop on Mac (Apple M1)
installed latest Heroku, latest NPM, latest Node
working with VS Code
dockerfile setup:
WORKDIR /app
COPY package.json .
RUN npm install
COPY index.js .
CMD ["node", "index.js"]
package.json
{
"name": "SigningV2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
Procfile
web: npm start
added two variables online
- PORT: 3000
- USE_NPM_INSTALL: TRUE (also tried without, same result)
ps: when running heroku local web, it works
Ok, found a working solution. It's Apple M1 that is breaking this standard setup (again) :(
We need to force the correct platform to be compatible by Heroku.
To do this, we are going to use Docker buildx. Note, do not use Heroku:container push because as far as I know, that does not support the forcing of different platforms.
What worked for me is the following sequence:
# replace signingv2 with your own tag
docker buildx build --platform linux/amd64 -t signingv2 .
# make sure to use the name of your Heroku app
docker tag signingv2 registry.heroku.com/signingv2/web
# use docker push to push it to the Heroku registry
docker push registry.heroku.com/signingv2/web
# then use heroku release to activate
heroku container:release web -a signingv2
Hope that Docker for Apple M1 is supported on several platforms soon.
In my case I solved the issue by changing the Dockerfile.
I updated it to pull node image for linux/amd64:
FROM --platform=linux/amd64 node:14.17.0-alpine
Rebuild, push to heroku and it should work fine then.
I modified #Jinxvar 's solution slightly as I had to use the --load option to be able to push the image. Otherwise, it didn't even show up when I ran docker image ls
Here's what worked for me:
$ docker buildx build --load --platform linux/amd64 -t registry.heroku.com/app_name/web .
$ docker push registry.heroku.com/app_name/web:latest
Jinxvar solution works. I had a different issue after
"message": "no pg_hba.conf entry for host "xxx", user "xxx", database "xxx", SSL off"
If your using Postgres for the DB and get the exit error make sure to include this in your pg config.
"ssl": true,
"extra": {
"ssl": { "rejectUnauthorized": false }
},
This route is probably not safe to run in production though. When it is ready for production change the config above to
"SSL": {
"ca": process.env.<your-SSL-cert-var>,
}

Npm workspaces - call workspace script from root package

I'm struggling with multiple npm packages in a root git repository with custom dev scripts to handle launch, compile, build and so on. Now I came across npm workspaces and wanted to use this stunning new feature in my following project structure but I can't get it to work:
projectx (root)
- package.json
- apps
-- backend
-- src
-- package.json (name: #projectx/backend, scripts: "dev":"ts-node or whatever")
-- common
-- src
-- package.json (name: #projectx/common)
-- frontend
-- src
-- package.json (name: #projectx/frontend, scripts: "dev":"webpack")
My root package.json contains:
{
"name": "packagex",
"version": "1.0.0",
"description": "",
"main": "index.js",
"private": "true",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"back:dev": "npm workspace #projectx/backend dev",
"front:dev": "npm workspace #projectx/frontend dev",
"dev": "run-p back:dev front:dev"
},
"workspaces": [
"apps/*"
],
"repository": {
"type": "git",
"url": "git_url"
},
"author": "me",
"license": "ISC",
"devDependencies": {
"npm-run-all": "^4.1.5"
}
}
And now I want to start backend and frontend with npm-run-all and the command on root: npm run dev which results in:
And I also want to share the common package with backend and frontend, which should be possible in this case. Maybe anobody else is facing the same problem or has some ideas what I am doing wrong here.
npm#7.7.0 added a way to call scripts from child packages/workspaces, here are some examples based on your original:
Running a script named "dev" in all workspaces located under apps/backend:
npm run dev -w apps/backend
Running a script named "dev" in all workspaces:
npm run dev --ws
Running a script named "dev" in a package named #projectx/frontend:
npm run dev -w #projectx/frontend
More info:
Related CHANGELOG entry: https://github.com/npm/cli/releases/tag/v7.7.0
Docs: https://docs.npmjs.com/cli/v7/commands/npm-run-script#workspaces-support
Blog post: https://dev.to/ruyadorno/npm-workspaces-npm-run-and-exec-1lg0
Your "workspaces" property in package.json looks right. I'm using NPM Workspaces and it's working well, but it's still missing a lot of features so you need to wire things up yourself. I also don't think npm worksace is a command (but maybe for the future?), so here's a checklist to get it to work:
Make sure you're using Node 15+ and NPM 7+
Set all package.json to "private": true,
Delete all package-lock.json inside of your project, go to the root, then npm install. It should generate one root level package-lock.json that contains all dependencies for your workspaces
Since you're using npm-run-all, add this to your scripts:
"scripts": {
"back:dev": "cd apps/backend && npm run dev",
"front:dev": "cd apps/fontend && npm run dev",
"dev": "npm-run-all build --parallel back:dev front:dev"
}
Then start it with npm run dev.
Note, you may want to consider using start scripts instead of dev to shorten the command you need to type (e.g. npm start instead of npm run dev), but npm run dev will still be fine.
In root package.json you can also add short name for each package:
"scripts": {
"api": "npm --workspace=#app/api run",
}
#app/api is a name in package.json
And run scripts in ./packages/api folder from root like so:
npm run api lint
npm run api dev
I think you wish to:
keep scripts and dependencies separate (thus the 4 package.json files), for ease of maintenance
May I suggest a work-around without workspaces that might do what you're after:
{
...
"scripts": {
"//back:dev": "npm workspace #projectx/backend dev",
"back:dev": "npm --prefix apps/backend dev",
"//front:dev": "npm workspace #projectx/frontend dev",
"front:dev": "npm --prefix apps/frontend dev",
"dev": "run-p back:dev front:dev"
},
"//workspaces": [
"apps/*"
],
"devDependencies": {
"#local/back": "file:apps/backend",
"#local/front": "file:apps/frontend",
"npm-run-all": "^4.1.5"
}
}
The npm --prefix runs npm scripts in another folder than the current one.
The #local/back dependencies are not necessary for that, but I've found such useful if eg. a package depends on another. You might use that trick to reach for the common by:
"dependencies": {
"#local/common": "file:../common"
}
I wished a week ago that workspaces would offer a better solution, but didn't find any benefit over the above mechanisms.
I would also like workspaces to:
only expose those files in the files entry of the particular package.json (now, all are shown)
only allow import to paths in the exports of the particular package.json, if it has one
See
NPM Workspaces monorepo - share local package's distribution folder as root instead of the entire source files

Dist folder is not getting created while building a single-spa root node app

I developed a micro-front end UI app, the application is running on local by npm start command, but now I want to deploy it, for that I need to publish it using build command, when I am trying to run npm build --prod or just npm build, its not throwing any error but I don't see any dist folder created in my app root folder.
Below is my package.json and app structure:
{
"name": "root-html-file",
"description": "The single-spa root config for coexisting-angular-microfrontends",
"main": "index.js",
"scripts": {
"start": "serve -s -l 4200",
},
"author": "Joel Denning",
"license": "MIT",
"devDependencies": {
"serve": "^11.1.0"
},
"dependencies": {}
}
Dist folder is not getting created while building a single-spa root node app
When we build the app on the azure cloud server, Azure devops will checkout the source code from the repo to the build agent. Then the output of the build pipeline will save on this agent instead of our repo.
So, we could not see the dist folder from the repo. It is saved in the local folder on the agent. We need use the copy files and publish build artifact task to publish build artifacts to Azure Pipelines.
Note: The Source Folder in the copy files task should be Agent.BuildDirectory or System.DefaultWorkingDirectory instead of directly selecting the path in the repo through the extension button.
If above not help you, please share you build log, so that we could check if the dist folder created or not.

Is there a way to run yarn test (jest) and directly update interactively?

Here's the current situation:
Whenever we push the code to our build servers, the tests are run on those servers.
When tests fail I have a list of failed tests I need to fix and it would be a timesaver if instead of running yarn test then proceed through the options by choosing "run all tests" and "i update interactively" to just go to that interactive update directly.
I've looked through some of the jest documentation, but couldn't find a clear answer whether it's possible to start it directly. For example by doing something like yarn test -update -interactive (made up parameters as example).
Why don't you use the watch mode
yarn run test --watch
For integration with commit and push you need to create git hooks, you may have a look at husky
{
"husky": {
"hooks": {
"pre-commit": "yarn test -u -t="your test title goes here" --watch",
"pre-push": "yarn test",
"...": "..."
}
}
}

How to keep version DRY for both node.js and docker

I'm building a set of microservices that are run on Docker and orchestrated with Kubernetes. I'm building the services in Node.js and so for each service I have a package.json file that has a version. I'd like to use this version when building my docker images, and to keep the build command simple I'd like to have it be a script in package.json. However, I want to keep the version number DRY (i.e. avoid spreading the version number throughout the package.json file, or having any other copies). For example, if I have the following package.json, how could I reference the version in the script to avoid repeating it?
{
"name": "sample-service",
"version": "1.0.0",
"description": "Sample service",
"dependencies": {
...
},
"scripts": {
"docker": "docker build -t prefix/image-name:$version ."
}
}
Obviously, using $version in the tag there doesn't work so is there another way? Just in case it makes a difference, I'm using yarn rather than npm.
Yes First get the version of package.json
Then export the version to the variable so you can access it as a variable.
Here is your package.json
{
"name": "sample-service",
"version": "1.0.0",
"description": "Sample service",
"dependencies": {
},
"scripts": {
"docker": "export version=$(node -e \"console.log(require('./package.json').version)\") || set version=$(node -e \"console.log(require('./package.json').version)\") ; echo version is: $version; docker build -t prefix/image-name:$version ." }
}
If you run this you will get some thing like

Resources