cannot access javascript file even the path is correct - javascript

It seems to be a common problem with Nodejs, and i tried all the solution without success.
My require('filepath') request fail, with the error " ENOENT: no such file or directory, open "a filepath"
For example i have those folders:
i want to use action1.js and data.json into folder2 action2.js
So while require with relative path don't work (why ?):
const action1 = require('../folder1/action1.js')
const data = require('../../data/data.json')
I tried absolute path, but that do not worked too
const action1 = require(path.join( __dirname,'../folder1/action1.js'))
const data = require(path.join( __dirname,'../../data/data.json'))
What am i doing wrong ?

The code looks correct, but I suggest you check the scope of your directory.
You can try the following steps to see your directory structure from action2.js
npm install directory-tree
Inside your file add the following lines:
const dirTree = require("directory-tree");
const tree1 = dirTree('../');
console.log(tree1);
const tree2 = dirTree('../../');
console.log(tree2);
This might help you find the scope of your directory. It's not a solution, but a way of helping you debug.

Ok, i have found my problem. And it was coming from an other require on an other file. My code here was correct.
Sorry for the dumb question, be indulgent i'm noob, and thank you for the answer.

Related

Why special charactors got replaced when imported using 'require' in NodeJS?

I'm new to JavaScript and NodeJS. So please don't condemn me if this issue is obvious.
My outsourced file is a simple config file. Here is a short version of it.
config.js:
var config = {};
config.Web = {};
config.Web.Title = 'Title with öüöäéàè';
module.exports = config;
The config.js gets loaded into my app.js with that code:
var configread = require('./views/config/config');
All special-characters get replaced by a � as visible on console:
var WebsiteTitle = configread.Web.Title;
console.log(WebsiteTitle);
Strings defined in the app.js script itself doesn't have this behavior. So i think the problem must be in the way of loading my config.js into my app.
Does someone have a solution for this behavior?
After some over-the-night-research i got the answer by myself.
The config.js file was created with Windows-Notepad. From there it got the ANSI-Encoding. It seems like NodeJS can't handle special-characters when config.js isn't encoded in UTF-8. I had to place the code in an UTF-8 encoded config.js file to solve the problem.
Thanks for any prior help!

Resolve relative path relative to another path instead of current working directory

I have a Node.js module that looks like this:
module.exports = function()
{
// linked dependencies
this.dependency1 = 'dependency1/dependency1.exe';
this.dependency2 = 'dependency2/dependency2.exe';
this.dependency3 = 'dependency3/dependency3.exe';
}
I want developers to be able to easily edit the location of the dependencies relative to the module file itself. However, when using the module, the current working directory process.cwd() is usually not the same as the module directory, so those paths do not resolve correctly. path.resolve() only seems to work relative to the current working directory, and doesn't have any arguments to allow for a custom reference point/path.
I have been able to resolve the paths in the following way, but I find it ugly and cumbersome, and it should be easier than this:
this.ResolvePath = function(p)
{
var cwd = process.cwd();
process.chdir(path.dirname(module.filename));
var resolvedPath = path.resolve(p);
process.chdir(cwd);
return resolvedPath;
}
Is there a cleaner way to do this? I feel like path.relative() should hold the solution, but I can't find a way to make it work. Perhaps chaining together multiple path.relative()s could work, but I can't wrap my brain around how that would work right now.
why not just:
path.resolve(__dirname, p)
__dirname works a bit differently and returns the current modules path, which can then be joined easily with the relative path.

my config.js file is not being recognized

In my index.js file, I have const config = require('config'); written as one of the first lines.
And I have a file in my project folder called config.js
But I keep having my console tell my that it Cannot find module 'config'
My config file is this basically:
module.exports = {
'secretKey': 'mySecretCode12232',
'mongoUrl' : 'mongodb://localhost:27017/test'
};
This doesn't make any sense it should be working.
const config = require( path.join(__dirname, 'config'+'.js' ) );
I also have own function which loads atomaticaly from specified subdirectory at it's definition, it saves a lot of time.
When you don't provide any path selector in the require statement (eg. require('./config')), your code will search for the package named config and fail as it cannot find this specific one, as require will assume that it was the package name that was provided (and will start searching e.g. in your node_modules etc. - search path for it is not a trivial topic :) ).
If you want to require the module from another file, you have to provide a correct path to it, so assuming your config.js resides in the same catalog as your other file, the correct statement would be:
const config = require('./config'); // Extension can be omitted

