0

I am novice in Javascript and i want to test a website using JavaScript. My Javascript file has two function Login and Some Function

Here, Some Function has space in between and i want to call this function via npm run command.

File somefile.js

module.exports = {
    "Login": function (browser) {
        console.log("Hi --> Pawan");
    },
    "Some Function": function (browser) {
        console.log("Some Script data");
    }
};

File package.json

{
  "name": "sampletest",
  "version": "1.0.0",
  "description": "",
  "main": "somefile.js",
  "scripts": {
     "loginScript": "node -e \"require('./someFile').Login()\"",
     "somefuncScript": "node -e \"require('./someFile').\"Some Function()\"\""
  },
  "author": "",
  "license": "ISC"
}

Commands used are :-

npm run loginScript returns Hi --> Pawan

npm run somefuncScript returns BLANK

CMartins
  • 3,247
  • 5
  • 38
  • 53
Pawankumar Dubey
  • 387
  • 1
  • 6
  • 21
  • Please confirm me again how it is duplicate? i am not calling function from js file but from json file. Please remove duplicate marking. @CertainPerformance – Pawankumar Dubey Aug 23 '19 at 07:45
  • You're trying to use `require('./someFile')."Some Function()"`, but that syntax isn't valid - see the canonical to see how to use spaces when looking up property names – CertainPerformance Aug 23 '19 at 07:47
  • I have already tried with %20 in between and even unicode character U+0020 in between but that didn't worked – Pawankumar Dubey Aug 23 '19 at 07:48
  • See the linked question - this has nothing to do with URL encoding – CertainPerformance Aug 23 '19 at 07:50
  • Please remove duplicate mark and let some professional to answer it for me. Thanks, link was not helpful for my understanding – Pawankumar Dubey Aug 23 '19 at 07:52
  • 1
    Look at the first example in the top answer in the linked question - it shows how to look up properties which can't be accessed with dot notation – CertainPerformance Aug 23 '19 at 07:55
  • I resolved it not using your link but a different link. This works now fine with "somefuncScript": "node -e \"require('./someFile')['Some Function']()\"" Please remove duplicate so that other dont face same issue, i want to answer this – Pawankumar Dubey Aug 23 '19 at 08:05
  • That looks to be exactly the solution described in the top answer in the linked question - putting the quoted property inside of brackets – CertainPerformance Aug 23 '19 at 08:06
  • Nope that is not the answer but solved using below one. Yours Duplicate marking is similar one which hints to possible solution but not exact answer https://stackoverflow.com/questions/21724158/property-names-with-spaces-in-object-literals – Pawankumar Dubey Aug 23 '19 at 08:51

0 Answers0