Change node path to current directory - node.js

I was trying to run this command from my user directory
NODE_ENV=~/Public/project node socket.js
But it return this error
Error: Cannot find module '/home/user/socket.js'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:418:7)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:533:3
The thing is i do not want to change directory. Is it possible to do that? And why this is happening?

NODE_ENV is, by convention, the type of environment you're running in, e.g. Development or Production or Test. It only matters for modules that inspect it and set options accordingly, e.g. Many loggers will default to more verbose in development.
Not really sure why you wouldn't just change directories, but your call to server.js can be an absolute or relative path. If the file is in the /home/user/project folder, you can (from /home/User) just call
node ./project/socket.js

After a little mor research, i found this
And according to that site, i have to do this
export NODE_CONFIG_DIR=/home/user/Public/project/config
and run this
node ./Public/smart-backwall-server/socket.js

Related

NestJS microservices "Cannot find module"

So, I'm trying to create my first microservice using NestJS, but the moment I try to run it, the service stops with this error:
[13:39:21] Found 0 errors. Watching for file changes.
Error: Cannot find module 'C:\Users\voryi\IdeaProjects\YWA\des_server\services\learning-service\dist\main'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
there's no main.js file in the first level of your dist directory. You can define the entry file by adding this to your nest-cli.json:
"entryFile": "learning-service/src/main"
the default is main
try npm run build and then restart your service
I use monorepo setup and this issue happens whenever I start the nestjs server from non-root folder by mistake.
Usually, removing the dist folder and restarting the nestjs server works for me.
In my case, the main.js file was inside src folder so I had to change the script to:
"start:prod": "node dist/src/main"
maybe simply use this inside the project,
npm i #nestjs/microservices

Node JS error: Cannot find module '../build/jvm_dll_path.json'

I am receiving the following error after running npm start:
Error: Cannot find module '../build/jvm_dll_path.json'
at Function.Module._resolveFilename (module.js:555:15)
at Function.Module._load (module.js:482:25)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/opt/stature-gqdss/node_modules/java/lib
[nodemon] app crashed - waiting for file changes before starting...
Can you help me with this issue?
First, you should check if the server is already running and kill the existing server. If no other server is running, try reinstalling your dependencies and try again.
npm install
I have a hack here. You can simply create a file named jvm_dll_path.json inside node_modules\java\build and paste the path of server to this, between quotes like shown below..
";C:\\Program Files\\Java\\jdkX.X.X_XXX\\jre\\bin\\server"
For me running
node postInstall.js
in the node_modules/java folder worked.

Node process.env.VARIABLE_NAME returning undefined

