简体   繁体   中英

Can't find module: Python, NodeJS, and fs module

With Python, I make a script that asks for input (an action). The second option runs a NodeJS file called settings.js in a new terminal window with:

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

In the NodeJS settings.js file, it basically uses fs to overwrite a key's value in config.json . In this case, change the key "PREFIX" to whatever the user inputs (eg ! ).

When it first reads the config.json file, it shows an error.

Code:

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

Error:

[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'
}

I figured that this happened as it originally ran from the python script, so the directory folder was outside of the NodeJS file (the python script's folder directory). I tried:

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

and,

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

But, I got:

(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.

Just so you know , this worked perfectly fine when the python script was in the same folder as the node.js files. I wanted to make it more organized so I put the node.js files in a src folder. The python script remained in the original folder. After that, I got this error.

Just fixed it, all I had to do was put the config.json in the same directory as the launch.py file. I don't think it's possible for the config.json to be in the src folder, but atleast it works.

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