Search code examples
javascriptreactjswebpackgithub-actionscodemirror

Github CD pipeline crashes with codemirror


I am trying to use codemirror with react, The build is successful in local but failing when running the pipeline, Please Help me out. Thanks in Advance.

enter image description here

Error: Module not found: Error: Can't resolve 'codemirror/lib/codemirror.css' in '/home/runner/work/onsquarecode-ui/onsquarecode-ui/src/components/codeEditor'

I guess it's not able to find the global path, any solution regarding that?

CodeEditor.js:

import { useContext, useState } from "react";
import { Controlled as CodeMirror } from "react17-codemirror2";
import { DEFAULT_SETTINGS } from "../../constants";
import { GlobalContext } from "../../context/GlobalProvider";
import "codemirror/lib/codemirror.css";
import "codemirror/theme/material.css";

const CodeEditor = ({ settings, readOnly }) => {
  const { state } = useContext(GlobalContext);
  const [value, setValue] = useState("");

  const handleChange = (editor, data, value) => {
    setValue(value);
  };

  const config = { ...DEFAULT_SETTINGS, ...settings };

  const options = {
    screenReaderLabel: "Code editor",
    lineNumbers: config.lineNumbers,
    firstLineNumber: config.firstLineNumber,
    mode: state || "xml",
    theme: "material",
    scrollbarStyle: null,
    viewportMargin: Infinity,
    lineWrapping: true,
    smartIndent: true,
    extraKeys: {
      "Shift-Tab": "indentLess",
    },
    readOnly,
    showInvisibles: config.hiddenCharacters,
    autoCloseBrackets: true,
  };

  return (
    <>
      <CodeMirror
        value={value}
        onBeforeChange={handleChange}
        autoScroll={false}
        options={options}
      />
    </>
  );
};

export default CodeEditor;

dev-pipeline.yml:

name: Devlopment CI/CD Pipeline
on:
  push:
    branches: [ develop,test ]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [12.x, 14.x, 16.x]
    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
    - name: Install Packages
      run: npm ci
    - name: Lint
      run: npm run lint
    - name: Build
      run: npm run build

package.json :

{
  "name": "onsquarecode-ui",
  "description": "Create and share beautiful images of your source code",
  "version": "0.1.0",
  "private": true,
  "homepage": "https://latticebyte.github.io/onsquarecode-ui",
  "engines": {
    "node": ">=12"
  },
  "dependencies": {
    "@apollo/client": "^3.5.7",
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^12.1.2",
    "@testing-library/user-event": "^13.5.0",
    "crypto-hash": "^2.0.1",
    "dom-to-image-more": "^2.9.5",
    "graphql": "^16.2.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^6.2.1",
    "react-scripts": "5.0.0",
    "react-select": "^5.2.2",
    "react17-codemirror2": "^7.2.3",
    "sass": "^1.48.0",
    "web-vitals": "^2.1.2"
  },
  "scripts": {
    "start": "npm run build:css && react-scripts start",
    "build": "npm run build:css && react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "lint": "eslint src",
    "build:css": "postcss src/styles/tailwind.scss -o src/styles/index.scss",
    "watch:css": "postcss src/styles/tailwind.scss -o src/styles/index.scss -w --verbose",
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    "prepare": "husky install"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "autoprefixer": "^10.4.2",
    "cssnano": "^5.0.15",
    "eslint": "^8.7.0",
    "eslint-config-google": "^0.14.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-import": "^2.25.4",
    "eslint-plugin-jsx-a11y": "^6.5.1",
    "eslint-plugin-react": "^7.28.0",
    "eslint-plugin-react-hooks": "^4.3.0",
    "gh-pages": "^3.2.3",
    "husky": "^7.0.4",
    "lint-staged": "^12.3.1",
    "postcss": "^8.4.5",
    "postcss-cli": "^9.1.0",
    "prettier": "^2.5.1",
    "tailwindcss": "^3.0.14"
  },
  "lint-staged": {
    "*.{js,jsx,ts,tsx}": [
      "npm run lint",
      "eslint --cache --fix"
    ],
    "*.{js,jsx,json,ts,tsx,scss,css,html,md}": "prettier --config .prettierrc --write"
  }
}


Solution

  • You will also need to add codemirror in your dependencies object in package.json because it is a peerDependency of react17-codemirror2.