I am trying to import images I have stored in a folder called resources located under the server section.
When I place the images in the app folder on my local drive, it works fine, but since this has to be deployed to our live server, it makes things hard to have to copy files back and forth.
The error I get:
Error: ENOENT: no such file or directory, open '../../resources/cp_pdf_logo.png'
at Object.openSync (node:fs:585:3)
at Object.readFileSync (node:fs:453:35)
at RouteEmails.orderAuth (C:\Development\kaizen\server\routes\emails\file:\C:\Development\kaizen\server\routes\emails\func.js:187:43)
at RouteEmails.sendMail (C:\Development\kaizen\server\routes\emails\file:\C:\Development\kaizen\server\routes\emails\func.js:430:17)
at RouteOrders.orderAuthorizedMail (C:\Development\kaizen\server\routes\orders\file:\C:\Development\kaizen\server\routes\orders\func.js:840:24)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at RequestRouter._routeRequest (C:\Development\kaizen\server\socketController\file:\C:\Development\kaizen\server\socketController\requestRouter.ts:64:34)
at RequestRouter.processMessage (C:\Development\kaizen\server\socketController\file:\C:\Development\kaizen\server\socketController\requestRouter.ts:43:9)
at Socket. (C:\Development\kaizen\server\socketController\file:\C:\Development\kaizen\server\socketController\socketClient.ts:62:7) {
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path: '../../resources/cp_pdf_logo.png'
}
Here is an example of my code:
let ordercplogo = this.base64_encode("../../resources/cp_pdf_logo.png")
let orderthankyoulogo = this.base64_encode("../../resources/thankyou_logo.png")
// let ordercplogo = this.base64_encode("/app/resources/cp_pdf_logo.png");
// let orderthankyoulogo = this.base64_encode("/app/resources/thankyou_logo.png");
The commented-out section is what works, but from the image below you can see that I have these files stored inside the app.
Can anyone tell me what I'm doing wrong?
PS. I have also tried to import the images above with
const logo1 = require("../../resources/image.png")
but also doesn't work and throws another error.
Your function probably tries to find the image relative to the working directory. It makes a difference if you were to do cd /; node /app/index.js or cd /app; node index.js. The working directory may be different in your live environment than locally.
There are a number of functions to help you find the path to a file in the built-in path module.
Let's assume you're in a file called routes/companies/a.js:
const { resolve } = require('path');
this.base64_encode(resolve(__dirname, "../../resources/cp_pdf_logo.png"));
// Will find the absolute path to the file.
( path.relative documentation page )
In CommonJS files (files that use require()) the value __filename always refers to the file it's in, rather than the main script file (so /app/routes/companies.a.js in this example) and __dirname is the directory it's in. That's the same value, without the a.js part.
Related
I’m looking to allow a user to upload a javascript file containing a thread and then to spawn that thread, but I’m facing some issues with the device paths.
First I use the DocumentPicker to pick the file:
const pickerResult = await DocumentPicker.pickSingle({
type: [types.allFiles],
copyTo: 'documentDirectory'
});
Then I use the RNFS.readDir to get the file and its path comes back as /data/user/0/playground.com/files/my-thread.js.
Now, the react-native-threads expects a relative path like such new Thread('./worker.thread.js’);. If I try to pass the path received by RNFS I receive the following no such file error:
Error: ENOENT: no such file or directory, open '/data/user/0/playground.com/files/my-thread.js'.
I feel like this may be possible to avoid by using a Blob, but perhaps there is an easier way?
In Node.js, I'm attempting to use fs.mkdirSync() to create a new directory in a remote server/folder structure '\\myserver.com'
My computer (Windows) seems to be connected to this server, have permissions to access it, and is viewable and editable in my File Explorer. fs.mkdirSync() seems to have no issue creating folders locally on my machine, but when I put the server path as the directory, it throws an error
Code:
fs.mkdirSync("\\\\myserver.com\\folder1\\folder2\\new folder")
Error:
{ Error: EPROTO: protocol error, mkdir '\\myserver.com\folder1\folder2\new folder'
at Object.fs.mkdirSync (fs.js:885:18)
at exports.create_new_folder (/file_calling_mkDir.js:107:6)
errno: -71,
code: 'EPROTO',
syscall: 'mkdir',
path: '\\myserver.com\folder1\folder2\new folder' }
What seems to be going wrong here? The folder does not get created and there isn't much documentation on using fs with server paths for me to refer to, making this difficult to solve. Any help would be massively appreciated.
I'm studying Node.js right now, using the "Beginning Node.js" textbook.
The example in the book does not execute properly in the command prompt. (I'm using Ubuntu 18.04 with Node v9.4.0, and the book is three years old, so this may be related?)
In the downloadable source code for the book, this is the code that is provided:
var fs = require('fs');
// Create readable stream
var readableStream = fs.createReadStream('./cool.txt');
// Pipe it to out stdout
readableStream.pipe(process.stdout);
The file, cool.txt, is in the parent directory.
--parentFolder
----jsFileToExecute.js
----cool.txt
When I run node jsFileToExecute.js in the command prompt, I get this response:
events.js:137
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open './cool.txt'
As this source code is directly from the textbook publisher's website and it still doesn't run, I'm assuming there's something wrong with it?
Looking for solutions to figure out why this isn't working. I've looked at the documentation at nodejs.org, but didn't find any clues.
Thank you for any help!
To avoid paths issues, it's recommended to use path.join, like:
const path = require('path');
const coolPath = path.join(__dirname, 'cool.txt');
const readableStream = fs.createReadStream(coolPath);
With above example, you're creating a path to the file referring to the current directory of the script stored in global variable called __dirname.
if the file is in the same directory where currently you are running your project then you can simply type the name of the file instead of giving it the path like ./ or / like here:
var fs = require('fs');
// Create readable stream
var readableStream = fs.createReadStream('cool.txt');
// Pipe it to out stdout
readableStream.pipe(process.stdout);
It will work as will read the file from the same directory
I am using Golang (Echo) for my backend and React for my frontend. When I bundle my code using webpack, the file is created; however, I am getting an error in my console when I go to localhost:3000 stating the bundle file cannot be found. This is the exact error message: GET http://localhost:3000/build/app.bundle.js net::ERR_ABORTED.
Here is my server:
func main() {
env.SetEnvVars()
e := echo.New()
e.File("/", "server/static/index.html")
e.Logger.Fatal(e.Start(os.Getenv("PORT")))
}
Here is my webpack.config.js file:
module.exports = {
entry: './client/main.jsx',
output: {
path: path.resolve(__dirname, 'server/static/build'),
filename: 'app.bundle.js'
},
...
And the script tag in my index.html file is:
<script src="./build/app.bundle.js"></script>
The directory path regarding these files is currently:
/
server/
main.go
static/
index.html
build/
app.bundle.js
Any help would be appreciated!
The echo server you've set up only serves one single path, the root path ("/"), by rendering the contents of the index.html file. Because you haven't set up any other handlers for that server, any request to a path other than the root will result in 404, including those requests made from the index page via script and link tags, e.g.; <script src="./build/app.bundle.js"></script>.
To be able to serve a request to a path like "/static/build/app.bundle.js" for example you need to tell the server how to do that by registering a new handler.
With the echo server you can use its Static method to do that.
e.Static("/static", "static")
Please keep in mind that the links you use in html tags, the location of the corresponding files on your machine, and the location from where you launched your app matters if you use relative paths like ./build/app.bundle.js, and because of that the two arguments to e.Static may need to be somewhat different from the example here.
Here's a bit more info.
I am trying to build and run an executable in Gulp using Node. My executable builds fine, however attempting to run it gives this error:
Error: spawn EACCES
at exports._errnoException (util.js:1023:11)
at ChildProcess.spawn (internal/child_process.js:313:11)
at Object.exports.spawn (child_process.js:387:9)
at Gulp. (/path/to/my/app/gulpfile.js:167:24)
at module.exports (/path/to/my/app/node_modules/orchestrator/lib/runTask.js:34:7)
at Gulp.Orchestrator._runTask (/path/to/my/app/node_modules/orchestrator/index.js:273:3)
at Gulp.Orchestrator._runStep (/path/to/my/app/node_modules/orchestrator/index.js:214:10)
at Gulp.Orchestrator.start (/path/to/my/app/node_modules/orchestrator/index.js:134:8)
at Gulp. (/path/to/my/app/node_modules/gulp-sync/lib/index.js:51:27)
at module.exports (/path/to/my/app/node_modules/orchestrator/lib/runTask.js:34:7)
Bear in mind that my script works perfectly fine when my gulpfile.js is in the same directory as the executable I am attempting to run.
But I have moved my gulpfile.js to one directory down, and in order to build/run the executable I have to change the cwd of the child process I am spawning to the directory above it. In doing so, even though the executable still builds fine, the running of it keeps giving me this error and I can't find any help online.
I tried to set executable permissions on the file, thinking it was a permissions problem, but that didn't do anything.
I also tried putting the logic to run the executable in a shell script and run the shell script using the child process instead (cause why not), but that didn't help either.
Here's the code:
// Spawn application server
if (os.platform() === 'win32') {
// server = child.spawn(appName + '.exe') <-- USED TO WORK
server = child.spawn(appName + '.exe', [], {
cwd: mainDotGoPath
});
} else {
// server = child.spawn('./' + appName) <-- USED TO WORK
server = child.spawn('./' + appName, [], {
cwd: mainDotGoPath
});
}
The path is fine, by the way, because I build the executable using a similar syntax and it builds just fine:
var build = child.spawnSync('go', ['build'], {
cwd: mainDotGoPath
});
Anyone have any ideas?