找不到模块:Python、NodeJS 和 fs 模块



使用Python,我制作了一个请求输入(操作(的脚本。第二个选项在一个新的终端窗口中运行一个名为settings.js的NodeJS文件,其中包含:

subprocess.call('start node src/settings.js', shell=True)`

在NodeJSsettings.js文件中,它基本上使用fs来覆盖config.json中键的值。在这种情况下,将键"PREFIX"更改为用户输入的任何内容(例如!(。

当它第一次读取config.json文件时,会显示一个错误。

代码:

await fs.readFile('config.json', 'utf8', async (err, data) => {
// code to overwrite (with given data)
}

错误:

[Error: ENOENT: no such file or directory, open 'C:UsersuserhereDesktopserenity-zeroserenity-zero-testconfig.json'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\Users\userhere\Desktop\serenity-zero\serenity-zero-test\config.json'
}

我认为这是因为它最初从python脚本运行,所以目录文件夹在NodeJS文件(python脚本的文件夹目录(之外。我试过了:

await fs.readFile('src/config.json', 'utf8', async (err, data) => {
// code
});

和,

await fs.readFile('./src/config.json', 'utf8', async (err, data) => {
// code
});

但是,我得到了:

(node:17348) UnhandledPromiseRejectionWarning: Error: Cannot find module './src/config.json'
Require stack:
- C:UsersuserhereDesktopserenity-zeroserenity-zero-testsrcsettings.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at C:UsersuserhereDesktopserenity-zeroserenity-zero-testsrcsettings.js:29:87
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:63:3)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:17348) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:17348) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

正如您所知,当python脚本与node.js文件位于同一文件夹中时,此操作非常正常。我想让它更有条理,所以我把node.js文件放在src文件夹中。python脚本保留在原始文件夹中。在那之后,我犯了这个错误。

刚刚修复了它,我所要做的就是将config.json放在与launch.py文件相同的目录中。我认为config.json不可能在src文件夹中,但至少它可以工作。

最新更新