Nodejs Forever detected script exited with code: null Nodejs path.existsSync is now called `fs.existsSync`.

Nodejs Forever detected script exited with code: null
Nodejs path.existsSync is now called fs.existsSync.

Forever detected script exited with code: null path.existsSync is now called fs.existsSync.

这是个什么错?

4 回复

Node.js Forever detected script exited with code: null

Node.js path.existsSync is now called fs.existsSync.

问题描述

当你在运行一个使用 forever 来管理的 Node.js 脚本时,可能会遇到以下错误信息:

Forever detected script exited with code: null
path.existsSync is now called fs.existsSync.

这条消息意味着你的脚本已经退出了,并且 forever 检测到了这一点。同时,它还提示你 path.existsSync 方法已经被废弃,现在应该使用 fs.existsSync

原因分析

  1. 脚本退出Forever detected script exited with code: null 表明脚本正常退出,但没有返回任何特定的退出码。
  2. 方法更名path.existsSync 现在应该使用 fs.existsSyncpath 模块中并没有 existsSync 方法,这可能是由于拼写错误或混淆导致的。

解决方案

为了修复这个问题,你需要更新你的代码,将所有出现的 path.existsSync 替换为 fs.existsSync。以下是具体的步骤和示例代码:

  1. 替换方法调用
    • 在你的代码中找到所有使用 path.existsSync 的地方。
    • 将其替换为 fs.existsSync

示例代码

假设你有以下代码片段:

const path = require('path');

if (path.existsSync('/some/path')) {
    console.log('Path exists');
}

你应该将其修改为:

const fs = require('fs');

if (fs.existsSync('/some/path')) {
    console.log('Path exists');
}

总结

通过将 path.existsSync 替换为 fs.existsSync,你可以解决这个警告。同时,确保检查你的脚本是否有其他潜在的退出原因,以避免 forever 持续重启脚本。如果脚本总是退出且没有明确的错误信息,可能需要进一步调试脚本逻辑。


从报错的堆栈信息来看,是/home/root/cloud9/plugins-server/cloud9.sourcemint/sourcemint.js使用了过期函数path.existsSync,不过这只是warning级别的错误,不应该导致程序崩溃。总之,先尝试把sourcemint.js中的path.existsSync替换成fs.existsSync吧。

这段信息表明你的 Node.js 脚本通过 Forever 监控器运行时意外退出了。同时,它还提醒你 path.existsSync 已经被弃用,现在应该使用 fs.existsSync

关于 Forever detected script exited with code: null

这通常意味着你的脚本因为某种原因停止了运行,但没有提供具体的退出码(因此显示为 null)。常见的原因包括脚本中的未捕获异常、资源耗尽或配置错误等。你可以通过查看 Forever 的日志来获取更多信息,以了解脚本停止的确切原因。

关于 path.existsSync is now called fs.existsSync

Node.js 中的文件系统模块已经将 path.existsSync 方法移动到了 fs 模块中,因此你需要更新代码以使用新的方法。如果你之前使用的代码是:

const { existsSync } = require('path');

你应该修改为:

const { existsSync } = require('fs');

示例代码

假设你有一个简单的 Node.js 应用,检查某个文件是否存在,以下是修改前后的代码对比:

修改前

// 存在问题的代码
const { existsSync } = require('path');

if (existsSync('./example.txt')) {
  console.log('File exists.');
} else {
  console.log('File does not exist.');
}

修改后

// 正确的代码
const { existsSync } = require('fs');

if (existsSync('./example.txt')) {
  console.log('File exists.');
} else {
  console.log('File does not exist.');
}

以上就是关于这个问题的解答,确保你的脚本正确处理异常,并使用 fs.existsSync 替换 path.existsSync

回到顶部