alhazmy13 / serverless-offline-python

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
35 stars 18 forks source link

TypeError: Os.tmpDir is not a function when plugin is active on all serverless commands #18

Open derekvincent opened 4 years ago

derekvincent commented 4 years ago

I am using a fresh serverless install (via brew) and when I enable the serverless-offline-python any sls function I try, produces the error below. If I comment the plugin out in the serverless.yml then everything works again (well not testing my Python-based lambda offline).

MacOS: 10.15.3 Node Version: 14.0.0 Framework Version: 1.68.0 Plugin Version: 3.6.6 SDK Version: 2.3.0 Components Version: 2.30.1

> SLS_DEBUG=* sls print

  Type Error ---------------------------------------------

  TypeError: Os.tmpDir is not a function
      at Object.<anonymous> (/Users/derekvincent/Dropbox/Development/aws_sap_blog_series/sls-email-poc/node_modules/hapi/lib/defaults.js:52:25)
      at Module._compile (internal/modules/cjs/loader.js:1185:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)
      at Module.load (internal/modules/cjs/loader.js:1034:32)
      at Function.Module._load (internal/modules/cjs/loader.js:923:14)
      at Module.require (internal/modules/cjs/loader.js:1074:19)
      at require (internal/modules/cjs/helpers.js:72:18)
      at Object.<anonymous> (/Users/derekvincent/Dropbox/Development/aws_sap_blog_series/sls-email-poc/node_modules/hapi/lib/cors.js:7:18)
      at Module._compile (internal/modules/cjs/loader.js:1185:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)
      at Module.load (internal/modules/cjs/loader.js:1034:32)
      at Function.Module._load (internal/modules/cjs/loader.js:923:14)
      at Module.require (internal/modules/cjs/loader.js:1074:19)
      at require (internal/modules/cjs/helpers.js:72:18)
      at Object.<anonymous> (/Users/derekvincent/Dropbox/Development/aws_sap_blog_series/sls-email-poc/node_modules/hapi/lib/connection.js:16:14)
      at Module._compile (internal/modules/cjs/loader.js:1185:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)
      at Module.load (internal/modules/cjs/loader.js:1034:32)
      at Function.Module._load (internal/modules/cjs/loader.js:923:14)
      at Module.require (internal/modules/cjs/loader.js:1074:19)
      at require (internal/modules/cjs/helpers.js:72:18)
      at Object.<anonymous> (/Users/derekvincent/Dropbox/Development/aws_sap_blog_series/sls-email-poc/node_modules/hapi/lib/server.js:12:20)
      at Module._compile (internal/modules/cjs/loader.js:1185:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)
      at Module.load (internal/modules/cjs/loader.js:1034:32)
      at Function.Module._load (internal/modules/cjs/loader.js:923:14)
      at Module.require (internal/modules/cjs/loader.js:1074:19)
      at require (internal/modules/cjs/helpers.js:72:18)
      at Object.<anonymous> (/Users/derekvincent/Dropbox/Development/aws_sap_blog_series/sls-email-poc/node_modules/hapi/lib/index.js:5:16)
      at Module._compile (internal/modules/cjs/loader.js:1185:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)
      at Module.load (internal/modules/cjs/loader.js:1034:32)
      at Function.Module._load (internal/modules/cjs/loader.js:923:14)
      at Module.require (internal/modules/cjs/loader.js:1074:19)
      at require (internal/modules/cjs/helpers.js:72:18)
      at Object.<anonymous> (/Users/derekvincent/Dropbox/Development/aws_sap_blog_series/sls-email-poc/node_modules/serverless-offline-python/src/index.js:9:14)
      at Module._compile (internal/modules/cjs/loader.js:1185:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)
      at Module.load (internal/modules/cjs/loader.js:1034:32)
      at Function.Module._load (internal/modules/cjs/loader.js:923:14)
      at Module.require (internal/modules/cjs/loader.js:1074:19)
      at require (internal/modules/cjs/helpers.js:72:18)
      at requireServicePlugin (/usr/local/Cellar/serverless/1.68.0/libexec/lib/node_modules/serverless/lib/classes/PluginManager.js:26:10)
      at /usr/local/Cellar/serverless/1.68.0/libexec/lib/node_modules/serverless/lib/classes/PluginManager.js:127:20
      at Array.map (<anonymous>)
      at PluginManager.resolveServicePlugins (/usr/local/Cellar/serverless/1.68.0/libexec/lib/node_modules/serverless/lib/classes/PluginManager.js:124:8)
      at PluginManager.loadAllPlugins (/usr/local/Cellar/serverless/1.68.0/libexec/lib/node_modules/serverless/lib/classes/PluginManager.js:111:15)
      at /usr/local/Cellar/serverless/1.68.0/libexec/lib/node_modules/serverless/lib/Serverless.js:96:35
  From previous event:
      at Serverless.init (/usr/local/Cellar/serverless/1.68.0/libexec/lib/node_modules/serverless/lib/Serverless.js:94:8)
      at /usr/local/Cellar/serverless/1.68.0/libexec/lib/node_modules/serverless/bin/serverless.js:81:8
      at processImmediate (internal/timers.js:456:21)
      at process.topLevelDomainCallback (domain.js:137:15)
  From previous event:
      at Object.<anonymous> (/usr/local/Cellar/serverless/1.68.0/libexec/lib/node_modules/serverless/bin/serverless.js:71:4)
      at Module._compile (internal/modules/cjs/loader.js:1185:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)
      at Module.load (internal/modules/cjs/loader.js:1034:32)
      at Function.Module._load (internal/modules/cjs/loader.js:923:14)
      at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
      at internal/main/run_main_module.js:17:47
