I have a Node.js file in c:\scripts and want to execute them in Windows command from anywhere by node my-file.js. It's kind of requirement for execute a bat file, which I can put the folder in %PATH%. I've tried to include c:\scripts in %NODE_PATH% but it doesn't work.
nodejs has the notion of executables ... for example package
jslint
once installed can be directly executed from anywhere ... its installed using the -g flag as in :
npm install -g jslint
just like normal packages its code is installed in dir defined by env var
NODE_PATH
additionally its executable lives in the bin dir which is by default added to PATH during initial nodejs install
on linux
type jshint
jshint is /home/stens/node-v5.8.0/bin/jshint
echo $NODE_PATH
/home/stens/node-v5.8.0/lib/node_modules
on windows box similar happens just examine your PATH to see the nodejs bin dir ... on linux its a location relative to $NODE_PATH
UPDATE :
Alternative to above you can put a some-file (do Windows equivalent to chmod +x some-file to make it an executable) into a dir listed in your $PATH ... for example on linux if I issue
type jslint
which is found because PATH contains dir /home/stens/node-v5.8.0/bin ... above command outputs :
jslint is hashed (/home/stens/node-v5.8.0/bin/jslint)
then show contents
cat /home/stens/node-v5.8.0/bin/jslint
#!/usr/bin/env node
var main = require("../lib/main.js");
main.runMain(main.parseArgs());
you can see the executable file in just nodejs code with the 1st line
#!/usr/bin/env node
which tell the system to execute it using node (dunno the Windows way but similar must be available)
so its executed using
jslint blah input-blah parm-blah goes-blah here-blah
Hopefully this tips you in the right direction
Related
In my ec2 instance I am able to run pm2 command.
But while deploying application through code deployment I get this error.
LifecycleEvent - ApplicationStop
Script - application_stop.sh
[stdout]Stopping any existing node servers
[stderr]/opt/codedeploy-agent/deployment-root/878477e5-6ffb-4175-8e9e-97045ea99290/d-HVRQ58IBL/deployment-archive/application_stop.sh: line 4: pm2: command not found
My application_stop.sh code.
#!/bin/bash
#Stopping existing node servers
echo "Stopping any existing node servers"
pm2 stop main
As per #ranjanistic I checked my pm2 path using which pm2 command and it returned
~/.nvm/versions/node/v16.15.1/bin/pm2
After that I update my application_stop.sh using this below command
~/.nvm/versions/node/v16.15.1/bin/pm2 start main
Also added symbolic link like this to npm, node and pm2.
///this process worked. Thanks #ranjanistic
which npm
which node
which pm2
sudo ln -s /home/ec2-user/.nvm/versions/node/v16.15.1/bin/npm
sudo ln -s /home/ec2-user/.nvm/versions/node/v16.15.1/bin/node
sudo ln -s /home/ec2-user/.nvm/versions/node/v16.15.1/bin/pm2
Still not working
The binary executable reference to your command needs to be available in the environment you're expecting to run it from.
You are using npm to run a pm2 command, which means it is installed as a local module. Therefore you can similarly create another npm script like npm run stop:all with your pm2 command, it should work.
If you're running it in a bash script, the command reference binary should be available in PATH. Or you can also use your command by mentioning its binary path instead of name, independent of wherever the binary is located, for example
If pm2 is installed as a global node module
/usr/bin/pm2 stop main # or whatever the path to the binary is.
Or if pm2 is installed as a node module in project then
./node_modules/bin/pm2 stop main # again, path to pm2 binary can be anything, you'll have to know beforehand
Also, I'd recommend a separate config file for each of your pm2 applications, so that you can use it anywhere without worrying whether your main app is available to pm2 or not.
You may also need to check if npm or node commands are running or not, and based on that you may add the path to your folder containing pm2 in $PATH variable before running the deployment. You can check the path to pm2 manually using which pm2 if it is available.
You need to provide absolute path like this /usr/bin/pm2
Is it possible to run a JavaScript file with node.js on windows? I have been trying to for hours and can't find any more solutions on the internet that work.
I have a js-file that uses scribbletune which only works with node.js.
I have node.js installed and I installed gitbash because it was recommended in a forum.
I tried to run from command prompt and gitbash but nothing seems to happen.
What am I doing wrong? Any help would be very much appreciated.
There is no need to install gitbash to use Node on Windows. It's a handy thing to have if you're used to a *nix environment. If you're not, it just gives you something more to learn, which isn't helpful if you're already in the middle of trying to learn Node.
Just:
Get the Windows installer from https://nodejs.org/en/download/
Run the Windows installer
Create a directory for your project
Open a Command Prompt Window to get a command line
Switch to your project directory
(Optional, but a good idea) Use npm init to create a package.json file (it'll walk you through it)
Install any libs you're going to be using via npm (for instance, npm install scribbletune from your command prompt window)
Put your JavaScript files in that directory
Use node main.js at the command line to run your main file (whatever it's called; main.js is just a placeholder)
Inorder to run a js (java script file) file
step 1. u need to go to the file location where u want to run.
step 2. just use "shift +right click".
u will see a pop-up and go to powershell or cmd.
step 3. type "node FILENAME.js"
final step: you will see the result ^.^
I tried to install SpiderMonkey 45 in Ubuntu 16.04, but I failed. I refered to SpiderMonkey Build Documentation and finished the make process.
cd js/src
autoconf-2.13
mkdir build_DBG.OBJ
cd build_DBG.OBJ
../configure --enable-debug --disable-optimize
make
After the installation, when I change the directory to ../build_DEBUG.OPT/js/src/shell and type js, the terminal gave me an error:
The program 'js' can be found in the following packages:
* nodejs
* rhino
Try: sudo apt install <selected package>
How to solve the problem? Thank you very much.
Calling js directly looks in your PATH environment variable for an executable called js. Since the directory that you built Spidermonkey in isn't in your path, the executable isn't found, causing an error.
./js expands to <your current directory>/js, which is specific enough for your terminal to find the executable and run it.
See this question for a more general explanation as to why ./executable works but executable doesn't.
I have some javascript that I have used the sublime jsdoc plugin to comment. I want to produce some documentation from those comments. I am developing this javascript on a machine behind a firewall and very poor internet, so using the command "npm install jsdoc" fails. This machine definitely has node/npm installed.
I have a laptop dual booting linux/windows 10 which I can take to a decent internet connection.
Searching I came across "opichals" workaround:
fetch the package and its dependencies into a local cache folder:
npm_config_cache=./npm_cache npm install express
run the 304 HTTP server
node -e "var http = require('http'); var fs = require('fs'); http.createServer(function (req, res) { res.writeHead(304, {'Content-Type': 'text/plain'}); res.end(); }).listen(9615);" &
now this will actually install even when the npmjs.org is not resolvable
npm_config_registry=http://localhost:9615/ npm_config_cache=./npm_cache npm install express
shut the 304 server down
fg
CTRL+C
Source:
https://github.com/npm/npm/issues/1738#issuecomment-10414774
Obviously (?) you can't type all of these commands into a command line. I have tried the steps, generated a cache folder which contains all jsdoc dependencies (and jsdoc).
I moved that cache onto my other computer, span up the http server (which I dont understand either, I assume I run this inside the cache folder?), set npm config "cache" and "registry" parameters to the appropriate locations. But when I try to npm install jsdoc, it cannot find any of the packages.
So I am seeking to answer how do I go about setting up jsdoc on a machine without internet?
I would also like to know if I am correct in believing you can install a package + dependencies from a local cache. Or could you copy and paste a node_modules folder from one computer to another?
The answer: execute "npm install jsdoc". Wherever jsdoc is installed to (usually within the folder that the command was executed - I had messed with some settings so mine was different). That folder will be called node_modules.
I took node_modules to the computer with poor internet. I did not change the name (and this is important!), opened a command prompt within it and I can now simply type .bin/jsdoc --version and the version prints out.
Because I am running executing jsdoc from the node_modules folder, I had to edit jsdoc.js so that the relative path it generated to look for another module was correct:
Find jsdoc.js (node_modules/jsdoc/jsdoc.js) - open with notepad++ etc
Change line 25 "require('requizzle')" to "require('../requizzle')".
Running jsdoc (had a huge number of warnings but still) generates an output folder within node_modules.
I hadn't realised you could just copy+paste a node_modules folder. I suspect the edit I had to do means I shouldn't do this?
I have to set-up node environment in my production system. I installed node in my local system its fine, after that i checked for version node -v.
Now i installed node in my production system using sudo apt get install nodejs and now i checked the version node -v nothing happened but i used nodejs -v, its working.
Also in my local system i used node filename.js to run my node app. But in my production system i have to use nodejs filename.js. I dont know why this happening? Also whether it will create problem in my production. Please share your ideas.
It's more linux specific question. You can create a symlink to use node instead of nodejs. Command example here:
sudo ln -sT $(which nodejs) /usr/local/bin/node
This is because your particular distribution of linux already has a node binary.
You're more than likely not using it for anything. You have two options.
rename the pre-installed node to something else, and rename nodejs to node
rename nodejs to node but make sure to adjust your $PATH so that node.js's directory is loading before the pre-installed node