在Docker中运行时,Craco构建在编译Typescript文件时失败

如何解决在Docker中运行时,Craco构建在编译Typescript文件时失败

我需要部署一个Web应用程序,该Web应用程序与打字稿反应。 deploy方法是构建运行nginx服务器的docker映像,该服务器为静态构建提供服务,并将其保存在kubernetes容器中。

构建脚本为craco build,并且在本地终端(macOS)中运行时可以正常运行。问题是当在Docker映像中运行相同的脚本时,它将返回错误:

Creating an optimized production build...
Failed to compile.

./src/views/main/routeConfigs.ts
Syntax error: Unexpected token : (7:25)

routeConfig.ts文件的头是

import { vueCompsInReact } from '@console/legacy-v1/vueDMZ';
import { IRoute } from 'models/router.models';
import { wrapReactifiedVueComponent } from 'views/common/routing/wrapReactifiedVueComponent';
import { ROUTE_KEY } from '../routes';
import Home from './Home';

export const MAIN_ROUTES: IRoute<ROUTE_KEY>[] = [{
    routeKey: ROUTE_KEY.R_HOME,path: '/',exact: true,component: Home,},...

Unexpected token : (7:25)export const MAIN_ROUTES: IRoute<ROUTE_KEY>[]行中的冒号':'

我不明白的是,它仅在docker中失败。 Dockerfile是:

FROM node:12-alpine as builder

# define PROJECT_PATH to project 
ENV PROJECT_PATH=packages/console-core
ENV V1_PATH=legacy-v1
ENV APP_ROOT=/console-v2

RUN node -v
RUN mkdir ${APP_ROOT}
WORKDIR ${APP_ROOT}
# copy root package to install dependencies
COPY .nvmrc .stylelintrc.js package.json package-lock.json lerna.json ./
# copy console core package
RUN mkdir ./packages
RUN mkdir ./${PROJECT_PATH}
COPY ${PROJECT_PATH}/package.json ${PROJECT_PATH}/package-lock.json ${PROJECT_PATH}/craco.config.js ./${PROJECT_PATH}/
# copy console v1 package
RUN mkdir ./${V1_PATH}
COPY ${V1_PATH}/package.json ${V1_PATH}/package-lock.json ./${V1_PATH}/
# install dependencies
RUN npm install 
RUN npx lerna bootstrap 

COPY ${V1_PATH} ./${V1_PATH}
COPY ${PROJECT_PATH} ./${PROJECT_PATH}

# RUN cd ${PROJECT_PATH}
WORKDIR ${PROJECT_PATH}

RUN npm run build

使用命令RUN node -v,我可以看到node:12-alpine映像带有节点12.18.3版本,而在本地则为12.14.0

package.json文件为

{
  "name": "@console/core","version": "2.0.0","private": true,"description": "Core package of the web console","scripts": {
    "start": "craco start","build": "craco build","serve": "serve -s build","test": "npm run lint && craco test --transformIgnorePatterns \"node_modules/(?!(@snipsonian))\"","eslint": "eslint --ext .js,.ts,.tsx src","stylelint": "stylelint src/**/*.scss","lint": "npm run eslint"
  },"dependencies": {
    "@console/legacy-v1": "^0.1.0","@material-ui/core": "^4.11.0","@material-ui/icons": "^4.9.1","@snipsonian/browser": "^1.12.0","@snipsonian/core": "^1.11.0","@snipsonian/dvlp": "^1.12.0","@snipsonian/observable-state": "^1.12.0","@snipsonian/react": "^1.12.0","@snipsonian/react-observable-state": "^1.12.0","clsx": "^1.1.1","flat": "^5.0.0","gsap": "^3.5.1","history": "^5.0.0","immer": "^7.0.1","oidc-client": "^1.10.1","ramda": "^0.27.0","react": "^16.13.1","react-dom": "^16.13.1","react-router-dom": "^5.2.0","reselect": "^4.0.0"
  },"devDependencies": {
    "@craco/craco": "^5.6.4","@testing-library/jest-dom": "^4.2.4","@testing-library/react": "^9.5.0","@testing-library/user-event": "^7.2.1","@types/flat": "^5.0.1","@types/history": "^4.7.6","@types/jest": "^24.9.1","@types/node": "^12.12.47","@types/ramda": "^0.27.7","@types/react": "^16.9.36","@types/react-dom": "^16.9.8","@types/react-router-dom": "^5.1.5","copy-webpack-plugin": "^6.0.3","eslint": "^6.8.0","http-proxy-middleware": "^1.0.4","node-sass": "^4.14.1","react-scripts": "3.4.1","serve": "^11.3.2","stylelint": "^13.6.0","vue-loader": "^15.9.3","vue-template-compiler": "^2.6.11","winston": "^3.3.3","yaml-loader": "^0.6.0"
  },"eslintConfig": {
    "extends": "react-app"
  },"browserslist": {
    "production": [
      ">0.2%","not dead","not op_mini all","last 2 chrome version","last 2 firefox version","last 2 safari version","not ie <= 11"
    ],"development": [
      "last 1 chrome version","last 1 firefox version","last 1 safari version"
    ]
  }
}

craco.config.js文件为:

/* eslint-disable @typescript-eslint/no-var-requires */
const { ESLINT_MODES,addBeforeLoader,loaderByName,getLoader } = require('@craco/craco');
const path = require('path');
// eslint-disable-next-line import/no-extraneous-dependencies
const StyleLintPlugin = require('stylelint-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const CopyPlugin = require('copy-webpack-plugin');

const appSrc = path.resolve(__dirname,'src');
const imagesConsoleV1 = path.resolve(__dirname,'../../legacy-v1/public/img');

module.exports = {
    eslint: {
        mode: ESLINT_MODES.file,webpack: {
        alias: {
            /**
             * Add the aliases for all the top-level folders in the `src/` folder.
             * In combination with tsconfig.pathsOverride.json
             * See https://resir014.xyz/posts/2019/03/13/using-typescript-absolute-paths-in-cra-20/
             */
            api: `${appSrc}/api/`,config: `${appSrc}/config/`,models: `${appSrc}/models/`,state: `${appSrc}/state/`,utils: `${appSrc}/utils/`,views: `${appSrc}/views/`,plugins: [
            new VueLoaderPlugin(),new StyleLintPlugin({
                configBasedir: __dirname,context: appSrc,files: ['**/*.scss'],}),new CopyPlugin({
                patterns: [
                    {
                        from: imagesConsoleV1,transformPath(targetPath) {
                            return `img/${targetPath}`;
                        },],configure: (webpackConfig /*,{ env,paths } */) => {
            // console.log('webpackConfig',JSON.stringify(webpackConfig));

            /**
             * Loader-execution-order is from bottom to top!
             * And from right to left!
             */

            /* making sure the file-loader does not handle the .vue files */
            const getFileLoaderResult = getLoader(
                webpackConfig,loaderByName('file-loader'),);
            if (getFileLoaderResult.isFound) {
                const { match } = getFileLoaderResult;
                // console.log('file-loader found',match.loader);
                // console.log(match.loader.exclude);

                /* orig value: exclude: [ /\.(js|mjs|jsx|ts|tsx)$/,/\.html$/,/\.json$/ ],*/
                match.loader.exclude = [ /\.(js|mjs|jsx|ts|tsx|vue)$/,/\.json$/ ];
            }

            /* the yaml loader has to be placed 'before' (above) the file-loader
             * so that the yaml-loader is used on .yaml files instead of the file-loader (oneOf) */
            const yamlLoader = {
                test: /\.ya?ml$/,type: 'json',// Required by Webpack v4
                use: 'yaml-loader',};

            addBeforeLoader(webpackConfig,yamlLoader);

            /* adding the vue-loader
             * p.s. Doing it myself instead of "addBeforeLoader(webpackConfig,loaderByName('babel-loader'),vueLoader)"
             * because that adds the vueLoader inside te oneOf array instead of on the top-level.
             * Also "addAfterLoader" did not produce the expected behaviour. */
            // webpackConfig.resolve.alias['vue$'] = 'vue/dist/vue.esm.js';
            const vueLoader = {
                test: /\.vue$/,loader: 'vue-loader',};
            webpackConfig.module.rules.splice(0,vueLoader); // adding it as the first element

            // console.log('webpackConfig.module',JSON.stringify(webpackConfig.module));

            return webpackConfig;
        },jest: {
        configure: {
            moduleNameMapper: {
                /**
                 * Jest module mapper which will detect our absolute imports.
                 * In combination with tsconfig.pathsOverride.json
                 * See https://resir014.xyz/posts/2019/03/13/using-typescript-absolute-paths-in-cra-20/
                 */
                '^api(.*)$': '<rootDir>/src/api$1','^config(.*)$': '<rootDir>/src/config$1','^models(.*)$': '<rootDir>/src/models$1','^state(.*)$': '<rootDir>/src/state$1','^utils(.*)$': '<rootDir>/src/utils$1','^views(.*)$': '<rootDir>/src/views$1',/**
                 * For the jest unit tests,we replace the vueDMZ (which serves as an entrypoint to the legacy v1 code)
                 * with a mock version,because otherwise jest has problems with the library imports in the legacy code
                 * (and problems with the vue code).
                 */
                '@console/legacy-v1/vueDMZ': '<rootDir>/../../legacy-v1/vueDMZ.jestMock',babel: {
        plugins: [
            "@babel/plugin-proposal-optional-chaining",};

我不是Web应用程序专家,我的问题是Docker映像中可能缺少哪些设置或本地启用的功能,这是导致此错误的原因,我理解这是“无法解析最新的打字稿语法”

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-