1
  • I build my application locally into a dist folder; package.json is also copied over

  • npm install --production is run in the dist folder

  • dist folder is then zipped up

  • Zip is uploaded to server, where it is unzipped

  • Attempting to run the application on the server results in Error: cannot find module <module_name>

This error occurs for any module unless I manually run npm install <module_name> after the unzip

Everything works fine if I run it from my dist folder, so it's not an issue with the pre-zip part

Running npm install --production after the unzip doesn't resolve anything (just says that all modules are up-to-date)

Deleting node_modules on the server, then running npm install --production makes everything work fine

Unzipping the zip file locally then running the application works fine

What am I missing/doing wrong?

EDIT: package.json:

{
    "name": "app-name",
    "version": "0.0.1",
    "scripts": {
        "start": "index.js"
    },
    "dependencies": {
        "body-parser": "^1.18.2",
        "email-validator": "^1.1.1",
        "express": "^4.16.2",
        "express-jwt": "^5.3.0",
        "express-jwt-permissions": "^1.0.0",
        "firebase-admin": "^5.5.1",
        "jsonwebtoken": "^8.1.0",
        "mailgun-js": "^0.13.1",
        "mysql2": "^1.5.0",
        "password-hash-and-salt": "^0.1.4",
        "randomstring": "^1.1.5",
        "reflect-metadata": "^0.1.10",
        "sequelize": "^4.22.6",
        "sequelize-typescript": "^0.5.0"
    },
    "devDependencies": {
        "@types/express": "^4.0.39",
        "@types/express-jwt": "0.0.38",
        "@types/jsonwebtoken": "^7.2.4",
        "@types/node": "^8.0.51",
        "@types/sequelize": "^4.0.79",
        "babel-core": "^6.26.0",
        "del": "^3.0.0",
        "gulp": "^3.9.1",
        "gulp-babel": "^7.0.0",
        "gulp-ssh": "^0.6.0",
        "gulp-typescript": "^3.2.3",
        "gulp-zip": "^4.0.0",
        "merge-stream": "^1.0.1",
        "typescript": "^2.6.1"
    }
}
Ben Ezard
  • 395
  • 4
  • 18
  • 1
    When it says that cannot find the module, it also says the path where it was looking. Check that is looking for the correct `node_modules` path. – Jorge Fuentes González Dec 13 '17 at 11:46
  • There's no path in the error: ```Error: Cannot find module 'express' at Function.Module._resolveFilename (module.js:536:15) at Function.Module._load (module.js:466:25) at Module.require (module.js:579:17) at require (internal/module.js:11:18)...etc``` (only path is the location of the file that's trying to load express) – Ben Ezard Dec 13 '17 at 11:57
  • Hm, ok. Can you check if you have `express` inside the `node_modules` folder? Maybe you have a global installation and don't remember that you installed it that way. – Jorge Fuentes González Dec 13 '17 at 12:07
  • Also checked that, and express is not installed globally (and neither are any of the other modules that I use) – Ben Ezard Dec 13 '17 at 12:09
  • Is kinda weird. Can you show your package.json? I see that express, in their GitHub, tell to install globally a native command. Maybe that's the problem. By the way, are you saying that the folder `node_modules` contains the folder `express` with all the needed express files? – Jorge Fuentes González Dec 13 '17 at 12:18
  • I've added the package.json contents to the question – Ben Ezard Dec 13 '17 at 12:26
  • 1
    Kind weird, yeah. I can't help anymore. I upvoted. Let's see if someone else can find what happens. – Jorge Fuentes González Dec 13 '17 at 12:33
  • What versions of node/npm are you running on the server vs dev? What do the permissions on node_modules look like when unzipped vs npm installed? Are there any other differences between the server/local (mac vs linux, etc)? – Will Dec 13 '17 at 16:20
  • Identical versions of both Node and NPM. Same error occurs even when I recursively chmod the entire node_modules directory to 777. Local machine is MacOS, server is Linux (Ubuntu). – Ben Ezard Dec 14 '17 at 09:27

1 Answers1

0

Use npm install --only=production instead. As per this article

Hope this helps, I am newbie to Node js, just trying to help

Nagaraja Malla
  • 124
  • 1
  • 9