Skip to content

Instantly share code, notes, and snippets.

@nyan-left
Last active January 27, 2021 14:43
Show Gist options
  • Save nyan-left/892e934f36692d7eb6e170c4e590c446 to your computer and use it in GitHub Desktop.
Save nyan-left/892e934f36692d7eb6e170c4e590c446 to your computer and use it in GitHub Desktop.
Fix for TypeError: Os.tmpDir is not a function (https://github.com/alhazmy13/serverless-offline-python/issues/18 )
const fs = require('fs');
const LINE_NUMBER = 6;
const FILE_LOCATION = 'node_modules/hapi/lib/defaults.js'
const INJECT_FIX_STRING = `
const isWin = process.platform === "win32";
const isLinux = process.platform === "linux";
const 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')
}
});
}
@nyan-left
Copy link
Author

usage:

// package.json

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment