Why is there an error during build next.js project? - node.js

**my node.js.yml file
**
name: Node.js CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout#v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build
- run: npm run export
- name: Deploy 🚀
uses: JamesIves/github-pages-deploy-action#v4
with:
branch: gh-pages
folder: out # The folder the action should deploy.
enter image description here
error here
link to my repository
https://github.com/EduardKop/web-cv-next
/** #type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
distDir: 'build',
images: {
loader: 'akamai',
path: '',
}
}
module.exports = nextConfig
i wrote next.config.js config. I made a commit, then set up the action as indicated above in the yaml file and got an error

You should give the path of images, in error says that path can't be less that 0 char.
/** #type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
distDir: 'build',
images: {
loader: 'akamai',
path: '',
}
}
module.exports = nextConfig
You should write path in path attribute.

Related

BitBucket Jest tests get stuck

When pipeline gets to the tests (jest). It just stays there and spinner keeps spinning and nothing happens, no error messages. The test run in local with no errors.
After googling i noticed people suggest to use --maxWorkers=20% in jest due to cpu/memory issues. But it didnt help.
Triggering the tests in webpack like this:
"test": "jest --coverage --watchAll --maxWorkers=20% --maxConcurrent=2 --verbose --config=configs/jest.json",
I have this pipeline setup in bitbucket:
image: node:16
pipelines:
default:
- step:
name: Build
caches:
- node
script:
- npm install
- npm run build
- npm run test
artifacts:
- dist/**
- step:
name: Deploy to S3
deployment: production
trigger: manual
script:
- pipe: atlassian/aws-s3-deploy:1.1.0
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION: 'eu-west-1'
S3_BUCKET: 'myname'
LOCAL_PATH: 'dist'
- step:
name: Invalidate CloudFront cache
trigger: automatic
script:
- pipe: atlassian/aws-cloudfront-invalidate:0.6.0
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
DISTRIBUTION_ID: 'E3HTTJFUB2KUCW'
Here is the jest config file if it matters.
{
"rootDir": "..",
"testEnvironment": "jsdom",
"coverageDirectory": "<rootDir>/tests/__coverage__/",
"setupFiles": [
"<rootDir>/tests/__mocks__/shim.js"
],
"roots": [
"<rootDir>/src/",
"<rootDir>/tests/"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/tests/__mocks__/fileMock.js",
"\\.(css|scss|less)$": "<rootDir>/tests/__mocks__/styleMock.js"
},
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx"
],
"transformIgnorePatterns": [
"/node_modules/"
],
"testRegex": "/tests/.*\\.(ts|tsx)$",
"moduleDirectories": [
"node_modules"
],
"globals": {
"DEVELOPMENT": false,
}
}
--watchAll=false should get you out of the menu.

Nodejs actions testing via Github not working, because Dialect needs to be explicitly supplied

So for this project I need to automate my testing. I can run the tests locally without a problem; they all pass. But as soon as I do a pull request, the tests fail in Github actions. I want to make sure the tests also pass on Github actions.
Error on Github actions
I get the following error:
node.js.yml
I use the following workflow to run the tests:
# This workflow will do a clean install of node dependencies, cache/restore them, build
the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Node.js CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
env:
DB_HOST: ${{ secrets.DB_HOST }}
DB_USER: ${{ secrets.DB_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
DB_DATABASE: ${{ secrets.DB_DATABASE }}
DB_PORT: ${{ secrets.DB_PORT }}
DB_DIALECT: ${{ secrets.DB_DIALECT }}
PORT: ${{ secrets.PORT }}
DB_DATABASE_TEST: ${{ secrets.DB_DATABASE_TEST }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
I've had this problem also locally, but I found a way to fix it. On Github however, I'm not sure why this problem is caused. I've added all the values from the .env file in the repository secrets, but it looks like it's not using them, since the test still asks for the dialect.
package.json
I run the following scripts to test the system
"scripts": {
"devStart": "nodemon server.js",
"migrate": "npx sequelize-cli db:migrate",
"migrate:reset": "npx sequelize-cli db:migrate:undo:all && npm run migrate",
"test": "cross-env NODE_ENV=test jest --testTimeout=10000",
"pretest": "cross-env NODE_ENV=test npm run migrate:reset"
},
config.js
This is where I solved the problem locally, but it could also cause a problem on Github actions. Since I need to add or remove the {path: '../.env'} part to get the code to work.
require('dotenv').config(); //When migrating include following in config(): {path: '../.env'}
module.exports = {
development: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT,
},
test: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE_TEST,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT,
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
};
You can find the repository here if you want to see more code: https://github.com/s3-db01/ISAAC-floor-back-end
I use Docker to start my MySQL database(s).
EDIT:
I tried to hard code the dialect, but without result on Github actions. I used the following code to hardcode the dialect:
models/index.js
let sequelize;
if (config.use_env_variable) {
console.log('Using environment variable');
//sequelize = new Sequelize(process.env[config.use_env_variable], config);
sequelize = new Sequelize(process.env[config.use_env_variable], {
host: 'localhost',
dialect: 'mysql'
});
} else {
console.log('im triggered');
//sequelize = new Sequelize(config.database, config.username, config.password, config);
sequelize = new Sequelize(config.database, config.username, config.password, {
host: 'localhost',
dialect: 'mysql'
});
}
Maybe try this? This is the version of Sequelize there own GitHub page
name: Node.js CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: windows-latest
env:
DB_HOST: ${{ secrets.DB_HOST }}
DB_USER: ${{ secrets.DB_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
DB_DATABASE: ${{ secrets.DB_DATABASE }}
DB_PORT: ${{ secrets.DB_PORT }}
DB_DIALECT: mysql
PORT: ${{ secrets.PORT }}
DB_DATABASE_TEST: ${{ secrets.DB_DATABASE_TEST }}
strategy:
fail-fast: false
matrix:
node-version: [12.x, 14.x, 16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm install
- run: npm run build --if-present
- run: docker-compose up -d ${DB_DIALECT}
- run: docker run --link ${DIALECT}:db -e CHECK_PORT=${SEQ_PORT::-1} -e CHECK_HOST=db --net cli_default giorgos/takis
- run: npm test
- run: npm install sequelize#${{ matrix.sequelize-version }}
Not sure if I understand your question completely but I think if you add DIALECT (instead of DB_DIALECT) under the env: in your workflow yml you should be fine.
At least, that's how the sequelize-cli GitHub Action for testing is set up;
https://github.com/sequelize/cli/blob/main/.github/workflows/ci.yml

Jest module not defined

I'm writing my first package and I just converted it to be TypeScript compatible, but this somehow affected my GitHub workflow. When I run my tests using Jest locally, they work just fine. When my tests are run on GitHub, it succeeds for 10.x, but not 12.x or 14.x, giving me the following error:
(node:2397) ExperimentalWarning: The ESM module loader is experimental.
ReferenceError: module is not defined
at file:///home/runner/work/enhancedMathJS/enhancedMathJS/jest.config.js:1:1
at ModuleJob.run (internal/modules/esm/module_job.js:146:37)
at async Loader.import (internal/modules/esm/loader.js:182:24)
at async readConfigFileAndSetRootDir (/home/runner/work/enhancedMathJS/enhancedMathJS/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:126:32)
at async readConfig (/home/runner/work/enhancedMathJS/enhancedMathJS/node_modules/jest-config/build/index.js:217:18)
at async readConfigs (/home/runner/work/enhancedMathJS/enhancedMathJS/node_modules/jest-config/build/index.js:406:26)
at async runCLI (/home/runner/work/enhancedMathJS/enhancedMathJS/node_modules/#jest/core/build/cli/index.js:230:59)
at async Object.run (/home/runner/work/enhancedMathJS/enhancedMathJS/node_modules/jest/node_modules/jest-cli/build/cli/index.js:163:37)
npm ERR! Test failed. See above for more details.
enhancedmath#2.0.0 test /home/runner/work/enhancedMathJS/enhancedMathJS
jest
File jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['<rootDir>/**/__tests__/**/*.spec.ts'],
testPathIgnorePatterns: ['/node_modules/'],
coverageDirectory: './test-reports',
coveragePathIgnorePatterns: ['node_modules', 'src/database', 'src/test', 'src/types'],
reporters: ['default', 'jest-junit'],
globals: { 'ts-jest': { diagnostics: false } },
};
Workflow
name: Node.js CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
I don't understand what the problem is, since everything works locally, but not for every version of Node.js on GitHub.
If you want to check out the files and errors for yourself, you can find my repository here.
I use esm in my project. And config { type: "module" } in file package.json. So just change module.exports = to export default.
I got it working by installing ts-node and updating my jest.config.js file to a jest.config.ts file:
npm i --save-dev ts-node
Jest config
export default {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['<rootDir>/**/__tests__/**/*.spec.ts'],
testPathIgnorePatterns: ['/node_modules/'],
coverageDirectory: './coverage',
coveragePathIgnorePatterns: ['node_modules', 'src/database', 'src/test', 'src/types'],
reporters: ['default', 'jest-junit'],
globals: { 'ts-jest': { diagnostics: false } },
transform: {},
};
I got the very similar error ReferenceError: module is not defined in ES module scope when running Jest in ESM context.
This did the trick for me:
Change
module.exports = {
/* config here */
}
to
export default {
/* config here */
}
in jest.config.js.
Most tutorials and the config scaffolding that comes with Jest make the config a CJS module which will lead to the above error if you use it in ESM context.

