Import using absolute path using Webpack and Electron - javascript

I am trying to import a file with an absolute path into my main electron thread.
Import code in the main electron thread:
import * as Test2 from 'app/main/tray.js';
I have added the resolve.root to the webpack.config:
{
module: {
loaders: [{
test: /\.jsx?$/,
loaders: ['babel-loader'],
exclude: /node_modules/,
}, {
test: /\.json$/,
loader: 'json-loader',
}],
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name]/index.js',
libraryTarget: 'commonjs2',
},
resolve: {
root: [
path.resolve('./')
],
extensions: ['', '.js', '.jsx'],
packageMains: ['webpack', 'browser', 'web', 'browserify', ['jam', 'main'], 'main'],
},
plugins: [
],
externals: [
// put your node 3rd party libraries which can't be built with webpack here
// (mysql, mongodb, and so on..)
],
};
No errors are thrown when I run the webpack server (webpack finds the files just fine) but an error is thrown when I run electron.
App threw an error during load
Error: Cannot find module 'app/main/tray.js'
at Module._resolveFilename (module.js:440:15)
at Function.Module._resolveFilename (C:\Git\stemn-electron-2\node_modules\electron-prebuilt\dist\resources\electron.asar\common\reset-search-paths.js:35:12)
at Function.Module._load (module.js:388:25)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (index.js:22:15)
at Module._compile (module.js:541:32)
at loader (C:\Git\stemn-electron-2\node_modules\babel-register\lib\node.js:158:5)
at Object.require.extensions.(anonymous function) [as .js] (C:\Git\stemn-electron-2\node_modules\babel-register\lib\node.js:168:7)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at loadApplicationPackage (C:\Git\stemn-electron-2\node_modules\electron-prebuilt\dist\resources\default_app.asar\main.js:288:12)
at Object.<anonymous> (C:\Git\stemn-electron-2\node_modules\electron-prebuilt\dist\resources\default_app.asar\main.js:330:5)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\david\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "run" "start-hot"
npm ERR! node v6.2.1
npm ERR! npm v3.10.6
npm ERR! code ELIFECYCLE
npm ERR! STEMN#0.0.1 start-hot: `cross-env HOT=1 NODE_ENV=development electron -r babel-register -r babel-polyfill ./app/main/index`
npm ERR! Exit status 7
npm ERR!
npm ERR! Failed at the STEMN#0.0.1 start-hot script 'cross-env HOT=1 NODE_ENV=development electron -r babel-register -r babel-polyfill ./app/main/index'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the STEMN package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! cross-env HOT=1 NODE_ENV=development electron -r babel-register -r babel-polyfill ./app/main/index
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs STEMN
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls STEMN
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! C:\Git\stemn-electron-2\npm-debug.log
It looks like electron is changing the webpack import behavior? These files are imported properly in the renderer windows, just not the main thread...

Have you tried this:
root: [
path.resolve(__dirname)
]

My issue was the I was using webpack to build the electron renderers but not using webpack to build the electron main-thread.
I added a new webpack.config specifically for the main thread - now I can use webpack.root (but all the code must compile now which is a bit of a shame...)

Related

Can't start webpack development server