marcosfede commented 4 years ago

same error here, any solutions?

magdiel01 commented 4 years ago

@marcosfede as a temporary solution; I switched to node 8 with command n 8 and worked (see n repo)

aelzeiny commented 4 years ago

This is caused by a deprecation of tmpDir in node. You can read more about it here: https://stackoverflow.com/questions/40913034/

My solution was to do the following:

Works perfectly, but I just have to remember to do this everytime I npm install

DiogoTheCoder commented 4 years ago

@aelzeiny Could you update the dependencies to use @hapi/hapi, since hapi is now deprecated. Having to use serverless-offline instead of serverless-offline-python until this gets resolved 🤷

scripterkaran commented 4 years ago

👍 I am having the same issue, need to update hapi

aelzeiny commented 4 years ago

@DiogoTheCoder Sorry, I'm not a maintainer of this code-base, and I only used it once for a side-project when I ran into this issue. If I were the one to make the change I'll probably break something else. I just thought my comment would be helpful.

JaxT38 commented 3 years ago

This is caused by a deprecation of tmpDir in node. You can read more about it here: https://stackoverflow.com/questions/40913034/

My solution was to do the following:

  • open /broadcaster/node_modules/hapi/lib/defaults.js
  • Go to line 5 right where it says const Os = require('os');
  • Add line Os.tmpDir = Os.tmpdir;

Works perfectly, but I just have to remember to do this everytime I npm install

Yep, me too.

ananunaki commented 3 years ago

In some caes Windows 10 uses tmpDir ( with camel case ) so you can solve this issue like this

var isWin = process.platform === "win32"; var isLinux = process.platform === "linux"; var os = require('os');

if(isWin){ os.tmpDir = os.tmpDir; } else if (isLinux) { os.tmpDir = os.tmpdir; }

ajoslin103 commented 3 years ago

this will only be a problem if you use node > 12

pin your project to use node <= 12 with nvm

echo 'lts/erbium' > .nvmrc nvm use

ref: https://github.com/nvm-sh/nvm/blob/master/README.md

DiogoTheCoder commented 3 years ago

this will only be a problem if you use node > 12

pin your project to use node <= 12 with nvm

echo 'lts/erbium' > .nvmrc nvm use

ref: https://github.com/nvm-sh/nvm/blob/master/README.md

NodeJS 10 support will be ending very soon.

image

ajoslin103 commented 3 years ago

'NodeJS 10 support will be ending very soon'

very true

'lts/erbium' is Node 12 and good for a little longer

The serverless framework has issues & PRs referring to os.tmpDir so it shouldn't be a problem much longer

