You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/06/11 17:14:56 UTC
[GitHub] csantanapr opened a new issue #57: CWD is not the root of the zip
action
csantanapr opened a new issue #57: CWD is not the root of the zip action
URL: https://github.com/apache/incubator-openwhisk-runtime-nodejs/issues/57
Problem when your action wants to reference another file in your zip, locally this is easy because the node process usually get's started from the root of the project where the `package.json` is located.
For example the following code works fine in my local computer.
Since I'm running the node process locally
```javascript
const fs = require('fs')
exports.main = () => {
const cwd = process.cwd()
const dirname = __dirname
//in openwhisk need to find the root of the zip
console.log(`dirname= ${dirname}`)
console.log(`cwd= ${cwd}`)
const file = fs.readFileSync('secondfile.txt',{encoding:'utf8'})
console.log(file)
return {file, cwd, dirname}
}
```
I run this code in openwhisk I get an error about not found
In openwhisk currently the current working directory (cwd) is set to where the nodejs process started `/nodejsAction`
This is different from using for example dockerskeleton or python action, where the cwd is changed to the tmp directory where the the zip action get's unzip.
For now the workaround is to use `__dirname` like `${dirname}/secondfile.txt` instead of `secondfile.txt`
```javascript
const fs = require('fs')
exports.main = () => {
const cwd = process.cwd()
const dirname = __dirname
//in openwhisk need to find the root of the zip
console.log(`dirname= ${dirname}`)
console.log(`cwd= ${cwd}`)
const file = fs.readFileSync(`${dirname}/secondfile.txt`,{encoding:'utf8'})
console.log(file)
return {file, cwd, dirname}
}
```
The fix is in runner.js to change the process directory `process.chdir()` to the directory where the zip get's extracted
cc @rabbah
https://github.com/apache/incubator-openwhisk-runtime-nodejs/blob/master/core/nodejsActionBase/runner.js#L59
```
process.chdir(moduleDir)
thisRunner.userScriptMain = eval('require("' + moduleDir + '").' + message.main);
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services