构建 Docker 图像时要求在已安装的情况下安装 Typescript

Asked to install Typescript when already installed when building Docker image

我正在尝试构建 Next.js/React 应用程序的 docker 图像,该应用程序使用 Typescript。

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" ]

感谢@Ahmed Rebai

必须手动将打字稿添加到 Package.json 文件中的依赖项。正常的 npm install 只会将其添加到 dev.

当你运行(即使在外面Docker)

export NODE_ENV=production
npm install

它只安装来自您的 package.jsondependencies 而不是 devDependencies。另一方面,您可能需要 devDependencies 来获得像 typescript.

这样的工具

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

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 卷中的设置不兼容。最终镜像是自包含的,不需要任何主机内容,并且可以 运行 在其他系统上保持原样,无需单独复制源代码。

在生产环境中它不安装devDependencies。所以你可以在 npm install

之后设置你的 NODE_ENV
FROM gcr.io/companyX/companyX-node-base:12-alpine

# Copy in the project files
COPY . .

# Clean
USER root
RUN rm -fr node_modules

COPY package*.json ./

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

ENV NODE_ENV=production

RUN npm run build

EXPOSE 3000

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

我在 AWS Amplify 上部署 Next.js 应用程序时遇到了同样的错误,我不想将 typescript 从 devdependencies 移动到 dependencies。

我的解决方案是将构建设置从 npm install
更改为 到 npm install --dev typescript && npm install

这将消除错误。

注意: 不过我不确定这是否适用于 docker。