Jest not found while running JEST in docker container - node.js

I have created below simple Dockerfile:
FROM node:16.7.0
WORKDIR /app
COPY . .
RUN npm install
# ENTRYPOINT [ "npm" ]
CMD ["sh", "-c", "tail -f /dev/null"]
I have added a cmd line with "tail -f /dev/null" to check exactly what's the issue if I issue npm test inside the container.
As soon as I run npm test inside the container --> It throws me below error
# npm test
> docker-jest#1.0.0 test
> jest --verbose
sh: 1: jest: not found
my package.json
{
"name": "docker-jest",
"version": "1.0.0",
"description": "Package for Jest",
"scripts": {
"test": "jest --verbose"
},
"Dependencies": {
"#babel/node": "*",
"#babel/core": "*",
"#babel/preset-env": "*",
"babel-jest": "*",
"jest": "*"
},
"license": "ISC"
}
sum.js
function sum(a, b) {
return a + b;
}
module.exports = sum;
sum.test.js
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Even if I disable CMD and enable ENTRYPOINT and after the build, if I issue:
docker run -it <imagename> test
It throws me the same error, I see the npm install is installing but can't find the jest # /usr/local/lib/node_modules/ as I see the node modules are deployed in location/usr/local/lib/node_modules/ inside the container, and if I issue jest it says jest not found. If I, run the same without the container it works fine. I mean just in the command line after running npm install and then npm run test.
Can anyone assist me with why I'm getting this error and how to fix it?
-----------UPDATE-------------
Found the fix, it was because of my corrupted package-lock file. When I tested in local without the docker, I somehow corrupted the lock file, and later stage when I build and try to run using docker, the corrupted lock file was causing a whole lot of issues. So I deleted it and again ran thru docker...It's working as expected.

I had the same issue and the fix for me was running npm install -g jest (or yarn global add jest).
To add this to your package.json do the following:
"scripts: {
"test": "npm install -g jest && jest --verbose"
},

Related

Build fail in NextJS monorepo — HookWebpackError: processor is not a function

When I try to build one of the NextJS apps in my monorepo, I get the error:
$ rm -r packages/library/node_modules/; rm -r packages/daily/node_modules/; rm -r node_modules/; rm -r packages/shared/node_modules; rm package-lock.json; sudo rm -r packages/daily/.next/; npm i; npm --prefix packages/daily/ run build
> next build
info - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
info - Checking validity of types
info - Creating an optimized production build
Failed to compile.
HookWebpackError: processor is not a function
> Build error occurred
Error: > Build failed because of webpack errors
at /home/ubuntu/cal-frontend/packages/daily/node_modules/next/dist/build/index.js:397:19
at async Span.traceAsyncFn (/home/ubuntu/cal-frontend/packages/daily/node_modules/next/dist/telemetry/trace/trace.js:60:20)
at async Object.build [as default] (/home/ubuntu/cal-frontend/packages/daily/node_modules/next/dist/build/index.js:77:25)
The strange thing is that the node/npm version and code are exactly the same as a few days ago when I last ran this command successfully. I have never had this error before. I even tried cloning to a brand new Linux EC2 instance to see if there was some cached file screwing it up but I got exact same error. I am still able to re-deploy the latest commit on Heroku from a few days ago.
The next dev command works fine still.
The issue started happening when I tried to create a new website under packages/ . But the error is persisting even after I reverted to a commit before I made that code.
The monorepo contains two websites that share a package of shared code inside the repo called shared. Here is the webpack next.config.js file for the package/daily app. Again, no code has changed since the error started.
const withFonts = require('next-fonts');
const withTM = require('next-transpile-modules')(['#my-repo-name/shared']);
require('dotenv').config({ path: '../../.env' });
module.exports = withTM(withFonts({
eslint: {
// Warning: This allows production builds to successfully complete even if
// your project has ESLint errors.
ignoreDuringBuilds: true,
},
webpack(config, options) {
return config;
},
}));
node version: v16.10.0
npm version: 7.24.0
(I also tried building using 16.14 and 8.3, and had the same error)
Also the root level package.json (with personal details redacted):
{
"name": "my-repo-name",
"version": "1.0.0",
"workspaces": {
"packages": [
"packages/*"
]
},
"keywords": [],
"author": "redacted-for-stack-overflow#stackoverflow.org",
"license": "UNLICENSED",
"description": "REDACTED FOR STACK OVERFLOW",
"engines": {
"node": "16.10.0"
},
"cacheDirectories": [
"node_modules",
"packages/library/.next/cache"
],
"scripts": {
"lint": "eslint . --ext js,jsx",
"build": "npm run build --prefix packages/$APP_NAME"
},
"dependencies": {},
"devDependencies": {
"babel-eslint": "^10.1.0",
"eslint": "^7.32.0",
"eslint-config-next": "^11.1.2",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-react-hooks": "^4.2.0"
}
}
There was an incident yesterday which was causing the issue at Vercel. It should be fixed now.
Adding resolutions keys in package.json solved this for me. Hope this should be a temporary issue with vercel.
"resolutions": {
"cssnano-preset-simple": "3.0.0"
}

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

