Module not found in Electron / Atom Shell - javascript

I'm new to npm, node and Electron.
My Folder looks like:
-package.json
-index.html
-main.js
-js/myStuff.js
-node_modules
In the file myStuff.jsI have var chokidar = require('chokidar'); but it shows then module not found error.
In the index.html I included the myFile.js like normaly in a script tag.
I did read how node is looking for modules. But that doesn't help cause then it should find it, cause it's looks in all parent dirs for the node_modules folder and then in there for a chokidar folder and in that for the index.js file, which is there.
What am I doing wrong?
It works now, but i have no idea why..

In contrast to other node apps, electron doesn't automatically set your NODE_PATH to global (i.e. /usr/bin) or local (e.g. node_modules) folders. So you need to export it manually:
NODE_PATH=/path/to/node_modules electron my_app

Related

Webpack path resolution

I have a SFCC project with multiple sites in one repository. Here is a brief overview of the structure of the codebase:
core
cartridge
js
something
dialog.js
modal.js
app.js
shirt.js
site_1
cartridge
js
page
file1.js
app1.js
site_2
cartridge
js
page
file2.js
app2.js
site_3
cartridge
js
page
file3.js
app3.js
build
site1.js
site2.js
site3.js
My webpack entry points are the site1, site2, and site3 js files. In each of those files, I am importing the respective app.js file. Each of these app.js files has their own files that they require for that particular site. When I run webpack I get the Module not found error.
In the app.js files, I am requiring outside dependencies like so:
require('./dialog.js')
The error for this example would be something like this:
ERROR in ../[site]/cartridge/js/app.js
Module not found: Error: Can't resolve './dialog' in '/path/to/repository/[site]/cartridge/js'
Any idea how I can concatenate the correct path to to these files?
Create an alias to core directory. Then require/import using [alias]/js/something/dialog.js

Require local node modules with absolute paths doesn't work on Windows

I'm developing a Node application with several modules.
My node-application is transpiled with Babel to /dist/app.
This is an example-structure
.
|- main
| |- config.js
| |- factories
| | |- example.js
This is config.js:
const ex = require("/main/factories/example");
I launch config.js with node dist/app/main/config.js.
The resulting error is:
Error: Cannot find module '/main/factories/example";
However when using const ex = require("./factories/example"); it works as it should.
This problem only occurs on Windows (testing Windows 8.1), both OS X and Linux are fine.
What is the problem here?
It's the other way around, the code works as expected on Windows. /main/factories/example means C:/main/factories/example on Windows. It works on OSX/Linux because of some reason (NODE_PATH being set probably). I'd suggest to not rely on a side effect to have a working code and don't use relative path either (entirely dependant on the working directory), you should build your absolute path like this:
const ex = require(__dirname + "/factories/example");
I think maybe the NODE_PATH cause this your issue. Refer to this article Better local require() paths for Node.js. There are several ways to require local node modules
The Symlink.
Create a symlink under node_modules to your app directory:
Linux: ln -nsf node_modules app
Windows: mklink /D app node_modules
The Module
Install some module:
npm install app-module-path --save
In your app.js, before any require() calls:
require('app-module-path').addPath(__dirname + '/app');
In your very/far/away/module.js:
var Article = require('models/article');
The startup script
Linux, create app.sh in your project root:
#!/bin/sh
NODE_PATH=. node app.js
Windows, create app.bat in your project root:
#echo off
cmd.exe /C "set NODE_PATH=.&& node app.js"
Hope it could help you.

Submodules in Browserify

/foo
/bar.js
/foobar.js
/index.js
In node.js if you a require a directory (require('foo')), it would look into that directory and find an index.js file and return whatever exports I have in that file, so I can just bundle up the contents of the directory in an index.js file. Therefore, I dont have to require bar and foobar separately if index.js already includes them.
However this approach doesn't work with browserify. It seems like only thing browserify understands is relative paths.
/star
/star.js
/starfoo.js
/index.js
/foo
/bar.js
/foobar.js
/index.js
In other words I want to separate my project into submodules, call require on a directory as if I am calling require on a dependency. For example in the star.js file I want to be able to require('foo') and get the exports of bar.js and foobar.js (as long as /foo/index.js is importing bar.js and foobar.js)
edit:
Looking at the react source code, i think what i am describing is possible
https://github.com/facebook/react/blob/master/src/isomorphic/ReactIsomorphic.js
In this file they call require on React-Children in line 14.
var ReactChildren = require('ReactChildren');
However react children is couple directories deeper.
https://github.com/facebook/react/blob/master/src/isomorphic/children/ReactChildren.js
Where is this mapping defined?
There isn't a way to specify a base directory because that's not how node modules work. If you want non-relative paths, use the node_modules directory. If you want require('foo') to work from any directory, just make a symlink from your project root:
ln -s foo node_modules/foo

How to load a task using loadNpmTask in gruntfile if module is in different directory

Trying to load module: grunt.loadNpmTasks('grunt-express-server'); from an external directory.
Get an error: task .... does not exist. Have you loaded it?
Directory structure:
client/
node_modules
gruntfile
dev_server/
node_modules/
grunt-express-server
So my question is: how do you run a grunt-task using a node-module which is stored in a external directory?
You will need to use grunt.task.loadtasks pointing it to the task directory which you want to load the tasks.
In your case:
grunt.loadTasks('../dev_server/node_modules/grunt-express-server/tasks');
If you check grunt's master on github, at line 325 of task.js it requires the taskfile (.../tasks/express.js) located in the filepath you passed as parameter.
// Load taskfile.
fn = require(path.resolve(filepath))
Edit
If you're wondering if you can relocate the grunt's path to node_modules, check out this question

require a file outside of node_modules with grunt

I am trying to require a node module that is used in my app into my gruntfile.js but whatever I do i get : Cannot find module 'appjs/config
In the gruntfile I simply do : var myconfig = require('appjs/config');
But it just don't want to load, is there a way to import this files easily? I also tried various path but appjs is at the root where the gruntfile is.
Have you tried this?
var myconfig = require('./appjs/config');
Seems like this should work, if appjs is in the same directory as your gruntfile.

Categories