I'm using environment variables on my mac to store some sensitive credentials, and trying to access them through Node. I added them into my environment profile with
export VARIABLE_NAME=mySensitiveInfo
When I use echo $VARIABLE_NAME I receive the correct output (my sensitive info).
However, when I am trying to access this same variable in Node with process.env.VARIABLE_NAME and try to print it out on the console, I get an undefined.
Other environment variables appear to be okay though. For example, when I console.log(process.env.FACEBOOK_CALLBACK_URL), it prints the correct value to my console. I added FACEBOOK_CALLBACK_URL a few days ago.
Do I have to restart my machine or something? Does it take a certain time before environment variables become available in Node? The closest answer I've seen on SO is this post, but nobody was able to figure out why it was happening.
nodemon.json file is only for setting nodemon specific configuration
So for create custom environment variables we can use dotenv package
First , Install dotenv package
npm install dotenv --save
after that create .env file in root and include environment variables as bellows
MONGO_ATLAS_PW=xxxxx
JWT_KEY=secret
Finally, inside your app.js file insert following after your imports.
require('dotenv').config()
Then you can use environment varibale like this
process.env.MONGO_ATLAS_PW
process.env.JWT_KEY
process.env.VARIABLE_NAME returns undefined because the Node.js execution environment does not know the newly added VARIABLE_NAME yet. To fix the issue, the Node.js execution environment (e.g. IDE) need to restart.
The following steps can be used to reproduce this issue:
Open IDE such as WebStorm and write a simple Node.js program: console.log(process.env.VARIABLE_NAME). It will print undefined as expected, as VARIABLE_NAME is not defined yet. Keep the IDE running, don't close it.
Open environment profile such as .bash_profile and add export VARIABLE_NAME=mySensitiveInfo in it.
Open system console and run source .bash_profile, so that the above export statement will be executed. From now on, whenever system console is opened, VARIABLE_NAME environment variable exists.
In system console, execute the Node.js program in step 1, it will print mySensitiveInfo.
Switch to IDE and execute Node.js program, it will print undefined.
Restart the IDE and execute Node.js program, this time, it will print mySensitiveInfo
I got a problem just now , and I use this solved it in webpack config
const plugins = [
new webpack.NoEmitOnErrorsPlugin(),
// after compile global will defined `process.env` this Object
new webpack.DefinePlugin({
BUILD_AT : Date.now().toString(32),
DEBUG: process.env.NODE_ENV !== 'production',
'process.env': {
'NODE_ENV': JSON.stringify(process.env.NODE_ENV || "development"),
'VARIABLE_NAME': JSON.stringify(process.env.VARIABLE_NAME)
}
})
]
For everyone who might have this issue in the future and none of the solutions above are working, it may also be because you're running the node <filename>.js in a subfolder or subdirectory, and since your .env file is in the root folder, processs.env.<variable> will always return undefined.
A simple way to check is to try the following code 👉
const test = require('dotenv').config()
console.log(test)
In the console we get the following error
{ error: Error: ENOENT: no such file or directory, open 'C:\Users\vxcbv\Desktop\voisascript-auth\model\.env'
at Object.openSync (node:fs:585:3)
at Object.readFileSync (node:fs:453:35)
at Object.config (C:\Users\vxcbv\Desktop\voisascript-auth\node_modules\dotenv\lib\main.js:72:42)
at Object.<anonymous> (C:\Users\vxcbv\Desktop\voisascript-auth\model\db.js:1:32)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47 {
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path: 'C:\\Users\\vxcbv\\Desktop\\voisascript-auth\\model\\.env' } }
Emphasis on the Error: ENOENT: no such file or directory, open.
To solve this, just navigate back to the root and run the file from there, but specify the full path of the file from the root, like 👉
node /<subfolder>/<file.js>
please check the dir of .env file, If it is in same file as your app.js or (abc.js) than move .env to one level up
Close the code Runner Or Ide environment once and reopen it.
If you use VS code, close it completely. (Or CMD or Power Shell or ...)
I had the same issue. In my case the env file and db.js was inside a subfolder configs.
So, in index/server.js , while importing the dotven , I used
require('dotenv').config({path: './configs/.env'});
This way my env variables were being accessed.
Hope this example of mine helps you! 😀

Mongoose on c9 how to fix

I didn't close my mongod when closing the laptop and now its giving me an error. On top of that, I delete the mongod data directory with all my data in it. Now I can start up my apps using node js - is there anyway to fix this or do I need to redo all my app.js again?
Error: Cannot find module '/home/ubuntu/workspace/RESTful/RESTful
Routing/RESTfulBlogApp/cats.js'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
All the files which are required in your node app must be present at the time of execution.
As per the error, it is unable to find cat.js in the required folder. You need to create again all the required files, and if they were your dbSchema, i would suggest you create your schemas again before running the app.

NODE_PATH is being ignored or not working

I'm trying to run my node app on a new server and am having some issues with the NODE_PATH environment variable. The app works fine on my local machine (OSX Lion) but not on the server (Redhat linux). When starting my app with node app.js from within my project directory /development/nodeproject, I get the following error :
Error: Cannot find module 'mod/core/models/Category'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at /development/nodeproject/app.js:57:5
at Object.<anonymous> (/development/nodeproject/app.js:258:1)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
mod/core/models/Category is the first require() in my app.js and looks like this: var Category = require('mod/core/models/Category'). So apparently node is not looking for modules in my project directory.
I'm not sure why though, because I made the following changes (that are working just fine on my local machine).
added export NODE_PATH=/development/nodeproject to my ~/.bash_profile
ran source ~/.bash_profile
if I run env I see NODE_PATH=/development/nodeproject listed there
in my app.js if I console log process.env.NODE_PATH I get /development/framework (should this output an array instead of a string?)
Other information that might be relevant:
I'm on node v0.6.7
I'm doing all of this as root (sudo su -)
At this point I'm not sure what else I can do. Any help would be greatly appreciated.
NODE_PATH used for modules, not for solutions files.
Try
module.paths.push("/development/nodeproject", "one/more/path");
before any require() call. And you really should use a relative require like require('./mod/core/models/Category') for files in your nodeproject directory
The functionality you are looking for was removed. Use the node_modules directory or a relative require like require('./mod/core/models/Category').
This answer has more info: NODE_PATH error with node.js while attempting to setup jsctags for vim

Resources