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 module not recognized #46

Closed
josebalius opened this issue Nov 19, 2014 · 38 comments
Closed

Node module not recognized #46

josebalius opened this issue Nov 19, 2014 · 38 comments

Comments

@josebalius
Copy link

Hi,

I have react-router installed as an npm module, and it's in the node_modules folder, but everything i run flow, i get the same error

/components/content/item/ContentItemList.js:8:14,36: react-router
Required module not found

I tried adding the node_modules folder to .flowconfig but no luck, any ideas? The node_modules folder is at the same level as .flowconfig

@nnarhinen
Copy link

As I noticed in #48 and #49 flow is using some own implementation following requires.

It does not find modules from node_modules if I try to use some sub paths of the modules.

In an attempt to workaround #48 I removed the NODE_PATH hack and instead symlinked some directories to node_modules(like ln -s ../lib/models)

After that require('models/somemodel') makes flow complain /path/to/some/file.js ... somemodel.js Required module not found

@josebalius
Copy link
Author

The thing is this a standard module in the node_modules folder in the same directory I ran flow init, not sure why it cannot find it.

One I noticed is that the module doesn't have an index.js but instead a dist folder

@avikchaudhuri
Copy link
Contributor

Our algorithm for finding node modules basically follows the spec at: http://nodejs.org/api/modules.html

