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');
Related
I want to do something like this, where, I want to keep all my packages globally just like node package itself. So for example in my package.json I have a package name called "Highcharts" I want to install it globally I don't want to create a local node_modules folder and use it but I want to access it from outside so next time whenever I want to create a copy of my project folder I should be able to use highcharts directly without using npm install. Is it possible?
globally installed node_modules - > Users/user/AppData/Roaming/node_modules/highcharts
app
src
node_modules (I don't want to keep it)
package.json
tsconfig.json
angular.json
How to link these globally installed node_modules with the current app or any app which we want to create?
Any help will be appreciated. Thank you so much :)
local packages are installed in the project directory
global packages are installed in a single place in your system
Usually it is a good idea to have all npm packages required for your project installed locally (project folder). This makes sure, that you can have dozens of applications which are running a different versions of each package if needed.
export NODE_PATH='yourdir'/node_modules
Hello, if am getting right, you want to keep all dependencies global.
You can just run install with -g command. Those libraries will be available in node installation folder.
From the Node docs
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.)
Additionally, node will search in the following locations:
1: $HOME/.node_modules
2: $HOME/.node_libraries
3: $PREFIX/lib/node
Where $HOME is the user's home directory, and $PREFIX is node's configured node_prefix.
These are mostly for historic reasons. You are highly encouraged to place your dependencies locally in node_modules folders. They will be loaded faster, and more reliably.
I hope I answered, you just need to manage the paths to node_modules wherever you have kept it.
After installing node-crawler in Node.js (not in the default directory) via the npm command, I tried to run the code in the "Usage" section but an error occurs when executing var Crawler = require("crawler"); and the VisualStudio Code debug console says Cannot find module 'crawler'.
Does it happen because I installed crawler in a custom location? How can I fix this?
npm install will install a package locally. (--save to have package appear in your dependencies.)
To have access to it from everywhere, you need to install it globally, using npm install -g
Maybe I found the solution. I replaced "crawler" in var Crawler = require("crawler"); with the path that points to the crawler.js file in the lib folder in node-modules, and now the code works. Maybe it happened because I installed crawler in a custom location and so VisualStudio couldn't find "crawler".
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
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.
I'm digging into a node package that uses a CLI, and am trying to extend it by adding some functionality. I've cloned the repo from github, but I've also installed it via npm.
How can I use my local version, instead of the one that's been installed via npm?
Thanks!
When you install a package using npm, it just puts it into the node_modules folder in the folder where you ran it (or if you pass -g, into a global node_modules folder).
require() uses a particular search order to find modules. To get a specific version of a module to load you can take two paths:
Specify a relative path to the module: require("./path/to/myfork/of/module")
Delete the version of the module installed by npm into mode_modules and put your fork of it in there
Make sure that your fork of that module is in a "closer" node_modules folder. Node searches the node_modules in same folder as the file calling require() and then works its way up the folder hierarchy to find a module.
For more information, take a look at http://nodejs.org/docs/v0.4.11/api/modules.html