在构建Docker映像时已安装时要求安装Typescript

时间:2020-01-10 13:46:29

标签: reactjs typescript docker next.js

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

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

但是,在构建docker映像时,我得到以下几点:

"The requested address is not valid in its context"

我已经安装了Typescript。这对我来说很混乱。

我正在使用的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.

4 个答案:

答案 0 :(得分:6)

运行时(甚至在Docker外部)

export NODE_ENV=production
npm install

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

答案 1 :(得分:1)

信誉@Ahmed Rebai

必须手动将Typescript添加到Package.json文件中的依赖项。正常的npm安装只会将其添加到dev中。

答案 2 :(得分:1)

我在 aws amplify 上部署 next.js 应用程序时遇到了同样的错误。 我不想将 typescript 移动到依赖项到 devdependencies。

我的解决方案只是更改构建设置

'npm 安装' 到 'npm install --dev typescript && npm install'

错误消失了。

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

答案 3 :(得分:0)

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