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

2024-09-28 03:17:28 发布

您现在位置:Python中文网/ 问答频道 /正文

使用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:\Users\userhere\Desktop\serenity-zero\serenity-zero-test\config.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:\Users\userhere\Desktop\serenity-zero\serenity-zero-test\src\settings.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:\Users\userhere\Desktop\serenity-zero\serenity-zero-test\src\settings.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脚本仍保留在原始文件夹中。在那之后,我犯了这个错误


Tags: 文件thesrc脚本文件夹confignodejson
1条回答
网友
1楼 · 发布于 2024-09-28 03:17:28

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

相关问题 更多 >

    热门问题