I am using selenium-webdriver module on npm and my code is =
const selenium = require('selenium-webdriver');
const { Builder, By, Key } = selenium;
(async() => {
const driver = await new Builder().forBrowser('chrome').withCapabilities(selenium.Capabilities.chrome().set('chrome.binary', './drivers/chromedriver.exe')).build();
})();
It gives me the error Error: The ChromeDriver could not be found on the current PATH. I downloaded chromedriver then try both using .withCapabilities(that part is still inside the code above) and export PATH=$PATH: but both not worked still giving same error I have chromedriver in a folder called drivers and that folder is on same folder with my index.js file I think my path writing is wrong but I really don't know how to do it can someone help?
As #Max Daroshchanka suggested in the comments section, first thing is that you must have the path to the executable chrome.exe for the "chrome.binary" option.
Secondly, I will suggest that you have the same version for chromedriver.exe and your chrome.exe. I faced the same issue because I was not having the same version.
Now the following is something that did not work for me. Could not be the case with you.
Thirdly, I will also suggest that instead of relying on the node.js (or any package manager's) library (this gets created when you perform npm install, for every dependency there is a /lib folder created that has the executable) to add the required chromedriver.exe file. What I mean is manually download the required version driver file from here:
http://chromedriver.storage.googleapis.com/index.html
and add this downloaded extracted path to the PATH env variable.
My project structure is like this:
moduleA -> Library that i publish to npm using Rollup
projectA -> My gatsby project that installs moduleA and uses it.
I'm using this library to bundle my workers with my other library code into the dist folder: https://github.com/darionco/rollup-plugin-web-worker-loader
Module A code:
workers/index.js
let webWorker = null;
if (typeof window !== "undefined") {
webWorker = new Worker(new URL("./my.worker.js", import.meta.url), {
type: "module",
});
}
export default webWorker;
workers/my.worker.js
self.onmessage = (message) => {
console.log("hii");
self.postMessage("yes");
};
When I build the above library the result is this:
So you can see that the workers are correctly in the library's dist now. This all works great.
If we take a look into index.modern.module.js you can see this is the output for the worker code:
if (typeof window !== "undefined") {
webWorker = new Worker(new URL("my.worker-f73c7cd4.js", import.meta.url), {
type: "module"
});
}
Now in my main project I have this webpack rule to convert the import.meta.url to a path otherwise webpack crashes as it does not recognize import.meta.url:
config.module.rules.push({
test: /\.jsx?$/,
loader: require.resolve("#open-wc/webpack-import-meta-loader"),
});
I now run my main react project (which is built in gatsby & webpack) and this is the output in index.modern.module.js:
if (typeof window !== "undefined") {
webWorker = new Worker(new URL("my.worker-8e6a623b.js", ({ url: getAbsoluteUrl('node_modules/#tracktak/dcf-react/dist/index.modern.module.js') }).url), {
type: "module"
});
}
Here's the network request, you can see it loans in but I'm pretty sure the path is wrong. It's just saying a 200 ok because it's going to 404 page I think:
And it gives me a console error:
Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
So, my question is. How do I load the web worker to the public folder for my Gatsby project (projectA). Do I need to use something like worker-loader here? I'm just not sure how it can work here because it's in my npm module and NOT in my project.
Any help would be great and I'd award a bounty!
Thanks
EDIT: It seems like worker-plugin is getting my closer to the solution. If I use this in my main project and modify the web workers in the dist output from this: new URL("my.worker-8e6a623b.js", ({ url: getAbsoluteUrl('node_modules/#tracktak/dcf-react/dist/index.modern.module.js') }).url) to this: new URL("my.worker-8e6a623b.js") it works fine as worker-plugin only accepts string arguments.
However this isn't a sustainable solution because obviously I don't want to modify dist files.
Updated:
Verify the dist folder contents are correct. I'm surprised to see files from old builds (multiple hash ids). Clean folder, then generate all scripts (for performance reasons, you might want to only do this on production builds.)
Then when you npm publish the files, verify npm picked up what you expect.
There's an alternate way to make sure it's not related to npm's selective file bundling. Use the GitHub repo URL to npm install <github repo URL>.
Original response:
I believe you need worker-loader.
I've used comlink & worker-loader to help smooth over Webpack worker bundling.
I have confusion about access node_modules that can be used in JavaScript. for this does anyone can give an example to call modul.export contained in the folder node_module (after install packet with NPM - nodejs) ?
tree structure file :
folder ethereum any folder node_modules , file index.html (for call module.export) , package-lock.json , package.json
package.json file :
enter link description here
so this way, I've installed "npm install web3". Now, when I call a function from web3 like for example in a program like this :
var Web3=require('web3');
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/metamask"));
}
console.log(web3);
Then output errors like this :
enter image description here
The web3 package can either be installed through npm with npm install web3 or is exposed as a global web3 if you import it like:
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
It can either be run as a global exposed by the <script></script> tag or as a node package that needs to be bundled first.
Your error code require is not defined tells you that node is not running your code, but something else is consuming your code. Try to bundle your code to something the browser understands, or only use the global web3 to interact with the package.
Read more about bundles here: https://docs.npmjs.com/getting-started/packages
If I understand correctly, you are missing a big piece of the puzzle here.
You need to compile your code for the browser to be able to run it. Try reading up on this question
The title of my question is how to run a command line tool from a node.js application because I think an answer here will apply to all command line utilities installable from npm. I have seen questions related to running command line from node.js, but they don't seem to be working for my situation. Specifically I am trying to run a node command line utility similar to npm (in how it is used, but not its function) called tilemantle.
tilemantle's documentation shows installing tilemantle globally and running the program from the command line.
What I would like to do is install tilemantle locally as a part of a npm project using npm install tilemantle --save and then run tilemantle from inside my project.
I've tried `tilemantle = require('tilemantle'), but the index.js file in the tilemantle project is empty, so I think this won't help with anything.
I tried the project node-cmd
const cmd = require('node-cmd');
cmd.run('./node_modules/tilemantle/bin/tilemantle', 'http://localhost:5000/layer/{z}/{x}/{y}/tile.png', '-z 0-11', '--delay=100ms', '--point=37.819895,-122.478674', '--buffer=100mi'
This doesn't throw any errors, but it also just doesn't work.
I also tried child processes
const child_process = require('child_process');
child_process.exec('./node_modules/tilemantle/bin/tilemantle', 'http://localhost:5000/layer/{z}/{x}/{y}/tile.png, -z 0-11 --delay=100ms --point=37.819895,-122.478674 --buffer=100mi'
This also doesn't throw any errors, but it also doesn't work.
Is there a way to get this working, so that I can run tilemantle from inside my program and not need to install it globally?
Update
I can get tilemantle to run from my terminal with
node './node_modules/tilemantle/bin/tilemantle', 'http://localhost:5000/layer/{z}/{x}/{y}/tile.png', '--delay=100ms', '--point=37.819895,-122.478674', '--buffer=100mi', '-z 0-11'
If I run the following as suggested by jkwok
child_process.spawn('tilemantle', ['http://myhost.com/{z}/{x}/{y}.png',
'--point=44.523333,-109.057222', '--buffer=12mi', '-z', '10-14'],
{ stdio: 'inherit' });
I am getting spawn tilemantle ENOENT and if I replace tilemantle with ./node_modules/tilemantle/bin/tilemantle.js I get spawn UNKNOWN
Based on jfriend00's answer it sounds like I need to actually be spawning node, so I tried the following
child_process.spawn('node', ['./node_modules/tilemantle/bin/tilemantle.js', 'http://myhost.com/{z}/{x}/{y}.png', '--point=44.523333,-109.057222', '--buffer=12mi', '-z', '10-14'], { stdio: 'inherit' });
Which gives me the error spawn node ENOENT which seems strange since I can run it from my terminal and I checked my path variable and C:\Program Files\nodejs is on my path.
Just to check I tried running the following with a full path to node.
child_process.spawn('c:/program files/nodejs/node.exe', ['./node_modules/tilemantle/bin/tilemantle.js', 'http://myhost.com/{z}/{x}/{y}.png', '--point=44.523333,-109.057222', '--buffer=12mi', '-z', '10-14'], { stdio: 'inherit' });
which runs without the ENOENT error, but again it is failing silently and is just not warming up my tile server.
I am running Windows 10 x64 with Node 6.11.0
You can install any executable locally and then run it with child_process. To do so, you just have to figure out what the exact path is to the executable and pass that path to the child_process.exec() or child_process.spawn() call.
What it looks like you ultimately want to run is a command line that does:
node <somepath>/tilemantle.js
When you install on windows, it will do most of that for you if you run:
node_modules\.bin\tilemantle.cmd
If you want to run the js file directly (e.g. on other platforms), then you need to run:
node node_modules/tilemantle/bin/tilemantle.js
Note, with child_process, you have to specify the actual executable which in this case is "node" itself and then the path to the js file that you wish to run.
This, of course, all assumes that node is in your path so the system can find it. If it is not in your path, then you will have to use the full path to the node executable also.
It looks like you are trying to capture the output of tilemantle from running a file rather than from the command line. If so, I did the following and got it to work.
Installed tilemantle and child_process locally into a new npm project as you did, and added the following into a file in that project.
// test.js file
var spawn = require('child_process').spawn;
spawn('tilemantle', ['http://myhost.com/{z}/{x}/{y}.png', '--
point=44.523333,-109.057222', '--buffer=12mi', '-z', '10-14'], { stdio: 'inherit' });
Run it using node test.js inside the project.
I tried a bunch of the other options in this post but could only get the above one to print the progress along with other output. Hope this helps!
Many command line utilities come with a "programmatic" API. Unfortunately, tilemantle does not, which is why you are unable to require that module in your code.
You can, however, easily access a locally installed version of the CLI from npm scripts. I don't know anything about tilemantle, so I'll provide an example using the tldr command line tool. In your package.json:
{
"name": "my-lib",
"version": "1.0.0",
"scripts": {
"test": "tldr curl"
},
"dependencies": {
"tldr": "^2.0.1"
}
}
You can now run npm test from the terminal in your project as an alias for tldr curl.
Per this post, you can use the global-npm package to run npm scripts from your code:
const npm = require('global-npm')
npm.load({}, (err) => {
if (err) throw err
npm.commands.run(['test'])
})
And voila, you can now run the locally installed CLI programmatically(ish), even though it has not offered an API for that.
I'm in the process of re-writing my electron app with ES6, using Laravel Mix to compile the app JS and SASS. Now the main process loads up the render process fine. Once that happens my app.js loads up and that's where I have my issues. So I do:
import { remote } from 'electron';
Which causes this error in console:
Uncaught Error: Electron failed to install correctly, please delete node_modules/electron and try installing again
Now i've tried reinstalling electron, even though electron works when the main process fires up to begin with. The line refers to this in the compiled js:
/* WEBPACK VAR INJECTION */(function(__dirname) {var fs = __webpack_require__(8)
var path = __webpack_require__(9)
var pathFile = path.join(__dirname, 'path.txt')
if (fs.existsSync(pathFile)) {
module.exports = path.join(__dirname, fs.readFileSync(pathFile, 'utf-8'))
} else {
throw new Error('Electron failed to install correctly, please delete node_modules/electron and try installing again')
}
I'm not really sure what's going on, any advice or information would be a great help!
Thanks
Edit: I've tried running it with --verbose:
/Library/Caches/com.apple.xbs/Sources/AppleGVA/AppleGVA-10.1.16/Sources/Slices/Driver/AVD_loader.cpp: failed to get a service for display 3
2017-06-13 16:10:42.383 Electron Helper[47106:766924] Couldn't set selectedTextBackgroundColor from default ()
Most probably source of problem is that path.txt doesn't exists.
path.txt is generated while installing electron from npm. If you are not seeing any error while installing electron that means, errors are getting suppressed.
Troubleshoot: Check if node_modules/electron/path.txt exist. If not, then you have got a problem.
Solution:
Note: If on Windows, use native CMD instead of Git Bash
Try to install electron manually after npm install by executing following script
cd node_modules/electron && node install.js
This may take up a while, since it is going to download electron's full package.