【问题标题】:Asked to install Typescript when already installed when building Docker image在构建 Docker 映像时要求安装 Typescript
【发布时间】:2020-04-28 04:18:29
【问题描述】:

我正在尝试构建使用 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" ]

【问题讨论】:

  • 你的 package.json 文件中提到过?
  • 我一直在尝试 npm install typescript --save,我认为它会将它添加到我的正常依赖项中,但我错了,它只是在 dev 依赖项上。所以谢谢老哥。手动将其添加到正常依赖项中并运行。 :)
  • @RebaiAhmed 就我而言,是的。但我仍然收到该错误消息。见stackoverflow.com/questions/65831617/…

标签: reactjs typescript docker next.js


【解决方案1】:

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

export NODE_ENV=production
npm install

它只从您的package.json 安装dependencies,而不是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 卷中的设置不兼容。最终镜像是自包含的,不需要任何宿主内容,可以在其他系统上按原样运行,无需单独复制源代码。

【讨论】:

  • 热重载怎么样?复制 src 文件并编译它们时,热重载功能不再起作用
  • 它在您的主机节点开发环境中应该仍然可以正常工作,即使您使用 Docker 进行部署。
  • 但是如果您使用构建文件来显示 Web 应用程序,那么代码更改将不会直接反映。它需要另一个构建来显示更改。 (在这里查看我的问题:stackoverflow.com/questions/65831617/…
【解决方案2】:

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

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

这将消除错误。

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

【讨论】:

    【解决方案3】:

    感谢@Ahmed Rebai

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

    【讨论】:

    • 这对我不起作用。我仍然收到相同的错误消息。见stackoverflow.com/questions/65831617/…
    • Typescript 只能在 devDependencies 中。将其添加到 dependencies 意味着您在生产中使用打字稿,而您不是(至少不应该)。 Typescript 编译您的代码,然后然后您在生产环境中运行输出的 JavaScript 代码。
    【解决方案4】:

    在生产环境中它不会安装 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" ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-06
      • 2015-09-09
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      相关资源
      最近更新 更多