Yarn start script empty after installation

I am new to React-Native and I wanted to install yarn
the command below is the error i received after initiaing yarn start
i also included the version of the npm and nodejs that i installed
PS E:\Native\confusion> node -v
v12.18.1
PS E:\Native\confusion> npm -v
6.14.5
PS E:\Native\confusion> yarn -v
1.22.4
PS E:\Native\confusion> yarn start
yarn run v1.22.4
error Command "start" not found.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
And right here is my package.json file
{
"name": "confusion",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-native-scripts": "2.0.1"
}
}
The problem is, that inside your package.json there is nothing defined for a start command. In order to fix it, you have to create a new json-node like this and fill it with whatever command you want the start script to execute:
{
"name": "confusion",
"version": "0.1.0",
"private": true,
"scripts": {
"start": "COMMAND WHICH SHOULD GET EXECUTED GOES HERE"
}
"devDependencies": {
"react-native-scripts": "2.0.1"
}
}
After you've defined this, you can execute yarn start and it will run your command.

Having an error in my Docker Multi-Stage script

I am trying to create a docker container for my application but there seems to be an error with my Docker script. Whenever I run the command docker build - < Dockerfile, I get the following output:
I'm not entirely sure why this is happening, since my folder layout is the following:
root folder, Docker ---
server ---- package.json
api
tests
In case the folder layout was a bit confusing, I have my docker file inside my root folder and inside the root folder is the folder called server which contains my package.json, my api files and the tests.
Here's my docker script:
# --- Base Node ---
FROM alpine:3.8 AS base
#install node
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/v3.7/main/ nodejs=8.9.3-r1 tini
# set working directory
WORKDIR /usr/src/app
# set tini as entrypoint
ENTRYPOINT ["/sbin/tini", "--"]
# copy project file
COPY . server/package*.json ./
# --- Dependencies ---
FROM base AS dependencies
# install node packages
RUN npm set progress=false && npm config set depth 0
RUN npm install .
# copy production node_modules aside
RUN cp -R node_modules prod_node_modules
# install ALL node_modules, including 'devDependencies'
RUN npm install
#
# ---- Test ----
# run linters, setup and tests
FROM dependencies AS test
COPY . .
RUN npm run lint && npm run test
#
# ---- Release ----
FROM base AS release
# copy production node_modules
COPY --from=dependencies /root/server/prod_node_modules ./node_modules
# copy app sources
COPY server/ ./
# expose port and define CMD
EXPOSE 3003
CMD [ "npm", "start" ]
I've used this question as an example.
Why am I getting this error from my docker script?
The node_modules need was generated by npm install according to parse package.json.
But, not all package.json will make npm install generate this folder, only the one with devDependencies, dependencies could results in the generate of nodes_moduls folder.
I give you a sample package.json which could manage that:
package.json which will generate node_modules:
{
"name": "my-demo",
"version": "1.0.0",
"description": "a project",
"main": "index.js",
"scripts": {
"build": "weex-builder src dist",
"build_plugin": "webpack --config ./tools/webpack.config.plugin.js --color",
"dev": "weex-builder src dist -w",
"serve": "serve -p 8080"
},
"keywords": [
"weex"
],
"author": "xxx#gmail.com",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.14.0"
},
"dependencies": {
"weex-html5": "^0.3.2"
}
}
Above build will ends ok with next:
Step 8/10 : RUN npm install .
---> Running in 181f572843bc
> core-js#2.6.9 postinstall /usr/src/app/node_modules/core-js
> node scripts/postinstall || echo "ignore"
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN my-demo#1.0.0 No repository field.
added 53 packages in 6.491s
Removing intermediate container 181f572843bc
---> 016c98d9650d
Step 9/10 : RUN cp -R node_modules prod_node_modules
---> Running in c24631cc4bc6
Removing intermediate container c24631cc4bc6
---> de413db9140c
But if you remove devDependencies & dependencies like next:
package.json which won't generate node_modules:
{
"name": "my-demo",
"version": "1.0.0",
"description": "a project",
"main": "index.js",
"scripts": {
"build": "weex-builder src dist",
"build_plugin": "webpack --config ./tools/webpack.config.plugin.js --color",
"dev": "weex-builder src dist -w",
"serve": "serve -p 8080"
},
"keywords": [
"weex"
],
"author": "xxx#gmail.com",
"license": "MIT"
}
It will results in:
Step 8/10 : RUN npm install .
---> Running in 45031bd21886
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN my-demo#1.0.0 No repository field.
up to date in 0.115s
Removing intermediate container 45031bd21886
---> f88364d0725d
Step 9/10 : RUN cp -R node_modules prod_node_modules
---> Running in 16cd11546db0
cp: can't stat 'node_modules': No such file or directory
The command '/bin/sh -c cp -R node_modules prod_node_modules' returned a non-zero code: 1

