Docker-无法解析'/ usr / src / app / src'中的节点模块

时间:2019-03-18 05:43:36

标签: node.js reactjs docker npm

我有这项docker服务:

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-dev

# 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

我想念什么?

1 个答案:

答案 0 :(得分:1)

希望这有助于为我工作的人奔跑

docker-compose down -v

它将删除所有卷。