通过node.js在Mac上运行js时出错。怎么修?

时间:2018-09-01 13:13:31

标签: node.js command-line-interface

我已经在文本编辑器中创建了一个.js文件,并尝试通过节点js运行它:

var pizzaDoge = require('fs')
pizzaDoge.writeFile("/index.html", "<h1>doge is fat</h1>", function(error) {
  if (error) {
    return console.log(error)
  } else {
    return console.log("you fed the doge!")
  }
})

当我尝试在命令行软件中使用“节点index.html”使用节点运行程序时,会弹出

{ Error: EACCES: permission denied, open '/index.html'
errno: -13,
code: 'EACCES',
syscall: 'open',
  path: '/index.html' }

以防错误编号与使用Mac的计算机有关的问题。感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在Mac上,根文件夹(public class SyncDataToServerService extends Service { private static final String TAG = "SyncDataToServerService"; private ServerCall serverCall; private SharedPreferences tempPref2; private Timer timer; LocationManager locationManager; UtilityClass utilityClass; PowerManager.WakeLock wakeLock; PowerManager powerManager; @Override public void onCreate() { Log.e(TAG, "onCreate"); serverCall = new ServerCall(this); timer = new Timer(); utilityClass = new UtilityClass(this); tempPref2 = getSharedPreferences(getString(R.string.temp_pref_name_2), Context.MODE_PRIVATE); } @SuppressLint("MissingPermission") @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); Log.e(TAG, "onStartCommand"); powerManager = (PowerManager) getSystemService(POWER_SERVICE); if (powerManager != null) wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyApp::MyWakelockTag"); wakeLock.acquire(); //initialize and start Location service startLocation(); //initialize and start the TimerTask's job startTimer(); return START_STICKY; } @SuppressLint("MissingPermission") private void startLocation() { locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (locationManager != null) locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 10, new MyLocationListener()); if (locationManager != null) locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 10, new MyLocationListener()); } @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onDestroy() { Log.e(TAG, "onDestroy"); if (locationManager != null) locationManager.removeUpdates(new MyLocationListener()); super.onDestroy(); } private void syncPath(Location mLocation) { Log.w(TAG, "Location: " + mLocation + " Latitude: " + mLocation.getLatitude() + " & Longitude: " + mLocation.getLongitude()); if (tempPref2.getString(getString(R.string.attendance_key), "").equalsIgnoreCase("present")) { serverCall.sendEmpPath(mLocation); } } private void startTimer() { TimerTask timerTask = new TimerTask() { @RequiresApi(api = Build.VERSION_CODES.KITKAT) public void run() { if (utilityClass.isInternetConnected()) { if (serverCall != null) { if (serverCall.requestQ.size() > 0) Log.e(TAG, "Request queue size ==" + serverCall.requestQ.size()); serverCall.syncData(); } } } }; //schedule the timer, to wake up every 1 second timer.schedule(timerTask, 1000, 1000); } private class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location location) { Log.e(TAG, "onLocationChanged: " + location); Intent intent1 = new Intent("com.example.mc_project"); intent1.putExtra("location", location); sendBroadcast(intent1); if (location.getAccuracy() < 25) syncPath(location); wakeLock.release(); } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } } }` )需要具有root权限才能访问

请尝试使用/代替./index.html在当前文件夹下创建文件