简体   繁体   中英

Docker + private npm package = module not found/cannot find module

I have a private package (source code) in my gitlab repository called 'anvil'. I have a project, the consumer of this package, called 'warden' that will be started in a docker container (it has a Dockerfile.)

I've defined an entry in warden's (consumer) dependencies in a way that it will install anvil (package in my gitlab), it looks like this:

"dependencies": {
    "@types/knex": "^0.14.18",
    "@types/node": "^10.9.4",
    "discord-anvil": "git+https://gitlab+deploy-token-<deploy_token_here>@gitlab.com/<myusername>/anvil.git#dev-2.0",
    "discord.js": "^11.4.2",
    "knex": "^0.15.2",
    "sqlite3": "^4.0.2",
    "typescript": "^2.9.2"
}

My Dockerfile:

FROM node:10
WORKDIR /app
ADD . /app
RUN npm install
ENV token="<private_token_here>"
ENV prefix="."
RUN npm start

The problem is that once I start the container using sudo docker run . everything goes fine until the postinstall script in anvil (the package) is executed.

Anvil's package.json (some unimportant stuff taken out):

{
    "name": "discord-anvil",
    "version": "2.9.0",
    "main": "./dist/index.js",
    "scripts": {
        "watch": "tsc --watch",
        "build": "tsc",
        "type-check": "tsc --noEmit",
        "test": "npm run build && mocha dist/test/tests.js",
        "postinstall": "npm run build"
    },
    "license": "MIT",
    "dependencies": {
        ...
    },
    "devDependencies": {
        ...
        "typescript": "^2.8.1"
    },
    "files": [
        "dist"
    ]
}

As you can see, anvil (the package) has a postinstall script which is executed when installed by warden. When the docker file is being built, this is what I get:

...
> discord-anvil@2.9.0 postinstall /app/node_modules/discord-anvil
> npm run build


> discord-anvil@2.9.0 build /app/node_modules/discord-anvil
> tsc

../../src/app.ts(14,8): error TS2307: Cannot find module 'discord-anvil'.
../../src/app.ts(15,39): error TS2307: Cannot find module 'discord-anvil/dist/commands/command'.
../../src/commands/ban.ts(3,68): error TS2307: Cannot find module 'discord-anvil'.
../../src/commands/ban.ts(4,37): error TS2307: Cannot find module 'discord-anvil/dist/commands/command'.
...

The same error continues for basically every file in which anvil is used (required/imported). I was forced into using the postinstall script to build the project since plain installing it from the git repo just puts the source code under node_modules not the actual compiled package. By the way, anvil is just short for discord-anvil which is the official package name.

However, if I repeat the same procedure locally (not docker container), everything goes fine and typescript does not throw errors.

Any ideas? Thanks in advance.

Turns out those errors were because typescript was compiling the whole project not just anvil which is what I wanted, this was probably due because anvil's package.json 'files' property was set to only contain the 'dist' folder, and when it was pulled from gitlab it would only actually pull README, LICENSE and package.json with nothing else .

I think that the typescript compiler if it couldn't find any files to compile nor a tsconfig.json file it would look on the parent directories (which was the root, warden) and thus throwing the errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM