Skip to content
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

NODE_PATH does not work #515

Closed
inyono opened this issue Feb 6, 2016 · 10 comments
Closed

NODE_PATH does not work #515

inyono opened this issue Feb 6, 2016 · 10 comments

Comments

@inyono
Copy link

inyono commented Feb 6, 2016

We are using NODE_PATH=. in our app to improve local require(). AVA (version 0.11.0) has problems with it, though.

flat-file structure:

// foo/index.js
export const foo = 'bar'
// test/foo.js
import test from 'ava'

import { foo } from 'foo'

test('foo should be bar', t => {
  t.same(foo, 'bar')
})
> NODE_PATH=. ava

  1 failed

  1. foo should be bar

  t.same(foo, 'bar')
         |
         undefined

  AssertionError: undefined === 'bar'
    Test.fn (test/foo.js:6:5)

nested-file structure

// foo/bar/index.js
export const foo = 'bar'
// test/foo.js
import test from 'ava'

import { foo } from 'foo/bar'

test('foo should be bar', t => {
  t.same(foo, 'bar')
})
> NODE_PATH=. ava

Error: Cannot find module 'foo/bar'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:289:25)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/Users/inyono/Projects/flitt/test/foo.js:9:12)
    at Module._compile (module.js:435:26)
    at Object.extensions.(anonymous function) [as .js] (/Users/inyono/Projects/flitt/node_modules/ava/node_modules/require-precompiled/index.js:13:11)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/Users/inyono/Projects/flitt/node_modules/ava/lib/test-worker.js:85:1)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:136:18)
    at node.js:963:3

  ✖ test/foo.js exited with a non-zero exit code: 1
@inyono inyono changed the title NODE_PATH does not work with subdirectories NODE_PATH does not work Feb 6, 2016
@jamestalmage
Copy link
Contributor

Conclusion #4 from that article:

Setting application-specific settings as environment variables globally or in your current shell is an anti-pattern

That said, not sure why this would not work. The NODE_PATH environment variable must be being ignored somehow.

@inyono
Copy link
Author

inyono commented Feb 6, 2016

I'm not super happy with the NODE_PATH solution myself, but it fits our use case quite well (especially ES6 modules hinder some other viable solutions). We didn't run into problems with mocha though.

If it's a beginner-friendly issue, I'd be glad to help.

@jamestalmage
Copy link
Contributor

If it's a beginner-friendly issue

I honestly don't know if it is. Feel free to give it a go. I would start by checking that neither this bit or require-precompiled are the problem.

@inyono
Copy link
Author

inyono commented Feb 6, 2016

I will give it a shot tomorrow, thanks for the pointers.

@ingro
Copy link
Contributor

ingro commented Feb 8, 2016

I'm stuck with the same problem, @inyono did you find a workaround or the cause of this issue?

@ingro
Copy link
Contributor

ingro commented Feb 8, 2016

I think I've got something, as @jamestalmage suggested inside test-worker.js module:

module.constructor._nodeModulePaths = function () {
    var ret = oldNodeModulesPaths.apply(this, arguments);
    ret.push(nodeModulesDir);
    return ret;
};

Seems like ret doesn't contain the additional paths specified with the NODE_PATH env variable:

[ 'D:\\projects\\app\\node_modules\\babel-core\\node_modules',
  'D:\\projects\\app\\node_modules',
  'D:\\projects\\node_modules',
  'D:\\node_modules',
  'D:\\projects\\app\\node_modules\\ava\\node_modules' ]

I tried naively to do this:

module.constructor._nodeModulePaths = function () {
    var ret = oldNodeModulesPaths.apply(this, arguments);
    ret.push(nodeModulesDir);
    if (process.env.NODE_PATH) {
        const additionalPath = path.join(process.cwd(), process.env.NODE_PATH);
        ret.push(additionalPath);
    }
    return ret;
};

But the problem is that process.cwd() points to the directory where the test file is, and not on the root of my application, so that didn't work.

A ugly solution could be to seth NODE_PATH as absolute but that would have a lot of downsides. I did not fully understand how the code inside test-worker.js gets invoked, if someone can clarify it maybe the solution could be at hand.

@inyono
Copy link
Author

inyono commented Feb 8, 2016

Yeah, I it works with absolute paths. Considering https://gist.github.com/branneman/8048520#6-the-hack, I looked into _initPaths(), but initPaths() also works with the relative path and does not create an absolute one...

@inyono
Copy link
Author

inyono commented Feb 8, 2016

@inyono
Copy link
Author

inyono commented Feb 8, 2016

See PR. I found a fix that at least works in my minimal example. Because I have failing tests in both my branch and on master, I guess I did something wrong, though ;)

@ingro
Copy link
Contributor

ingro commented Feb 9, 2016

I tried another approach, hope one of them could be merged :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants