the hierarchy of my files looks something like:
BACKEND(COMPLETE)
->Routers
->userrouter.js
->login.html
I wanted the access of my login.html file in userrouter.js for which I copied the Path(absolute) of the login.html file.
But I am getting this error:
path must be absolute or specify root to res.sendFile()
My Code:
function loginUser(req,res)
{
res.sendFile('C:\Users\ASUS\Desktop\backend(complete)\login.html');
res.end();
}
Try modifying C:\Users\ASUS\Desktop\backend(complete)\login.html to C:/\Users/\ASUS/\Desktop/\backend(complete)/\login.html
Try using path (path.join) npm package which Node provides out of the box to avoid this confusion irrespective of the OS.
Try the Npm package path and use path.join(["yourpath", "here") for cross system compatibility. Note that every part between slashes needs to be its own entry in the array.
Related
This is my project's filesystem.
root
index.js
package.json
...etc
commamds
- ping.js
- eval.js
- ...etc
This is a normal discord.js bot.
But when I try reloading the commands, I use the following code:
...etc
let pull = require(`./${file}`);
// file is command files from fs.readdirSync() and it can be 'ping.js', 'eval.js', ...
...etc
But it throws a referenceerror that the module can't be found. But when I try fs.readFile(), it works. What's the problem?
fs.readFile() defaults to the current working directory if there is no path or if there's a relative path on the filename.
require() has a completely separate set of rules for where it looks for files. For example, a filename with no path at all looks in the node_modules directory and in the global module location(s). A filename starting with ./ looks in the current module's home directory. And so on... It's a different set of rules than fs.readFile().
Since you don't show us what file actually is, it's hard to know precisely, but perhaps you need to combine the filename with the appropriate path so you are giving require() a full path name and it will go exactly there, not use the normal rules for where require() looks when given only a plain filename.
I would like to use valid-url to validate some URLs using JSFiddle so that I can share it later. I tried adding a link to the index.js file from Github (https://raw.githubusercontent.com/ogt/valid-url/master/index.js) but Fiddle says:
Github is not a CDN, using it as such will cause issues with loading the file. Do you still with to it?
And obviously when I try to use it, an error is thrown:
Refused to execute script from [...] because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.
So, is there any way to use npm packages in a JSFiddle? Or any workaround to achieve this?
Use unpkg.com. They allow you to load any npm module from the browser like a CDN.
Using UNPKG you can load any file from any package following this pattern
https://unpkg.com/:package#:version/:file
So, if you add
https://unpkg.com/valid-url#1.0.9/
Then you'll get this (as shown in the next image)
If you specify the index.js file
https://unpkg.com/valid-url#1.0.9/index.js
Then you'll get this (as shown in the next image)
You'll then be able to use it in your fiddles by adding the Resources you want to.
If you want to import something from npm on the frontend, you can use https://www.skypack.dev/ which converts npm modules to ES Modules, example:
<script type="module">
// Use 'https://cdn.skypack.dev/' + 'npm package name' + '#version its optional'
import random from 'https://cdn.skypack.dev/canvas-sketch-util#1.10.0/random'
console.log('A random number: ', random.range(1,10))
</script>
Here's a JSFiddle using SkyDev to load an npm module.
There are many cases that https://unpkg.com/ wouldn't handle that https://www.skypack.dev/ can, so I recommend using it whenever it's on the front-end
I wrote a slightly more complete answer here about this:
How can I use a CommonJS module on JSFiddle?
I am using Browserify (http://browserify.org/) to load a module in JavaScript. I keep getting the following error:
I have no idea why this is happening. I have a "package.json" file in a directory called "wordnet-develop", which is located in the same location as the JavaScript file.
Originally I thought that there might be a path problem. However, I did the same exact thing but with a test.js file, and it worked. So, I think that there may be something wrong with using package.json.
The beginning of the package.json file:
The beginning of my JavaScript file:
The directory containing the javascript file:
The directory (seen above as "wordnet-develop")containing the package.json file:
UPDATE
I replaced var WordNet = require('./wordnet-develop/node-wordnet'); with var WordNet = require('./wordnet-develop/lib/wordnet'); as suggested by klugjo.
It may have worked, but now I am getting a new error message:
This happened again but with 'async' module missing. I checked lib/wordnet, and it included requirements for bluebird and async, so that's probably the error source.
However, I now have no idea what to do. I'm new to node.js and modules, so I'm unfamiliar with solutions. Am I supposed to parse all of the code and find all the required modules online? Shouldn't they have been included in the module? Is the problem that I'm trying to use a node.js module in vanilla JavaScript?
I don't think what you are trying to do is supported: you have to link directly to the entry javascript file of the node-wordnet library.
Replace
var WordNet = require('./wordnet-develop/node-wordnet');
With
var WordNet = require('./wordnet-develop/lib/wordnet');
I have a project in meteorjs that is using the nodes filesystem to read file, but I am not able to locate the file to be read.
My file Location
Server
- startup
- app.load.coffee
- myfileToBeRead.txt
My try in app.load.coffee
fs = Npm.require('fs')
console.log fs.readFileSync 'server/startup/myfileToBeRead.txt'
I am not able to read the file as it says
Error: ENOENT, no such file or directory 'server/startup/myfileToBeRead.txt'
I think since meteor merges everything in a js file, I have to add full path to the file.
I have tried other paths aswell (with the full path, without the full path). Can you point me out to the correct direction here?
Thank you
Well with the answer from David, I also found that I could do this with the assets/app directory of the project. All I had to do was add the file to a directory named private. This would also help me write to a file inside the directory aswell.
fs = Npm.require('fs')
console.log fs.readFileSync "assets/app/myfileToBeREad", 'utf8'
if the file should be checked in
This is the easy case - just place the file in your private directory and access it with the assets api. For more examples, see my blog post on exactly this subject.
if the file should exist somewhere else on the server
Use an absolute path to a directory not associated with your project, e.g. /tmp or /home/foo/bar. Directories inside of a meteor project get jumbled up after you bundle and deploy your app, so their existence can't be counted on. Using your example above it should work if you do something like:
var fs = Npm.require('fs');
fs.readFileSync('/tmp/myfileToBeRead.txt');
This should be a very simple question. I think it might be as simple as just being the convention, but I would like to check as I have no idea if there is anything else behind it or what phrases to even search regarding it.
var hello = require('./hello');
hello.world();
Imagine the above code. The require path is prefixed by a ./ this is always present for files in the same folder. Why not just the file name?
Comparitively the common use
var http = require('http');
Is not prefixed by a ./ I am currently assuming this is due to the http file being a "native" module. Therefore would I be right in saying that anything without ./ is looking in the native Node namespace and anything with a ./ is looking for a local file?
Also would a file in a higher directory like in PHP it would be ../
In Node would it be .././ or ./../
Yeah, this is a simple convention used in node. Please see the module.require docs
And for what it's worth, you won't always be using require("./hello"). Sometimes you'll be using require("../../foo") or require("../").
Simply put,
You use a path for requiring files within your module
You use a string identifier for including other modules