nyan-left commented 3 years ago

This may be helpful for anyone that stumbles across this in the future. An automated version of @ananunaki 's solution:

(I have only tested on Darwin)

// fix-sls-offline.js

const fs = require('fs');
const LINE_NUMBER = 6;
const FILE_LOCATION = 'node_modules/hapi/lib/defaults.js'
const INJECT_FIX_STRING = `
var isWin = process.platform === "win32";
var isLinux = process.platform === "linux";
var isDarwin = process.platform === "darwin";
if (isDarwin || isLinux) {
  Os.tmpDir = Os.tmpdir;
} else if (isWin) {
   Os.tmpDir = os.tmpDir;
}
`

let data = fs.readFileSync(FILE_LOCATION)
if (data.includes(INJECT_FIX_STRING)) {
  console.log("Skipping fix injection, already exists.")
} else {
  data = data.toString().split("\n");
  data.splice(LINE_NUMBER, 0, INJECT_FIX_STRING);
  let text = data.join("\n");

  fs.writeFile(FILE_LOCATION, text, (err) => {
    if (err) {
      return console.log(err);
    } else {
      return console.log('Injected fix successfully')
    }
  });
}
// package.json

  "scripts": {
    "deploy": "serverless deploy",
    "start": "serverless offline start",
    "prestart": "node fix-sls-offline.js"
  }
asirihewage commented 3 years ago

adding Os.tmpDir = Os.tmpdir; fixed the issue

rgabeh83 commented 3 years ago

I am having trouble with this and hoping someone could help me out. I have downgraded to node 12 but i am still have the same proplem. I tried to locate the "hapi/lib" file but there is no "lib" file. Any ideas?

AlexanderAlvarez9 commented 3 years ago

adding Os.tmpDir = Os.tmpdir; fixed the issue

where you add this?

Aiperikurenkeeva commented 3 years ago

question

This is caused by a deprecation of tmpDir in node. You can read more about it here: https://stackoverflow.com/questions/40913034/

My solution was to do the following:

  • open /node_modules/hapi/lib/defaults.js
  • Go to line 5 right where it says const Os = require('os');
  • Add line Os.tmpDir = Os.tmpdir;

Works perfectly, but I just have to remember to do this everytime I npm install

Should I add this as a new line 6 (did not work for me) or on line 5 next to the const Os = require('os'); ? line 5: const Os = require('os'); Os.tmpDir = Os.tmpdir; or line 5: const Os = require('os'); line 6 : Os.tmpDir = Os.tmpdir; which way you did it?

andrewsauter-nbcuni commented 3 years ago

@Aiperikurenkeeva both ways should work. Probably a bit cleaner to add Os.tmpDir = Os.tmpdir; to line 6.

sbjovanovski commented 3 years ago

This may be helpful for anyone that stumbles across this in the future. An automated version of @ananunaki 's solution:

(I have only tested on Darwin)

// fix-sls-offline.js

const fs = require('fs');
const LINE_NUMBER = 6;
const FILE_LOCATION = 'node_modules/hapi/lib/defaults.js'
const INJECT_FIX_STRING = `
var isWin = process.platform === "win32";
var isLinux = process.platform === "linux";
var isDarwin = process.platform === "darwin";
if (isDarwin || isLinux) {
  Os.tmpDir = Os.tmpdir;
} else if (isWin) {
   Os.tmpDir = os.tmpDir;
}
`

let data = fs.readFileSync(FILE_LOCATION)
if (data.includes(INJECT_FIX_STRING)) {
  console.log("Skipping fix injection, already exists.")
} else {
  data = data.toString().split("\n");
  data.splice(LINE_NUMBER, 0, INJECT_FIX_STRING);
  let text = data.join("\n");

  fs.writeFile(FILE_LOCATION, text, (err) => {
    if (err) {
      return console.log(err);
    } else {
      return console.log('Injected fix successfully')
    }
  });
}
// package.json

  "scripts": {
    "deploy": "serverless deploy",
    "start": "serverless offline start",
    "prestart": "node fix-sls-offline.js"
  }

Worked like a charm. Thank you very much :)