Use existing node_modules during serverless deployment using serverless-webpack

I am working on a node application using serverless architecture. We are using serverless-webpack plugin to build the application. Here is the webpack.config.js
module.exports = {
mode: isLocal ? 'development' : 'production',
entry: slsw.lib.entries,
externals: [nodeExternals()],
devtool: 'inline-cheap-module-source-map',
resolve: {
extensions: ['.js', '.jsx', '.json', '.ts', '.tsx']
},
output: {
libraryTarget: 'commonjs2',
path: path.join(__dirname, '.webpack'),
filename: '[name].js'
},
optimization: {
minimize: true,
minimizer: [new TerserPlugin()],
},
target: 'node',
module: {
rules: [{
test: /\.ts$/,
exclude: /node_modules/,
loader: 'ts-loader',
options: {
// disable type checker - we will use it in fork plugin
transpileOnly: true
}
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader',
}
]
},
// plugins: [new ForkTsCheckerWebpackPlugin()]
};
and here is the serverless.yml file :
service: app
provider:
name: aws
region: ${env:AWS_REGION}
stage: ${env:STAGE}
logRetentionInDays: 90
role: ${env:ROLE}
versionFunctions: false
runtime: nodejs12.x
package:
individually: true
plugins:
- serverless-webpack
- serverless-offline
custom:
webpack:
webpackConfig: './webpack-gql.config.js'
packager: 'npm'
includeModules: true
functions:
######## GraphQl ###########
graphql:
role: ${env:ROLE}
runtime: nodejs12.x
timeout: 30
handler: aws/lambda/common/api/graphql.graphqlHandler
events:
- http:
path: graphql
method: post
- http:
path: graphql
method: get
When I am running sls deploy --config serverless.yml to deploy the application , everything is working and the lambda is getting deployed successfully.
Only thing is , during the process of deployment , it is using the package-lock.json and installing the used packages again :
Serverless: Package lock found - Using locked versions
Serverless: Packing external modules: graphql-compose#^7.19.3, linewrap#^0.2.1, d64#^1.0.0, graphql-scalars#^1.2.6, graphql-tools#^6.0.12
Serverless: Packaging service...
My question is how to stop rerunning the npm install again during deployment instead it should use the existing node_moudles while packing the external modules.
Can anybody help me on this. ?

