gpt4 book ai didi

node.js - Docker - 无法解析 '/usr/src/app/src' 中的 Node 模块

转载 作者:行者123 更新时间:2023-12-02 21:09:55 24 4
gpt4 key购买 nike

我有这个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

我错过了什么?

最佳答案

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

docker-compose down -v

它删除所有卷。

关于node.js - Docker - 无法解析 '/usr/src/app/src' 中的 Node 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55215171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com