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.
Related
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.
I am working on this react app and thinking of adding some environment variables inside, this is what I've done:
installed the latest version of react-scripts
added .env file on the root folder (the same location where node_modules folder is)
added REACT_APP_OTHER_OTHER_THING=asdfas just to test the variable
REACT_APP_OTHER_OTHER_THING=asdfas
open index.js and console.log(process.env.REACT_APP_OTHER_OTHER_THING) inside to see the output
import React from 'react';
import Reactdom from 'react-dom';
import App from './App';
console.log(process.env.REACT_APP_OTHER_OTHER_THING, 'DOTENV')
Reactdom.render(<App/>, document.getElementById("app"))
then I rebuilt the app and started the app to see the result
but then it gives out undefined as the output for process.env.REACT_APP_OTHER_OTHER_THING. I then tried to print process.env.NODE_ENV (which is working and prints "development" as output).
note: I have also tried to add temporary variable as the docs said in https://create-react-app.dev/docs/adding-custom-environment-variables >> rebuilt the server and run ($env:REACT_APP_OTHER_OTHER_THING= "abcdef") -and (npm start) << due to me running it on powershell which still gives undefined as output.
is there anything I can do on this?
thank you
Ensure it has correct directory tree
D:\your-react-project \ .env
Here it maybe your-react-project \ .env.development
It should be in the path of where default README.md placed
Alright, the problem I'm having seemed to be from the webpack I made in my React App,
I tried to follow the instruction from this article and it's working well!
after configuring my webpack, I rebuilt it, restart the server, and it works!
edit: for my solution, I added:
const dotenv = require('dotenv');
const env = dotenv.config().parsed;
const envKeys = Object.keys(env).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(env[next]);
return prev; }, {});
at the top of webpack.common.js
and also added
plugins: [
new webpack.DefinePlugin(envKeys), //this line
]
in the module.exports
in the plugin. I hope this helps! :)
Can't comment, so i will post an answer, sorry.
Are you sure about ($REACT_APP_OTHER_OTHER_THING= "abcdef") -and (npm start), because docs says ($env:REACT_APP_NOT_SECRET_CODE = "abcdef") -and (npm start)
I just added new env var, and it gave me undefined, but after server restart it worked just fine. Can you try to restart server, but add env variable not in terminal, but inside .env file?
UPD1:
just so you know, NODE_ENV is set by npm start or npm run build commands, they set to development or production, respectively.
As docs says:
You cannot override NODE_ENV manually. This prevents developers from accidentally deploying a slow development build to production.
This worked for me with the same issue. I double-checked everything was connected correctly then killed my server in the terminal after started it back up & it worked fine.
Other causes of undefined environment variables on a much simpler level than the op's Webpack configuration question include:
Leaving out process.env.REACT_APP_SERVER_URL and just writing REACT_APP_SERVER_URL in your code
Forgetting to name the environment variable with REACT_APP as the first part of the string
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 am creating vue-cli project and everything works file, but when i try to install new module it show me error after install :
This dependency was not found:
* fs in ./~/babel-loader/lib!./~/vue-loader/lib/selector.js?type=script&index=0!./src/components/Index.vue, ./~/mp3-duration/index.js and 1 other
To install it, you can run: npm install --save fs
I tried multiple times to confirm but still an error, in the node_modules folder there is a library.
I am installing this module and using :
const musicdata = require('musicmetadata');
After add above code i am writing this :
getDuration(file) {
fs.createReadStream(file), (err, metadata) => {
if (err) throw err;
console.log(metadata)
}
}
But getting an error as I specified above, I want a single page website but want some node_modules to get some information, how do I include these modules?
Thanks
I would say this is because fs is a specifically for reading files in a Node application and you're running it in the browser. You could look into the FileReader API which helps read local files (and I would imagine your application's files). Worst case, someone has written an NPM package for it.
Context
I cloned a basic node-browserify boilerplate project and got it up. I'm using coffee-script. Right now, I'm trying to add jadeify to the equation as follows:
bundle = browserify
entry: __dirname + "/app/init.coffee"
debug: true
mount: "/app.js"
bundle.use jadeify __dirname + '/views'
app.use bundle
This is before I even attempt to use jadeify anywhere.
Issue
Everything works, until I add bundle.use(jadeify(__dirname + '/views')) as a middleware to browserify. Then I get the following error message in browser's console:
Uncaught ReferenceError: __require is not defined
According to the browser's console, the source of this message is /app.js.
Question
Why does this script fail with an error as soon as I try to add the jadeify middleware for browserify?
Motivation
I figured that it'll be more convenient to reuse server-side jade templates on client-side, so I'm ditching underscore templates in favor of jade. While doing my research, I came across this solution to a related question that suggests the use of jadeify. It seems doable, but something seems to be failing.
One can bypass jadeify by using browjadify
Usage:
browjadify --entry=app.coffee >bundle.js
Source: browjadify
#!/usr/bin/env node
var jade = require('jade')
var browserify = require('browserify')
var fs = require('fs');
var argv = require("optimist").argv;
var b = browserify()
b.register('.jade', function(body) {
var options = {"client": true, "compileDebug": false};
body = "module.exports = " + jade.compile(body, options).toString() +";";
return body;
});
var jaderuntime = require('fs').readFileSync(__dirname+"/node_modules/jade/runtime.js", 'utf8');
b.prepend(jaderuntime); // Brings in var jade that jade.compile needs
b.addEntry(argv.entry); // gets browserify to do its thing
console.log(b.bundle()); // the bundled output
I've seen this today as well and managed to fix it.
The problem for me was that jadeify depends on browserify version at most 1.2.9 but the current version of browserify in the git repo is newer (much newer, something above 1.8 if i remember correctly). And being new myself to working with this setup I installed browserify first (with the newest version) and then jadeify installed it's own dependent browserify (with the supproted version) in it's own module space.
Then when I was running my app the browserify I was calling was the new version but the libs that jadeify was using were the old version and this created a conflict somewhere and thus the error you were seeing as well.
I ended up just reinstalling the latest supported version of browserify in my app space and that fixed it.
LATER EDIT:
Problem with the fix above was that browserify#1.2.9 does not have caching and made reloading the server very slow. But I managed to find browserijade which works with the latest version of browserify (1.9.4) an does exactly the same things as jadeify.
Hope it helps!