How can I deploy only one specific nodeJS app to one specific environment with PM2?

I have two nodejs app deployed on Digital Ocean, with PM2, which I am learning.
I'd like to know how I could update my apps (services) separately.
At the moment my pm2 config looks like this:
// ecosystem.config.js
module.exports = {
apps: [
{
name: `app1`,
script: './app1.js',
// ...
},
{
name: `app2`,
script: './app2.js',
// ...
}
],
deploy: {
production: {
key: '',
user: 'admin',
host: '',
ref: '',
repo: '',
path: '/home/admin/app/prod',
'post-deploy': './deploy.sh production'
},
development: {
key: '',
user: 'admin',
host: '',
ref: '',
repo: '',
path: '/home/admin/app/development',
'post-deploy': './deploy.sh development'
},
}
}
I can deploy per environment, like pm2 deploy production.
But, can I do something like pm2 deploy development app2 to only update app2?
You have many ways to do that, for me I always create ecosystem file in the client folder and server folder, then create publish ecosystem file in the root folder, like:
client
ecosystem.config.js
server
ecosystem.config.js
ecosystem.config.js
The root ecosystem file like:
deploy: {
frontend: {
...other,
path: '/app/easyv_spaceship',
'post-deploy': 'cd ./client && npm install && npm run build'
},
backend: {
...other,
path: '/app/easyv_spaceship',
'post-deploy': 'cd ./server && npm install && npm run tsc && pm2 startOrRestart ecosystem.config.js --env production'
}
}
hope this can inspire you.

Resources