Docker-node: Error when running Node after Babel build

Created on 4 May 2017  路  5Comments  路  Source: nodejs/docker-node

Description

I have problem getting my docker deployment working. The following setup works on my computer.

OS: macOS 10.12.4
node: v7.9.0
npm: 4.2.0

and also on a Linux server running
node: v6.9.4
npm: 3.10.10

npm run production

Error

But inside container I get:

node@7cfddd540d82:~/app$ node build/index.js
module.js:472
    throw err;
    ^

Error: Cannot find module '../models/User'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/node/app/build/services/authService.js:118:13)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/node/app/build/index.js:7:20)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)

Running out of ideas to try 馃槥

Setup

Folders

src/
- models
-- user.js
- services
-- authService.js 
- index.js
package.json
yarn.lock
.dockerignore
.babelrc
.env

services/authService.js

import Promise from 'bluebird';

import User from '../models/User';

class AuthService {
   authenticate(username, password) {
      return Promise.resolve(new User({}))
   }
}
const service = new AuthService();
export default service

models/user.js

class User {
   constructor(params) {
      this.username = params.username || null;
      this.password = params.username || null;
   }
}

export default User

index.js

import dotenv from 'dotenv'
dotenv.config();
import authService from './services/authService.js'
import express from 'express';

const app = express();
app.post("/", function(req, res) {
  authService.authenticate(req.body.username, req.body.password)
    .then(user => {
       res.json({success:true})
    })
});

app.listen(3000, function () {
 console.log('Example app listening on port 3000!')
});

package.json

{
  "name": "app",
  "version": "1.0.0",
  "main": "index.js",
  "repository": {},
  "author": "Kristjan Metsalu",
  "license": "MIT",
  "scripts": {
    "dev": "NODE_ENV=development LOG_LEVEL=debug nodemon -d 2 src/index.js --exec ./node_modules/.bin/babel-node",
    "clean": "rm -rf build && mkdir build",
    "build": "babel -d ./build ./src -s",
    "production": "npm run clean && npm run build && NODE_ENV=production node build/index.js"
  },
  "dependencies": {
    "axios": "^0.16.1",
    "bcrypt": "^1.0.2",
    "bluebird": "^3.4.7",
    "body-parser": "^1.16.1",
    "cors": "^2.8.1",
    "dotenv": "^4.0.0",
    "express": "^4.14.1",
    "helmet": "^3.5.0",
    "jxon": "^2.0.0-beta.5",
    "knex": "^0.13.0",
    "mysql": "^2.13.0",
    "nodemailer": "^4.0.1",
    "pg-monitor": "^0.7.1",
    "pg-promise": "^5.6.7",
    "redis": "^2.7.1",
    "winston": "^2.3.1"
  },
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-plugin-add-module-exports": "^0.2.1",
    "babel-plugin-transform-object-rest-spread": "^6.23.0",
    "babel-preset-es2015": "^6.24.1",
    "nodemon": "^1.11.0"
  }
}

.babelrc

{
  "presets": [
    "es2015"
  ],
  "plugins": [
    "transform-object-rest-spread",
    "add-module-exports"
  ]
}

.dockerignore

.idea/
.git/

node_modules/
build/

Dockerfile

FROM node:7.10

ENV HOME=/home/node

RUN mkdir -p $HOME/app

COPY yarn.lock $HOME/app/
COPY package.json $HOME/app/
RUN chown -R node:node $HOME/*

USER node
WORKDIR $HOME/app
RUN npm install

EXPOSE 3000
ENV NODE_ENV production

COPY . $HOME/app

RUN npm run build

CMD ["npm", "run", "production"]
question

Most helpful comment

@kmetsalu probably you already found your issue but:

All 5 comments

@kmetsalu probably you already found your issue but:

I'm having a slightly different error but i believe babel isn't transpiling my code at the right time on build. I know to include these in my .babelrc, which leads to an insancely slow build once i have all these installed with Docker, but i dont need them with npm scripts. Am i missing some environmental variables / loading them at the wrong point

Couldn't find preset "es2015-loose"
Couldn't find preset "stage-1"

FROM mhart/alpine-node:latest

RUN npm i -g webpack \
    babel-cli

# creates a cache for node_modules and updates them if package.json changes
ADD package.json /tmp/package.json
RUN cd /tmp && npm config set registry https://registry.npmjs.org/ && npm install
RUN mkdir -p /usr/src/app && cp -a /tmp/node_modules /usr/src/app

COPY . /usr/src/app

# Set working directory from here onwards
WORKDIR /usr/src/app

RUN ["npm", "run", "prodWebpack"]

EXPOSE 2020

version: '2'
services:
  web:
    build: .
    volumes:
      - ".:/usr/src/app"
    ports:
      - "2020:2020"

I dont need these in any of my npm scripts with out docker:

"devWebpack": "THEME=mpsl NODE_ENV=development webpack-dev-server --progress",
"devExpress": "THEME=mpsl NODE_ENV=development node index.js --progress",
"prodWebpack": "THEME=mpsl NODE_ENV=production webpack -p --progress --config ./webpack.prod.config.js",' "prodExpress": "THEME=mpsl NODE_ENV=production node index.js",`

@kmetsalu Still having issues?

@lxm7 you're using mhart/alpine-node and not the official image. Do you have the same issue if you try node:alpine?

../models/User not ../models/user

Not a problem anymore. Thank you quys for all the suggestions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eyaylagul picture eyaylagul  路  3Comments

marciomsm picture marciomsm  路  4Comments

frankbaele picture frankbaele  路  3Comments

polys picture polys  路  3Comments

tigrus picture tigrus  路  4Comments