简体   繁体   中英

NodeJS Javascript piece code running on Windows but not on Linux

I am particularly new to NodeJS and Javascript and have created a small web Application. I have my code running on Windows machine seamlessly. However, When I tried running it on a Linux VM it blocked at a point with no error or Exceptions thrown.

I discovered there was a line which actually caused a blockage in Linux, commented it out and the code continued from there on limiting the Application's functionality which greatly depends on that line with the other independent parts being functional.

....
var localEntry = entry.split('\\')
if(!localEntry)
localEntry = entry.split('/')

localEntry = localEntry[localEntry.length -1]

this.scripts[extn][localEntry].day1Vars = searchedVars[entry].day1Vars
}

I was extremely puzzled to find the exact same code running seamlessly on a Windows machine and blocking on a Linux vm due to a single line of code and am wondering how that could be ? The line which is responsible for such behavior is : this.scripts[extn][localEntry].day1Vars = searchedVars[entry].day1Vars

My expectation is code on Node.js is platform independent and the thought of a line of code causing such difference is revolting. I am using Windows 10 and RHEL 6.9 with 8GB of ram in both.

Could someone guide me if I am missing something or what has gone wrong? Any help is greatly appreciated.

As requested by folks here, Sample values(on Windows) :

entry : "d:\NodeProjects\BApp\uploads\bp\bp\scripts\nodejs\set-nodejs-root.sh"
localEntry : "set-nodejs-root.sh"

When you specify a path under windows you have to use "\\"
Linux uses "/" for path

Its better to check what is the environment. For example you should split in:

ubuntu .split("/")

windows .split("\\\\")

Looks like you are working with paths, the best recommendation here is to use the path module to handle those routes for you:

https://nodejs.org/api/path.html

Here is a good explanation on how to handle routes for both systems:

https://nodejs.org/api/path.html#path_windows_vs_posix

I would use something like

https://nodejs.org/api/path.html#path_path_parse_path

To parse the paths correctly or if you need to construct paths you can use:

https://nodejs.org/api/path.html#path_path_join_paths

Check all different options there, I am 99% sure that you will find the proper method for your use case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM