Record the pit where Node.js failed to build (/bin/sh: vite: no command found error Command failed with exit code 127.) Author: Chuwen Time: 2023-10-13 Classification: WEB development comment ##Troubleshooting After checking, it is found that the 'vite' package is not installed. Then I think that the 'vite' package is written in the 'devDependencies' list of the' package. json 'file Final view [yarn document]( https://classic.yarnpkg.com/lang/en/docs/cli/install/#toc -Yarn install production true false) found a configuration item: If the environment variable 'NODE_ENV' is set to 'production', the package in the 'devDependencies' list will be ignored Then I took a look at the pipeline environment variable configuration, and the environment variable 'NODE_ENV=production' was set as expected, which also confirmed why the prompt 'vite' command could not be found during the construction ##Solution Choose one of the following options ### 1. Remove NODE_ENV environment variable ### 2. When installing dependencies, append the '-- production=false' parameter Complete example: ` yarn install -- production=false`
Axios Exception Interceptor - Some request custom handling exceptions Author: Chuwen Time: 2023-02-24 Classification: TypeScript comment ##Current situation Current 'request. ts' of the project: ```ts import toast from "./toast" const request = axios.create({ baseURL: " https://api.nowtime.cc ", responseType: "json", timeout: 12_000 }) //Response interceptor request.interceptors.response.use( response => { //TODO response interceptor related operations return response }, error => { //Handling network errors if ( error.code === "ECONNABORTED" || error.message === "Network Error" || error.message.includes("timeout") ) { toast("Network timeout", 5e3, true) return Promise.reject(error) } if (error.response) { //TODO processing API response error const { status } = error.response if (status === 500) { toast("Server error", 5e3) } // ...... } return Promise.reject(error) } ) export default request ``` However, there are some drawbacks in the above code. For example, if there are some requests that need custom exception handling, and 'toast' does not need to pop up, then we need to do so ##Implementation code Add the 'axios. ts' file and paste the following code: ```ts import "axios" declare module "axios" { //Extension of AxiosRequestConfig interface export interface AxiosRequestConfig { catchHandler?: boolean;// Whether to enable automatic exception handling } } ``` `request.ts`: ```ts import toast from "./toast" const request = axios.create({ baseURL: " https://api.nowtime.cc ", responseType: "json", timeout: 12_000 }) //Response interceptor request.interceptors.response.use( response => { //TODO response interceptor related operations return response }, error => { //Handling network errors if ( error.code === "ECONNABORTED" || error.message === "Network Error" || error.message.includes("timeout") ) { toast("Network timeout", 5e3, true) return Promise.reject(error) } //Judge whether the current request config processes the same exception //False: Do not handle the same error if (Object.hasOwn(error.config, "catchHandler") && error.config.catchHandler === false) { return Promise.reject(error) } if (error.response) { //TODO processing API response error const { status } = error.response if (status === 500) { toast("Server error", 5e3) } // ...... } return Promise.reject(error) } ) export default request ``` ###Use: ```ts import request from "./request" rquest.get(`/v2/user`, { catchHandler: true }) ```
Resolve ECharts deprecated parameter: [ECharts] DEPRECATED: itemStyle.normal.lineStyle is deprecated, use lineStyle instead Author: Chuwen Time: 2022-09-21 Classification: WEB development comment ! [ECharts DEPRECATED: itemStyle.normal.lineStyle is deprecated, use lineStyle instead.]( https://cdn.nowtime.cc/2022/09/21/1491597354.png ) ##Original configuration items ```js { series: [ { itemStyle: { normal: { color: "#47C1BF", lineStyle: { Width: 3//Set line thickness } } }, } ] } ``` ##After correction ```js { series: [ { itemStyle: { color: "#47C1BF", }, lineStyle: { Width: 3//Set line thickness }, } ] } ```
Error in reading environment variables by Vite.js Author: Chuwen Time: 2022-09-17 Classification: TypeScript , WEB development comment ##Background When I packaged the file and found that all my environment variables were output in JS during debugging, I was shocked? ! [When I package the file with Vite.js, I find that my environment variables are output in JS during debugging]( https://cdn.nowtime.cc/2022/09/17/511833052.png ) ##Solution Then recall that I used 'process. env' to read environment variables. I remember saying in the official Vite.js document that only environment variables with the prefix 'VITE_' would be exposed. With this doubt, I looked through the official document again: https://cn.vitejs.dev/guide/env-and-mode.html ! []( https://cdn.nowtime.cc/2022/09/17/3888607772.png ) ###A sudden realization It turns out that I remember wrongly/mistakenly. I should use 'import. meta. env' to read environment variables, and then replace them globally immediately` Replace with ` import.meta.env` ###Pack again After packaging and previewing again, it is found that only the environment variables with the prefix 'VITE_' remain ! []( https://cdn.nowtime.cc/2022/09/17/3137341323.png ) ##Optimizable points I found that after packaging, just use 'import. meta. env VITE_ * ` will be packaged like this, so I can see it in many places ```js var isPreview = "true" === { VITE_APP_ENV: "local", VITE_MOCK_LOGIN_UID: "1", VITE_API_URL: " https://api-.com/v1/ ", VITE_SHOPIFY_API_KEY: "14356af9cc5372", VITE_DEFAULT_LANG: "en", VITE_ENABLE_HTTP_REPLACE_HTTPS: "true", VITE_EXTENSION_UUID: "c899b-afa8917aa493", VITE_I18N_DEBUG: "false", VITE_COOKIE_NAME_PREFIX: "pg_", BASE_URL: "/", MODE: "production", DEV: ! 1, PROD: ! zero }. VITE_PLUGIN_IS_PREVIEW ``` This is obviously not a good practice ###Optimization method I think of a simple optimization method, which can be written as follows: ```ts /* eslint-disable sort-keys,sort-keys-fix/sort-keys-fix */ const envLists = import.meta.env const EnvHelper = { //Self defined env: envLists. VITE_APP_ENV, //Vite's own judgment conditions get mode() { return envLists. MODE }, get isDev() { return envLists. MODE === "development" }, get isProd() { return ! this.isDev }, // ///////// //Custom get mockLoginUid() { return envLists. VITE_MOCK_LOGIN_UID }, get apiURL() { return envLists. VITE_API_URL }, get shopifyApiKey() { return envLists. VITE_SHOPIFY_API_KEY }, get defaultLang() { return envLists. VITE_DEFAULT_LANG }, get enableHttpReplaceHttps() { return envLists. VITE_ENABLE_HTTP_REPLACE_HTTPS }, get extensionUUID() { return envLists. VITE_EXTENSION_UUID }, get i18nDebug() { return envLists. VITE_I18N_DEBUG }, get cookieNamePrefix() { return envLists. VITE_COOKIE_NAME_PREFIX }, get pluginIsPreview() { return envLists. VITE_PLUGIN_IS_PREVIEW }, get pluginBuildTime() { return envLists. VITE_PLUGIN_BUILD_TIME } } export default EnvHelper ```
Vue 3 labels returned from the rendering backend | Use of component labels and compile Author: Chuwen Time: 2022-05-13 Classification: WEB development comment ##Cause There is a requirement in the project that the HTML returned by the backend should be dynamically rendered, as shown below. The following * * each * * 'svg' is dynamically returned by the backend ```html <template> <div class="class-name"> <!-- Each svg here will return rendering from the API --> <svg></svg> <svg></svg> <svg></svg> </div> </template> ``` Try this, but it will not render: ```html <template> <div class="class-name"> <template v-html="svg1"></template> <template v-html="svg2"></template> <template v-html="svg3"></template> </div> </template> ``` ##Solution By browsing the Vue.js document, the following articles are found: -[Render Function - Template Compilation]( https://v3.cn.vuejs.org/guide/render-function.html#%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BB%84%E4%BB%B6 Template Compilation) -[Built in special element - component]( https://staging-cn.vuejs.org/api/built-in-special-elements.html#component ) -[Use Vue. file in Vue.js]( https://stackoverflow.com/questions/68981411/vue3-how-to-use-vue-compile-in-vue3 ) ###'<component>' is a Vue built-in special element >'<component>' and '<slot>' have similar component characteristics and are part of the template syntax. They are not real components and will be compiled during template compilation. Therefore, they are usually written in lowercase letters in the template. Therefore, using '<component>' and 'Vue. file' can achieve the function I want gist: https://gist.github.com/PrintNow/f9fec1890bd736593800564c45584c3b ```vue <template> <h2>Achieve results</h2> <div class="class-name"> <component :is="getSvg" :style="svg_style"></component> <component :is="getSvg" :style="svg_style"></component> <component :is="getSvg" :style="svg_style"></component> </div> <h2><code>getSvg</code>Output result:</h2> <pre v-text="getSvg"></pre> <h2><code>svg</code>Output result:</h2> <pre v-text="svg"></pre> </template> <script> import { compile } from "vue"; export default { name: "App", components: {}, data() { return { svg_style: { width: "64px", height: "auto" }, svg: `<svg t="1652418272564" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns=" http://www.w3.org/2000/svg " p-id="2045" width="512" height="512"> <path d="M408 617m-348 0a348 348 0 1 0 696 0 348 348 0 1 0-696 0Z" fill="#C9F4EB" p-id="2046"></path> <path d="M927 369H111.75a25 25 0 0 1 0-50H927a25 25 0 0 1 0 50zM569.79 748H225a30 30 0 0 1 0-60h344.79a30 30 0 0 1 0 60zM668 748h-2.84a30 30 0 1 1 0-60H668a30 30 0 0 1 0 60z" fill="#087E6A" p-id="2047"></path> <path d="M832 860H197A125.14 125.14 0 0 1 72 735V263a125.14 125.14 0 0 1 125-125h635a125.14 125.14 0 0 1 125 125v472a125.14 125.14 0 0 1-125 125zM197 188a75.09 75.09 0 0 0-75 75v472a75.09 75.09 0 0 0 75 75h635a75.09 75.09 0 0 0 75-75V263a75.09 75.09 0 0 0-75-75z" fill="#087E6A" p-id="2048"></path> </svg>`, }; }, computed: { getSvg() { return compile(this.svg); }, }, }; </script> <style> .class-name * { display: block; } </style> ``` <iframe src=" https://codesandbox.io/embed/sparkling-fog-2ycfeb?fontsize=14&hidenavigation=1&module=%2Fsrc%2FApp.vue&theme=dark " style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" title="sparkling-fog-2ycfeb" allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" ></iframe>