export node module to local error

i am a newby on node.js. i want to parse a xml file into json .so i am trying to use bulebutton library from https://github.com/blue-button/bluebutton.js .
first i have installed the module by command npm install bluebutton and its created a node_modules folder with bluebutton module.
now i created a test.js file with following code
var bb = require('bluebutton');
var myRecord = bb.BlueButton('./asd.xml');
console.log(myRecord);
but its gave me an error that bluebutton is not define .please help me to figureout this problem thanks
REVISED ANSWER
From bluebuttonjs.com/docs, the require statement you use would return the BlueButton object, so bb represents said object, and it would be called like so
var myRecord = bb('someFile.xml');
However you might also note that they use fs to read the file before passing it. http://www.bluebuttonjs.com/docs/#parsing-node
PREVIOUS ANSWER (for wrong module)
According to their npm docs, you need to do
var bb = require('blue-button');
https://www.npmjs.com/package/blue-button

AWS Lambda Function is returning "Cannot find module 'index'" yet the handler in the config is set to index

As my title explains I am getting the following error:
{
"errorMessage": "Cannot find module 'index'",
"errorType": "Error",
"stackTrace": [
"Function.Module._resolveFilename (module.js:338:15)",
"Function.Module._load (module.js:280:25)",
"Module.require (module.js:364:17)",
"require (module.js:380:17)"
]
}
I have tried both solutions provided in creating-a-lambda-function-in-aws-from-zip-file and simple-node-js-example-in-aws-lambda
My config currently looks like:
and my file structure is:
and my index.js handler function looks like :
exports.handler = function(event, context) {
What else could be causing this issue aside from what was stated in those two answers above? I have tried both solutions and I have also allocated more memory to the function just incase thats why it couldn't run.
EDIT -
For the sake of trying, I created an even simpler version of my original code and it looked like this:
var Q = require('q');
var AWS = require('aws-sdk');
var validate = require('lambduh-validate');
var Lambda = new AWS.Lambda();
var S3 = new AWS.S3();
theHandler = function (event, context) {
console.log =('nothing');
}
exports.handler = theHandler();
And yet still does not work with the same error?
Try zipping and uploading the contents of the folder lambda-create-timelapse. Not the folder itself.
If this was unclear for anyone else, here are the steps:
Step 1
Navigate to the folder of your project, and open that folder so that you are inside the folder:
Step 2
Select all of the images you want to upload into to Lambda:
Step 3
Right-click and compress the files you have selected:
This will give you a .zip file, which is the file you need to upload to Lambda:
There are a lot of ways to automate this, but this is the manual procedure.
I ran into this problem a few times myself, and this indeed has to do with zipping the folder instead of just the contents like you're supposed to.
For those working from the terminal...
While INSIDE of the directory where the .js files are sitting, run the following:
zip -r ../zipname.zip *
The * is instructing the client to zip all the contents within this folder, ../zipname.zip is telling it to name the file zipname.zip and place it right outside of this current directory.
I had the same problem sometime ago - I reformatted the code.
function lambdafunc1(event, context) {
...
...
...
}
exports.handler = lambdafunc1
The problem occurs when the handler cannot be located in the zip at first level. So anytime you see such error make sure that the file is at the first level in the exploded folder.
To fix this zip the files and not the folder that has the files.
Correct Lambda function declaration can look like this:
var func = function(event, context) {
...
};
exports.handler = func;
You may have other syntax errors that prevent the index.js file from being properly ran. Try running your code locally using another file and using the index.js as your own module.
make sure in your handler following code added
exports.handler = (event, context, callback) => {
...
}
Another reason this can occur is if you don't do an npm install in the folder before packaging and deploying.

Categories