(We don't do anything special for dist.)

@josebalius
Copy link
Author

@avikchaudhuri thanks for getting back to me, the react-router package json has the following

{
  "name": "react-router",
  "version": "0.10.2",
  "description": "A complete routing library for React.js",
  "main": "./modules/index",
  "repository": {
    "type": "git",
    "url": "https://github.com/rackt/react-router.git"
  },
  "homepage": "https://github.com/rackt/react-router/blob/latest/README.md",
  "bugs": {
    "url": "https://github.com/rackt/react-router/issues"
  },
  "directories": {
    "example": "examples"
  },

Notice the main property, (i know i said dist, but it seems to be a modules folder) is flow handling this? I'm a bit rusty on the modules spec but I will take a look and see if this is valid.

@tcoopman
Copy link
Contributor

Similar problem with hapi

hapi Required module not found

In their package.json file they have:

"main": "index"

@tcoopman
Copy link
Contributor

Changing "main": "index" to "main":"index.js" solves the issue.

@josebalius maybe you can try that with react-router too?

@tcoopman
Copy link
Contributor

@josebalius changing package.json to have the main include .js fixes this too for react-router.

@avikchaudhuri
Copy link
Contributor

If this is resolved, can we close the issue?

@KnisterPeter
Copy link
Contributor

I have the same problem for the dat-gui package. In the package.json is no main attribute and there is an index.js file which should be used by default.
So there is still something wrong.

@KnisterPeter
Copy link
Contributor

The missing "main" attribute in the package.json is the problem. If I add it, then it does work.
This should be fixed since "main" is optional if there is a index.js in the root folder of the module.

@nnarhinen
Copy link

Also it seems if something is in node_modules flow expects it to have a package.json file This is not always the case and shouldn't be treated like that. Node is completely happy to resolve require('foo/bar/baz') to node_modules/foo/bar/baz.js

@KnisterPeter
Copy link
Contributor

@nnarhinen This is not entirely right. flow searches for a package.json, if there is one it only looks for the main attribute. If that is missing the resolving stops. If there is no package.json it looks for an index.js file, if that is missing the resolving fails.

@nnarhinen
Copy link

@KnisterPeter ok, well that's not how node works in this case. nodejs resolves foo/bar/baz just happily to node_modules/foo/bar/baz.js. that's how flow should work too, IMO.

@KnisterPeter
Copy link
Contributor

@nnarhinen You are right with that. I just wanted to explain the flow implementation.

@josebalius
Copy link
Author

@tcoopman yep that fixed the error, but flow should be able to handle this right?

@tcoopman
Copy link
Contributor

@avikchaudhuri @josebalius I believe that flow should indeed be able to handle this.

Maybe someone can add this based on #78

@KnisterPeter
Copy link
Contributor

@tcoopman @avikchaudhuri @josebalius I could look into this later this day and extend my pull request to be more node-resolving algorithm compliant.

@tcoopman
Copy link
Contributor

@KnisterPeter 👍

@KnisterPeter
Copy link
Contributor

@avikchaudhuri @tcoopman @josebalius The pull request is extended with cases adding '.js' to the required path if no other resolution is found.
But I guess the code needs a review, because I'm not an expert on OCaml 😄

@josebalius
Copy link
Author

@KnisterPeter thanks! keen to try it. I just upgraded to the new version, and it seems that @tcoopman's solution doesn't work anymore :(

@KnisterPeter
Copy link
Contributor

Which exactly do you mean? There are the node_tests which should contain all possible solutions. Which one is failing? Or which one is missing?

@josebalius
Copy link
Author

@KnisterPeter your PR hasn't been merged yet, so I am pretty sure it's not your solution, but whatever code they push for the 0.1.1 release doesn't pick up any of my modules :(

@josebalius
Copy link
Author

Seeing this

client/routes.js:5:13,28: react.js
Required module not found

client/routes.js:6:14,36: index.js
Required module not found

Which comes from:

var React = require('react');
var Router = require('react-router');

Each of those modules have a main property in their package.json but for some reason in 0.1.1 flow can't find it. It was able to find react in 0.1.0, and I gotten react-router to work by adding .js to the main property, but none of them work at the moment for me.

@avikchaudhuri
Copy link
Contributor

cc @lordhoto regression in 0.1.1?

@KnisterPeter
Copy link
Contributor

At least with my patch applied all the node_tests run well. I'll executed them with 'flow check --all'

@tcoopman
Copy link
Contributor

@josebalius I've tested my workaround in 0.1.1 for react-router and it works for me. React is also correctly imported.

Are you sure you don't have anything else that's changed?

@josebalius
Copy link
Author

@tcoopman Yeah I am pretty sure, what's the best way to reset flow completely just in case?

@josebalius
Copy link
Author

All, my mistake i added the node_modules folder to the ignore property in .flowconfig, I removed it and everything worked.

@nnarhinen
Copy link

Any updates on these resolving problems? For me they are a show-stopper if I can't use flow on a browserify-based application.

@KnisterPeter
Copy link
Contributor

I've updated my patch yesterday morning. I think its finished now.

@nnarhinen
Copy link

@KnisterPeter Please give me a quick brief how I can test your patch and I'll let you know how it behaves on my existing project

@KnisterPeter
Copy link
Contributor

@nnarhinen If you don't rely on any other latest commits you could clone my fork (the patch is in the master branch), run make to compile flow and all should be ready to run. Otherwise you just need to clone the current flow master and add my pull-request on top and then compile flow.

@nnarhinen
Copy link

Ok @KnisterPeter I installed your fork. Here's the result:

$ ../../opensource/flow/bin/flow 

/Users/nnarhinen/projects/myproject/src/backend/models/Model.js:5:17,44: bookshelf.js
Required module not found

/Users/nnarhinen/projects/myproject/src/backend/models/Model.js:9:20,55: index.js
Required module not found

/Users/nnarhinen/projects/myproject/src/backend/models/Model.js:10:15,45: openssl.js
Required module not found

The related requires are:

var Bookshelf = require('backend/bookshelf');
var foos = require('backend/foo/foos'); // the path is a directory
var openssl = require('backend/foo/openssl');

Output of ls -l node_modules/backend

lrwxr-xr-x  1 nnarhinen  staff  14 19 Mar 12:40 node_modules/backend -> ../src/backend

@KnisterPeter
Copy link
Contributor

@nnarhinen Can you please output 'ls -l node_modules/backend/' (Note the slash at the end which lists the content of the node_modules/backend folder)

@nnarhinen
Copy link

Relevant parts:

-rw-r--r--   1 nnarhinen  staff   176  7 Mar 06:16 bookshelf.js
drwxr-xr-x   5 nnarhinen  staff   170 24 Mar 15:13 foo

(no package.json nor index.js)

ls -l node_modules/backend/foo/

drwxr-xr-x  4 nnarhinen  staff   136 21 Mar 21:21 foos
-rw-r--r--  1 nnarhinen  staff  1189 24 Mar 15:13 openssl.js

ls -l node_modules/backend/foo/foos/

-rw-r--r--  1 nnarhinen  staff  3597 21 Mar 21:21 index.js

@KnisterPeter
Copy link
Contributor

@nnarhinen The require('backend/bookshelf') you do is exactly like the 'node_tests/path_node_modules' unit test which does run successful on my setup. Can you please verify if it works for you or if it fails? Use 'flow check --all' to run the test case.

avikchaudhuri added a commit that referenced this issue Nov 26, 2014
FIX #46: Fallback to index.js if no main attribute in package.json
@nnarhinen
Copy link

@KnisterPeter it's the symlinks that make it fail. I did

cd node_tests/path_node_modules
mv node_modules/bar_lib lib
cd node_modules
ln -s ../lib bar_lib
cd ..
../../bin/flow check --all

and it fails with

/Users/nnarhinen/opensource/flow/node_tests/path_node_modules/foo.js:1:9,38: bar.js
Required module not found

@cristian-sima
Copy link

Anything on this issue ?

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

No branches or pull requests

6 participants