reactjs - 构建 Docker 镜像时已安装 Typescript 时要求安装

标签 reactjs typescript docker next.js

我正在尝试构建一个使用 Typescript 的 Next.js/React 应用程序的 docker 镜像。

安装了 Typescript,我可以在没有 docker 的情况下在本地运行构建。

但是,随着 docker 镜像的构建,我得到了以下几点:

Step 8/10 : RUN npm run build
 ---> Running in ee577c719739

> project@0.0.5 build /app
> next build

Creating an optimized production build...
Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
https://nextjs.org/telemetry

It looks like you're trying to use TypeScript but do not have the required package(s) installed.

Please install typescript by running:

        npm install --save-dev typescript

If you are not trying to use TypeScript, please remove the tsconfig.json file from your package root (and any TypeScript files).

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! project@0.0.5 build: `next build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the project@0.0.5 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我已经安装了 Typescript。这对我来说非常困惑。

我正在使用的 Docker 镜像如下所示:
FROM gcr.io/companyX/companyX-node-base:12-alpine

# Copy in the project files
COPY . .

# Clean
USER root
RUN rm -fr node_modules

ENV NODE_ENV=production

COPY package*.json ./

RUN npm install && \
    npm cache clean --force

RUN npm run build

EXPOSE 3000

# Running the app
CMD [ "npm", "start" ]

最佳答案

当你运行时(甚至在 Docker 之外)

export NODE_ENV=production
npm install

它只安装 dependencies来自您的 package.json而不是 devDependencies .另一方面,您可能需要 devDependencies获取类似 typescript 的工具.

这里的好解决方案是使用多阶段构建。第一阶段安装所有依赖项;第二阶段仅复制运行应用程序所需的内容。

FROM gcr.io/companyX/companyX-node-base:12-alpine AS build

# Copy in only the parts needed to install dependencies
# (This avoids rebuilds if the package.json hasn’t changed)
COPY package.json package.lock .

# Install dependencies (including dev dependencies)
RUN npm install

# Copy in the rest of the project
# (include node_modules in a .dockerignore file)
COPY . .

# Build the project
RUN npm run build

# Second stage: runtime
FROM gcr.io/companyX/companyX-node-base:12-alpine

ENV NODE_ENV=production

# Again get dependencies, but this time only install
# runtime dependencies
COPY package.json package.lock .
RUN npm install

# Get the built application from the first stage
COPY --from=build /app/dist dist

# Set runtime metadata
EXPOSE 3000
CMD [ "npm", "start" ]
# CMD ["node", "dist/index.js"]

请注意,此方法与使用来自主机的任意内容覆盖应用程序内容或尝试将库存储在 Docker 卷中的设置不兼容。最终镜像是独立的,不需要任何主机内容,并且可以在其他系统上按原样运行,而无需单独复制源代码。

关于reactjs - 构建 Docker 镜像时已安装 Typescript 时要求安装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59682961/

相关文章:

android - 如何将 Ionic 3 与 android native 代码链接起来?

reactjs - 键入 React HOC 的正确方法是什么?

python - 尝试从外部Python连接到Docker中的SQL Server

css - 使用 Typescript 在我的 SASS 代码中注入(inject)背景 url ("MyImageHere.png"

amazon-web-services - 在Jenkins中的Docker容器中运行命令

docker -/bin/sh : service: command not found on cent os 7

javascript - 在 React 的不同页面中渲染同一组件两次

javascript - JSX 是什么?它在 React 中到底做了什么?

javascript - 停止监听滚动事件直到数据加载

javascript - Discordjs bot (Nodejs) 如何使用reactjs将数据添加到DOM