【问题标题】:Docker - Can't resolve node module in '/usr/src/app/src'Docker - 无法解析“/usr/src/app/src”中的节点模块
【发布时间】:2019-08-08 10:56:20
【问题描述】:

我有这个dockerservice:

client/
      .dockerignore
      Dockerfile-dev
      package.json
      node_modules/
                  react-router-dom/   
                  spotify-web-api-js/
                  and-many-more/
      src/  
         App.jsx
         Spotify.js

其中安装了以下node 模块:

package.json

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "axios": "^0.18.0",
    "react": "^16.8.2",
    "react-dom": "^16.8.2",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.5",
    "spotify-web-api-js": "^0.22.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "enzyme": "^3.8.0",
    "enzyme-adapter-react-16": "^1.7.1"
  }
}

像这样:

Dockerfile-开发

# base image
FROM node:11.6.0-alpine

# set working directory
WORKDIR /usr/src/app

# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install --silent
RUN npm install react-scripts@2.1.2 -g --silent

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

在同一条路径上,src,但是,我观察到两种不同的行为:

1) 这行得通:

App.jsx

import React, { Component } from 'react';
import { Route, Switch } from 'react-router-dom';

2) 但这不起作用:

Spotify.js

import React, { Component } from 'react';   
import SpotifyWebApi from 'spotify-web-api-js';

抛出以下错误:

Failed to compile
./src/Spotify.js
Module not found: Can't resolve 'spotify-web-api-js' in '/usr/src/app/src'

docker-compose-dev.yml

  client:
    build:
      context: ./services/client
      dockerfile: Dockerfile-dev
    volumes:
      - './services/client:/usr/src/app'
      - '/usr/src/app/node_modules'
    ports:
      - 3007:3000
    environment:
      - NODE_ENV=development
      - REACT_APP_WEB_SERVICE_URL=${REACT_APP_WEB_SERVICE_URL}
    depends_on:
      - web  

.dockerignore

node_modules
coverage
build
env
htmlcov
.dockerignore
Dockerfile-dev
Dockerfile-prod

我错过了什么?

【问题讨论】:

  • 那是你完整的 Dockerfile 吗?看起来你还没有复制你的 src 文件。
  • 是的。我还应该做什么?
  • 我的意思是你还没有将你的 src/* 复制到 docker。 COPY src/* .npm install 声明之后,但我不确定这是否是导致问题的原因。
  • 让我们试试吧..
  • ERROR: Service 'client' failed to build: When using COPY with more than one source file, the destination must be a directory and end with a /

标签: node.js reactjs docker npm


【解决方案1】:

希望这可以帮助对我有用的人

docker-compose down -v

它会删除所有卷。

【讨论】:

  • 我很尴尬地说我花了多长时间重新安装模块甚至 NPM / Node 完全。最后,我发现我有大约 100 个以前的卷仍在后台运行。此命令解决了我的问题 - 非常感谢分享
  • 不知道为什么这个答案还没有被接受,虽然它有效。
猜你喜欢
  • 2023-01-05
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 2018-11-19
  • 2022-09-26
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
相关资源
最近更新 更多