Node.js app doesn't work when I try to host it via GCP deploy command. Error: Cannot find module 'express'

I have my NodeJS app wrote using TypeScript and based on the Express framework. I want to host it in GCP cloud with gcloud app deploy command.
So, first of all, I build my TS sources to JavaScript -is that the correct way of doing it?.
Then from the build (with JS source code) folder I'm trying to run npm start command and it works successfully and I'm also able to check it with Preview:
.
It works well. So far, so good.
Then I run gcloud app deploy from the build folder (with built to JS sources) and I didn't see any errors during deploy.
But afterward, I receive a 500 error on each request whenever I'm trying to reach the deployed app. I've taken a look into a log and I see next error:
Error: Cannot find module 'express'
What seems to be the problem?
I tried the next commands in the build folder:
npm install
npm install express --save
npm install -g express
sudo apt-get install node-express
Nothing works for me.
Here is my package.json file:
{
"name": "full-node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc",
"dev": "node -r ts-node/register ./src/server.ts",
"debug": "ts-node --inspect ./src/server.ts",
"start": "node build/server.js",
"prod": "npm run build && npm run start"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"ts-node": "^7.0.1",
"typescript": "^3.0.1"
},
"dependencies": {
"#types/lodash": "^4.14.116",
"body-parser": "^1.18.3",
"connect": "^3.6.6",
"cors": "^2.8.4",
"crypto": "^1.0.1",
"express": "^4.16.3",
"firebase-admin": "^6.0.0",
"lodash": "^4.17.10"
}
}
Any idea what I'm missed? Is this the correct way to deploy an app wrote with TypeScript to GCP cloud?
app.yaml:
# [START app_yaml]
runtime: nodejs8
# [END app_yaml]
since you are running gcloud app deploy from within the build folder,probably the package.json is not deployed as npm install is run first by app engine there is no way express could be missing.you can go to gcp console and under app engine view the version and then under diagnose you can view the source(the files that were actually deployed to app engine).keep in mind that this is only possible for the standard version and not the flex.I can see from your app.yaml you are using the standard.If some files are missing then go to your app root directory and in your .gcloudignore file you can ignore the files/folders you do not want to deploy.then run gcloud app deploy from within the root directory of your project
The problem was pretty simple. Seems like gcloud app deploy use npm run build & npm run start commands to start application somewhere inside. To host Node.Js wrote on TS first we need to build it to simple JS using tsc command. Then in the build folder rewrite package.json file to use correct commands. Look at my start command: "start": "node build/server.js". I was using it inside build folder as well so that's mean gcloud command was searching in /build/build/ folder. I've changed start command to "start": "node server.js" and then all works well.

Resources