I am new on es6. I followed a blog Beginner’s Guide To Setup ES6 Development Environment. I completed all of the process step-by-step. Then the step comes up to install the webpack development server. Then I write this code to cmd - npm run build. I get this error:
λ npm run build
-> jj#1.0.0 build C:\Users\galib\Desktop\jj
-> webpack-dev-server
The CLI moved into a separate package: webpack-cli
Please install 'webpack-cli' in addition to webpack itself to use the CLI
-> When using npm: npm i -D webpack-cli
-> When using yarn: yarn add -D webpack-cli
internal/modules/cjs/loader.js:888
throw err;
^
Error: Cannot find module 'webpack-cli/bin/config-yargs'
Require stack:
- C:\Users\galib\AppData\Roaming\npm\node_modules\webpack-dev-server\bin\webpack-dev-server.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:885:15)
at Function.Module._load (internal/modules/cjs/loader.js:730:27)
at Module.require (internal/modules/cjs/loader.js:957:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Users\galib\AppData\Roaming\npm\node_modules\webpack-dev-server\bin\web
at Module._compile (internal/modules/cjs/loader.js:1068:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module._load (internal/modules/cjs/loader.js:774:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\galib\\AppData\\Roaming\\npm\\node_modules\\webpack-dev-server\\bin\\webpack-dev-serv
]
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! jj#1.0.0 build: `webpack-dev-server`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the jj#1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\galib\AppData\Roaming\npm-cache\_logs\2021-06-08T18_22_59_337Z-debug.log
The article you read was probably written before Webpack's cli was moved to webpack-cli, so you might need one small extra step.
Please install 'webpack-cli' in addition to webpack itself to use the CLI
-> When using npm: npm i -D webpack-cli
-> When using yarn: yarn add -D webpack-cli
Run npm i -D webpack-cli and then you can use the Webpack CLI.

Debug Error with log4js appender when starting npm

When starting npm I am facing an error. The error states that "Error: log4js configuration problem for { type: 'log4js-syslog-appender'," Essentially seems like the format for the log4js config might be wrong. I am a complete newbie to node and would appreciate any pointers on what may be a good next step here.
I have inherited a bunch of code that was written with Node 4.2 and npm 3.x I have had to compile openssl fips binaries and then recompile and reinstall node (6.11.1) and npm with it (3.10.10). I am now simply trying to do npm start and running into an error.
Config.js looks like this for the log4js part:
/*
|--------------------------------------------------
| Logging
|--------------------------------------------------
*/
var log4js = require('log4js');
var use_syslog = process.env.LOG_SYSLOG == "true";
var appenders = [ { type: 'console' } ];
if (use_syslog) {
appenders.push({ type: 'log4js-syslog-appender',
tag: 'API',
facility: 'local0',
path: '/dev/log',
transport: 'socket' });
}
log4js.configure({
appenders: appenders
});
config.logger = use_syslog ? log4js.getLogger('syslog') : log4js.getLogger('console') ;
config.logger.setLevel(process.env.DEBUG_LEVEL);
I would like to find a way to solve this error but cannot figure out what exact change I need to make in the config.js file. If any direction can be provided it'll be great.
The exact error is as below:
==========================================================================
oi#oi01:~/api$ npm start
> oi-api#2.0.0 start /home/oi/api
> babel-node server.js --presets es2015
/home/oi/api/node_modules/log4js/lib/log4js.js:316
throw new Error(
^
Error: Problem reading log4js config { appenders:
[ { type: 'console', makers: [Object] },
{ type: 'log4js-syslog-appender',
tag: 'API',
facility: 'local0',
path: '/dev/log',
transport: 'socket',
makers: [Object] } ] }. Error was "log4js configuration problem for { type: 'log4js-syslog-appender',
tag: 'API',
facility: 'local0',
path: '/dev/log',
transport: 'socket',
makers:
{ console: [Function: bound configure],
'log4js-syslog-appender': [Function: bound configure] } }" (Error: log4js configuration problem for { type: 'log4js-syslog-appender',
tag: 'API',
facility: 'local0',
path: '/dev/log',
transport: 'socket',
makers:
{ console: [Function: bound configure],
'log4js-syslog-appender': [Function: bound configure] } }
at /home/oi/api/node_modules/log4js/lib/log4js.js:256:15
at Array.forEach (native)
at configureAppenders (/home/oi/api/node_modules/log4js/lib/log4js.js:248:18)
at configureOnceOff (/home/oi/api/node_modules/log4js/lib/log4js.js:308:7)
at Object.configure (/home/oi/api/node_modules/log4js/lib/log4js.js:371:3)
at Object.<anonymous> (/home/oi/api/config/config.js:92:8)
at Module._compile (module.js:570:32)
at loader (/home/oi/api/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/oi/api/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32))
at configureOnceOff (/home/oi/api/node_modules/log4js/lib/log4js.js:316:13)
at Object.configure (/home/oi/api/node_modules/log4js/lib/log4js.js:371:3)
at Object.<anonymous> (/home/oi/api/config/config.js:92:8)
at Module._compile (module.js:570:32)
at loader (/home/oi/api/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/oi/api/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
npm ERR! Linux 4.4.0-1002-fips
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v6.11.1
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! oi-api#2.0.0 start: `babel-node server.js --presets es2015`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the oi-api#2.0.0 start script 'babel-node server.js --presets es2015'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the oi-api package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! babel-node server.js --presets es2015
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs oi-api
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls oi-api
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /home/oi/api/npm-debug.log
===============================================================

log4js npm error , log4js configuration

root#------:~# cd /home/bot
root#------:/home/bot# node site.js
/home/bot/node_modules/log4js/lib/configuration.js:43
throw new Error(Problem with log4js configuration: (${util.inspect(this.candidate, { depth: 5 })}) +
^
Error: Problem with log4js configuration: ({ appenders:
[ { type: 'console' },
{ type: 'file', filename: 'logs/site.log' } ] }) - must have a property "appenders" of type object.
at tests.forEach (/home/bot/node_modules/log4js/lib/configuration.js:43:15)
at Array.forEach (native)
at Configuration.throwExceptionIf (/home/bot/node_modules/log4js/lib/configuration.js:41:11)
at new Configuration (/home/bot/node_modules/log4js/lib/configuration.js:203:10)
at Object.configure (/home/bot/node_modules/log4js/lib/log4js.js:205:12)
at Object. (/home/bot/site.js:12:8)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
i use command npm install log4j --save
root#------:/home/bot# npm install log4j --save
npm ERR! Linux 3.2.0-4-amd64
npm ERR! argv "/root/.nave/installed/6.11.1/bin/node" "/root/.nave/installed/6.11.1/bin/npm" "install" "log4j" "--save"
npm ERR! node v6.11.1
npm ERR! npm v3.10.10
npm ERR! code E404
npm ERR! 404 Registry returned 404 for GET on https://registry.npmjs.org/log4j
npm ERR! 404
npm ERR! 404 'log4j' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
npm ERR! Please include the following file with any support request:
npm ERR! /home/bot/npm-debug.log
I had the same problem as u with the same script xD.
I found a solution!!!!!!!!!!!!!
U need to change thelog4js.configure in your site.js to this
log4js.configure({
appenders: {
out: { type: 'console' },
task: { type: 'file', filename: 'logs/site.log' }
},
categories: {
default: { appenders: [ 'out' ], level: 'info' },
task: { appenders: [ 'task' ], level: 'info' }
}
});

npm run dev command does not work on vuejs' hackernews example

I am trying to run the vue-hackernews-2.0 example from vuejs' github repository
In setup section it explains how to install the project:
# install dependencies
npm install # or yarn
# serve in dev mode, with hot reload at localhost:8080
npm run dev
# build for production
npm run build
# serve in production mode
npm start
I did npm install. After changing the port to 8888 in server.js I typed "npm run dev" on command line.
> vue-hackernews-2.0# dev D:\Users\212399486\WebstormProjects\vue-hackernews-2.0-master
> node server
server started at localhost:8888
DONE Compiled successfully in 16328ms 1:02:18 PM
DONE Compiled successfully in 17845ms 1:02:19 PM
webpack built bd162a76119031a85eed in 17845ms
When I go localhost:8888 It will just try to load for 1 min and then it fails, without anything on the console.
I thought I should also try "npm run build" and "npm start" so I also used two commands. "npm run build" successfully created the dist file.
But after "npm start" I get this error:
> vue-hackernews-2.0# start D:\Users\212399486\WebstormProjects\vue-hackernews-2.0-master
> cross-env NODE_ENV=production node server
module.js:471
throw err;
^
Error: Cannot find module './dist/vue-ssr-server-bundle.json'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\Users\212399486\WebstormProjects\vue-hackernews-2.0-master\server.js:41:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
events.js:160
throw er; // Unhandled 'error' event
^
Error: spawn node ENOENT
at notFoundError (D:\Users\212399486\WebstormProjects\vue-hackernews-2.0-master\node_modules\cross-spawn\lib\enoent.js:11:11)
at verifyENOENT (D:\Users\212399486\WebstormProjects\vue-hackernews-2.0-master\node_modules\cross-spawn\lib\enoent.js:46:16)
at ChildProcess.cp.emit (D:\Users\212399486\WebstormProjects\vue-hackernews-2.0-master\node_modules\cross-spawn\lib\enoent.js:33:19)
at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "D:\\Users\\212399486\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v6.11.2
npm ERR! npm v3.10.8
npm ERR! code ELIFECYCLE
npm ERR! vue-hackernews-2.0# start: `cross-env NODE_ENV=production node server`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the vue-hackernews-2.0# start script 'cross-env NODE_ENV=production node server'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the vue-hackernews-2.0 package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! cross-env NODE_ENV=production node server
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs vue-hackernews-2.0
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls vue-hackernews-2.0
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! D:\Users\212399486\WebstormProjects\vue-hackernews-2.0-master\npm-debug.log
Screenshot of dist folder:
package.json scripts:
"scripts": {
"dev": "node server",
"start": "cross-env NODE_ENV=production node server",
"build": "rimraf dist && npm run build:client && npm run build:server",
"build:client": "cross-env NODE_ENV=production webpack --config build/webpack.client.config.js --progress --hide-modules",
"build:server": "cross-env NODE_ENV=production webpack --config build/webpack.server.config.js --progress --hide-modules"
},
Requires Node.js 7+
I managed to make it work without any problems.
$node -v
v8.3.0
$npm -v
5.3.0
Change the default port successfully :
To change the default port you have to edit package.json scripts:
dev: cross-env PORT=8888 node server
start: cross-env PORT=8888 NODE_ENV=production node server
Then running npm run dev
> vue-hackernews-2.0# dev /home/emx/so/vue-hackernews-2.0
> cross-env PORT=8888 node server
server started at localhost:8888
you are using a version of node that is not supported. Your error point to node v6.11.2 see below.
npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "D:\\Users\\212399486\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v6.11.2
npm ERR! npm v3.10.8
vue-hackernews-2.0 requires Node.js 7+. Upgrade your node to a 7+ version and you should be fine. I'll also recommend to update your npm version.
See the answers to this question for details about how to update your node version, if you need instructions.

Error when running "npm start"?

I have started with electron today and followed a guide to create a simple application browser window and I ran npm start in the command window and it displayed a lot of errors after I had entered it, I have posted the information below and my code.
Console error:
C:\Users\Administrator\Desktop\electron>npm start
> breef#1.0.0 start C:\Users\Administrator\Desktop\electron
> node index.js
module.js:471
throw err;
^
Error: Cannot find module 'app'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\Administrator\Desktop\electron\index.js:1:73)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v6.9.2
npm ERR! npm v3.10.9
npm ERR! code ELIFECYCLE
npm ERR! breef#1.0.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the breef#1.0.0 start script 'node index.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the breef package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node index.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs breef
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls breef
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! C:\Users\Administrator\Desktop\electron\npm-debug.log
Here is my package.json:
{
"name": "breef",
"version": "1.0.0",
"description": "A simple chat application.",
"main": "index.js",
"dependencies": {
"electron": "^1.4.12"
},
"devDependencies": {},
"scripts": {
"test": "breeftestcmd",
"start": "node index.js"
},
"keywords": [
"breef"
],
"author": "Sage & Sh4wn",
"license": "ISC"
}
Here is my index.js:
var app = require("app")
var browserWindow = require("browser-window")
app.on('ready', function() {
var mainWindow = new browserWindow({
width: 800,
height: 600
})
mainWindow.loadUrl('http://google.com');
})
If app is a module, it's missing in your package.json. If not, you should use a path to require it
var app = require('./app');
The guide you followed is out of date, as of Electron v1.0.0 the way built-in Electron modules are imported has changed. This is the correct way:
const {app, BrowserWindow} = require("electron")
You may also find it beneficial to read the previous question that covered this topic: What's the proper way to require in Node.js?

Categories

Resources