-
-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to configure NODE_PATH for the Electron instance? #44
Comments
Seems like this is the same issue (but not solution): electron/electron#11 EDIT: well even if that |
To paraphrase your issue: You'd like to load a file for each test file execution, is that right? (e.g. auto-insert If that's the case, then we need to reframe your understanding a bit =/ Karma is a browser test runner, not a Node.js one. We need to look at everything through the lens of "what if this was executing in Chrome or Firefox"? (where we don't have sweet features like Once we reframe it, then we realize we need to find support for this either in
Documentation:
Example:
If you'd like something even more custom, Documentation:
Example: |
That's not exactly my issue (but that is one way I thought I could possibly solve my issue). The issue is that when a meta-package of mine (which contains karma and karma-electron, and this meta package has other things like TypeScript build steps, Webpack bundling steps, etc) is not installed normally in my project, but symlinked, then Node which is running in karma-electron's Electron instance will no longer find my project's node_modules. To make it more clear, if I run Module {
...
paths: [
'/home/trusktr/src/lume+umbrella/packages/builder-js-package/node_modules/electron/dist/resources/default_app.asar/node_modules',
'/home/trusktr/src/lume+umbrella/packages/builder-js-package/node_modules/electron/dist/resources/node_modules'
],
...
} But the thing is, my project is actually located at
So Node will not find This is happening, because I tried to switch to Lerna for multi-project management, which symlinks projects together. Now karma and karme-electron are not directly inside of
This I know, which is exactly why I'm using karma+karma-electron (because with Jest I can only get fake browser APIs which have bugs or missing features). As for the rest of your previous comment, I think we can ignore it because it was misunderstanding. I just want EDIT: Or, wait, maybe I misunderstood: Are you saying, that if I add the files to karma |
Alrighty! I think there might be an easy fix for this in karma-electron. I found this line:
Maybe, we can add an option for karma-electron called Let me see about a pull request. |
In case I can make it more clear, in my project, I install some NPM packages: npm install mkdirp Now in my test code, this normally works fine: const mkdirp = require('mkdirp')
describe('foo', () => {
it('bar', () => { /* ... use mkdirp ...*/ })
}) But the problem happens only when karma-electron is no longer in my project node_modules (because it is in a symlinked location), and in this case I get an error
|
Thank you for your responsiveness on this by the way! You are more responsive than most any other project maintainer. |
Ah, I see. Thanks for explaining the issue in more detail =) A PR won't be necessary, more detailed options coming up in next comment |
Great find! The This template is loaded via a To reiterate, First off, I recommend giving it a try with a specific test file. To reuse your // I prefer to use `concat` to avoid contaminating the original array
module.paths = module.paths.concat(['path/to/custom/node_modules']);
// Normal `mkdirp` execution
const mkdirp = require('mkdirp')
describe('foo', () => {
it('bar', () => { /* ... use mkdirp ...*/ })
}) If that works fine, then we've got 2 options for you:
|
Aha!! So, I couldn't get the above const path = require('path')
process.env.NODE_PATH = path.resolve(process.cwd(), 'node_modules') + ':' + process.env.NODE_PATH
require('module').Module._initPaths()
const mkdirp = require('mkdirp')
describe('TODO, tests', () => {
it('needs to be done', () => {
expect(mkdirp).toBeInstanceOf(Function)
})
}) thanks to this hint on StackOverflow: https://stackoverflow.com/a/33976627/454780. |
Great! Glad to hear it all worked out =) |
Well so far that sample works, but I still want to avoid writing that inside every test file. Let me try your Would that trick with NODE_PATH be worth putting in the mustache template to ensure that |
The current code is meant to match normal Node.js resolution which
traverses upwards based off the current filepath
https://nodejs.org/api/modules.html
I don't believe using cwd in that resolution is a common setup so I'd
prefer to leave it as a one off setup
If I'm mistaken in how your symlink is setup and it should match normal
Node.js behavior, please correct me
…On Sat, Mar 7, 2020, 8:08 PM Joe Pea ***@***.***> wrote:
Well so far that sample works, but I still want to avoid writing that
inside every test file. Let me try your files/included suggestion.
Would that trick with NODE_PATH be worth putting in the mustache template
to ensure that path.resolve(process.cwd(), 'node_modules') is always
available, even when symlinked?
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#44?email_source=notifications&email_token=AAG4KWAVY5VTUBRZP5K36XDRGMK3DA5CNFSM4LDR7SFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEOEL6UI#issuecomment-596164433>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAG4KWAFTUIOOP4HAKNV6RTRGMK3DANCNFSM4LDR7SFA>
.
|
Ugh, missed anchor link for page
https://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders
…On Sat, Mar 7, 2020, 8:48 PM Todd Wolfson ***@***.***> wrote:
The current code is meant to match normal Node.js resolution which
traverses upwards based off the current filepath
https://nodejs.org/api/modules.html
I don't believe using cwd in that resolution is a common setup so I'd
prefer to leave it as a one off setup
If I'm mistaken in how your symlink is setup and it should match normal
Node.js behavior, please correct me
On Sat, Mar 7, 2020, 8:08 PM Joe Pea ***@***.***> wrote:
> Well so far that sample works, but I still want to avoid writing that
> inside every test file. Let me try your files/included suggestion.
>
> Would that trick with NODE_PATH be worth putting in the mustache template
> to ensure that path.resolve(process.cwd(), 'node_modules') is always
> available, even when symlinked?
>
> —
> You are receiving this because you modified the open/close state.
> Reply to this email directly, view it on GitHub
> <#44?email_source=notifications&email_token=AAG4KWAVY5VTUBRZP5K36XDRGMK3DA5CNFSM4LDR7SFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEOEL6UI#issuecomment-596164433>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAG4KWAFTUIOOP4HAKNV6RTRGMK3DANCNFSM4LDR7SFA>
> .
>
|
Alright, it worked! I changed my files: [
// include the augment-node-path.js file first
path.resolve(__dirname, 'augment-node-path.js'),
// include all the test files after augment-node-path.js (order matters)
{pattern: 'dist/**/*.test.js', watched: false},
], And // Most of the time, this doesn't do anything. But when karma-electron is
// symlinked into the project's node_modules, the regular upward lookup for
// node_modules will not find the project's node_modules, so this explicitly
// adds that to NODE_PATH. The karma.config.js file ensures this is loaded
// before all test files.
const path = require('path')
process.env.NODE_PATH = path.resolve(process.cwd(), 'node_modules') + ':' + process.env.NODE_PATH
require('module').Module._initPaths() |
I agree with that sentiment, but maybe we should consider the following: With normal Node usage, the user runs But for whatever reason, with karma-electron existing in a folder symlinked into the project node_modules it caused As an example, while karma-electron's Electron was failing to find anything in I wasn't trying to add a specific non-standard location to my |
I hear you're upset that I'm refusing to allow support for loading modules
based off of cwd location
Please consider the following
- REPL load files relative to the terminal as that's where the would-be
file that someone is writing is being executed
- Using cwd leads to inconsistent behavior. For example, if I open a
terminal on my desktop and run ~/github/my-project/test.sh, I would expect
it to load files in said folder, not from my desktop (cwd)
I recommend reviewing what other projects do for similar setups. If I
recall correctly, it's usually been a hardcoded relative filepath since
each project might locate files differently. Eg
- app
- linked-project
- node modules
- test (files inside folder would load ../linked-project)
For linked projects that are using npm link, those are really expected to
behave link good node modules that don't exit the node modules container as
they don't know how deep they're nested
(If we're loading from a file in a node module, then there are tricks like
require.resolve and require('my-module/path/to/file') that can be used for
avoiding export pass through headaches)
I hope this info helps
…On Sun, Mar 8, 2020, 12:50 PM Joe Pea ***@***.***> wrote:
I don't believe using cwd in that resolution is a common setup so I'd
prefer to leave it as a one off setup
I agree with that sentiment, but maybe we should consider the following:
With normal Node usage, the user runs node in their project, and it
always resolves things in project/node_modules.
However, with karma-electron symlinked into the project, it for some
reason causes node (inside Electron) not to look at project/node_modules
which seems to be a behavior that doesn't match plain node usage.
As an example, while I had this issue, I could run node in my terminal,
and in the repl I could successfully execute require('mkdirp'). But then
it failed in Electron, although one would think it would behave the same.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#44?email_source=notifications&email_token=AAG4KWGCHFCTLRRGJQY5MGDRGQAJNA5CNFSM4LDR7SFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEOE7RBA#issuecomment-596244612>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAG4KWF57O75YYK5WTCJ3V3RGQAJNANCNFSM4LDR7SFA>
.
|
That may depend on expectations, but I see what you mean. One thing is for sure though:
So there definitely is some problem that the end user shouldn't have to worry about. The question is, what's the solution? |
I think there's a false dichotomy that you seem to be looping on =/ I've got 2 immediate thoughts on hopefully resolving this:
|
Well I am running both Or in other words, if I run Just to clarify that a little more, I am not running What I can do is provide a more clear example of my folder structure, and how things are symlinked, so that I can show that where karma is located shouldn't matter, but that rather the module resolution should happen relative to the files being executed (test files for example), which doesn't seem to be the case, and instead it seems to depend on the location of I need to find some time to show more clearly how my filesystem is laid out... |
Ah, I see. Maybe I'm misinformed of how |
I haven't had the time to make an example, but to try and describe it simply, suppose we have this folder structure:
where const someLib = require('some-lib/index.js')
console.log(someLib) and assuming that the current working directory is If I run If I run This problem only happens if When dependencies are not linked, module resolution works as expected (which seems to indicate that module resolution happens based on the actual location of When karma dependencies are linked and live somewhere outside of The workaround that I used is to add to It seems that Electron running via karma-electron is not performing module resolution the same as when running |
I'm quite busy the next 2 nights so I don't have time to parse/dig into all of this. I'll give it a read by the end of the weekend =) |
Alright, so that explanation is great and well laid out. That being said, trying to reproduce that can either be very quick or very slow/painful by trying to guess what went went (and even if it's quick, it might not be the issue you're describing). I definitely need a demonstration repo/similar to have a version of "here's it working in Node", "here's it not working in Electron" I'll re-open the issue for now but that demo repo is needed to move forward =/ |
No rush, it took me more than a year to circle back with a reply. :) I'll try to make a repo (I'll take my repo and strip everything out except one file, and hopefully that'll work). No guarantees on a timeline though. :) |
When karma-electron is symlinked into a project, the upward search path for
node_modules
folders may not include the application where we are running tests, therefore the tests will not be able torequire
dependencies because they won't be found.I tried running
karma
withNODE_PATH=
pwd/node_modules:$NODE_PATH karma ...
in case this scenario happens (it isn't needed if karma-electron is under the project node_modules).But this
NODE_PATH
does not propagate into the Electron instance.Do you know how to set
NODE_PATH
(or something similar) for the Electron instance?The text was updated successfully, but these errors were encountered: