Can someone please explain how do node's globally installed behave. It is really confusing me. If I install a package (with executables) such as http-serverglobally I can run it with:
http-server
But if I do
node http-server
I get
module.js:339
throw err;
^
Error: Cannot find module '/path/to/current/dir/http-server'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:136:18)
at node.js:972:3
I suspect my ternpackage in emacs is trying to run it with node hence breaking. Why is this happening? Why can't node find the path to it's own modules?
There are two ways installing packages: globally and locally.
Locally installed package files end up in your local node_modules (in your project folder where you called npm install some-package).
Globally installed package files end up in your system so they are available in command line, if globally installed packages provides executable then you can invoke it in command line directly some-package(without node), if it does not provide executable then you can use it in repl mode (node) like var package = require('some-package') and it is also available locally (inside your project folder even if you don't have it installed locally).
This started as a comment but got now a little longer.
The problem is not exactly node not finding global packages, node only searches for packages in the current location (like under under node_modules), and that is by design. Globally installed packages can be run from the command like because of the way npm installs them, and this is what makes global packages special in some way.
On Unix based systems, npm creates soft links to the main executables of globally installed packages, like http-server in a folder in the executable path. On my machine, this is /usr/local/bin/. This is why those commands can be invoked from the command line without specifying a full path.
On Windows, npm creates an executable batch file named for instance http-server.cmd under %APPDATA% (typically something like C:\Users\YourUserName\AppData\Roaming). The batch file contains instructions to run the target executable from the location where it's actually installed.
rahul#Rahul-Machine:~$ node blalal
module.js:338
throw err;
^
Error: Cannot find module '/home/rahul/blalal'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:286:25)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3
ooh same error
this is because i first command you are actually trying to access a global variable but in second you are some where in your file hierarchy and from there you are saying that you want to access that package so you are wrong if you want to execute that global package try
whereis http-server
then go to that directory and find the file package.json and then open it and find the "main" property and there you get a file name then type
node index.js
your file will be executed
This answer will help you run the npm node module on the command line if you haven't installed it globally.
Either run it globally as they way you are doing.
Other option is to give full path of local package file. For example I want to run a package live-server which is installed as package locally in my current directory.
node ./node_modules/live-server/live-server --port=5000
Here(on my Mac) the live-server.js file is inside the live-server directory, Its optional to add .js and execute the command as below. Also port is optional argument for live-server
node ./node_modules/live-server/live-server.js --port=5000
When you install something globally you store a variable with a stored path linking to it as well as the execution program. While your operating system will know how to access it. Node will not. If you want to "node something.js" you much either be in the directory it is or adjust your path so that node knows how to get to the file. for instance node "c:/jsapps/main/app.js" or if you were in lets say the folder 'jsapps' you would type node "main/app.js" to execute the same file.
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
I had a system crash. While trying to fix it, I deleted a few files and (I may be wrong here), I deleted ~/.profile file among them.
After system restore, my globally installed npm packages are not working.
I uninstalled npm and node and installed the latest version of nodejs 16.x, and then reinstalled globally required npm packages, however, they are still not working. Somewhere the environment variables (in .profile file?) have gone missing.
How can I get the globally installed packages to run?
You are missing the path to the globally installed npm modules in your PATH. There are multiple fixes described on this page, one of which:
Open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
Back on the command line, update your system variables:
source ~/.profile
When I run npm install -g <package> it installs the packages in my user/AppData/Roaming/npm/npm_modules/ folder. This sub-folder is not in my PATH so if I try to run the package without explicitly calling the entire path the call fails with a '<package>' is not recognized as an internal or external command, operable program or batch file.
What can I do to fix this?
Thanks
I'm using win8.1 and I found that the nodejs installer didn't add the path to global node modules to the system PATH. Just add %AppData%\npm; to the user variable(since %AppData% dir is depending on user) PATH to fix it.
You will need to log out then log back in for the change to your PATH variable to take effect.
SET PATH=%AppData%\npm;%PATH%
You have to run this line SET PATH=pathtonodejs;%PATH% (where pathtonodejs is where you installed nodejs) once the installation for nodejs is complete and it should work.
It turned the problem was a change in behavior of the module I was using.
I'd been following old tutorials for using Express.js. The old tutorials assumed Express would be in my path after installing it globally, but as of Express v4.0 there's a separate Express module you have to install in order to get it in your path
I'm quite new to the whole NPM-stuff, however, I would like to create a new package that should be able to run as a console-app (like gulp and grunt).
So basically what I would like to do is making it possible to run
npm install -g mypackage
and then
mypackage
and that would then kick off the console application.
I have been using npm init to init my new package, I have also created the entry point (in my case app.js), and node app.js runs fine.
I have also used npm pack to create a package and npm install {path to my .tgz}.
This is my app.js:
console.log('Hi from NodeJS');
Nothing fancy so far.
The package is called "mypackage"
The problem is that when I type:
mypackage in my console the console application doesn't run.
Any ideas?
Br,
Inx
You're looking for bin.
To install one or more executable files to PATH, you shoul supply a
bin field in your package.json which is a map of command name to
local file name. On install, npm will symlink that file into
prefix/bin for global installs, or ./node_modules/.bin/ for local
installs.
For example, grunt-cli has this:
"bin": {
"grunt": "bin/grunt"
},
Where bin/grunt is a js file (yet without an extension defined).
P.S. If you're planning to ship this as a global package, don't forget to set preferGlobal.
I am using Node.js for a project. I installed WebdriverIO globally using
npm install -g webdriverio
In one of my files, I have:
file.js
var webdriverio = require('webdriverio');
When this file gets loaded, I get an error in the console that says:
Message:
Cannot find module 'webdriverio'
Details:
code: MODULE_NOT_FOUND
If I comment out the line var webdriverio = ..., my code runs fine.
Considering I've installed webdriverio globally, I do not understand why I'm getting this problem.
When you install globally, you should then go to the root of your app and call:
npm link webdriverio
P.S. no need to call npm install, since you will end up having two separate installations of this module, one in global and another in your local node_modules folder
Node.js require looks into the local node_modules folder.
Check this link to learn how to load modules from the global modules folder:
https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
If the NODE_PATH environment variable is set to a colon-delimited list
of absolute paths, then node will search those paths for modules if
they are not found elsewhere. (Note: On Windows, NODE_PATH is
delimited by semicolons instead of colons.)
You need it locally for your app, run npm install webdriverio in the root directory of your app.
Node looks for modules in the innode_modules folders only (starting with current folder and then looking in the folder above). In order to make it work, you have to install this package locally as well.
npm install webdriverio
You can use the full global path:
const wdio = require('/usr/local/lib/node_modules/webdriverio');