7

I have a library and I want to test it by doing a full install locally before publishing to npm. One of the things I want to test is if all the dependencies are part of the project. Because of how nodejs structures its module system though, (a nodejs module is searched for at every directory level) I do not have a good way to ensure that the module I am requiring came from the current folder's node_modules, and not the parent folder's node_modules.

Here is the project structure:

library-folder/
  package.json
  node_modules/
  testing/integration/
    package.json
    node_modules/
    test-install.js

I want to know if there is a way to tell nodejs to only require modules from the current directory, and ignore any parent directories.

andykais
  • 996
  • 2
  • 10
  • 27
  • Does this answer your question? [Issue with babel-jest dependency when running npm start in a React app](https://stackoverflow.com/questions/53089122/issue-with-babel-jest-dependency-when-running-npm-start-in-a-react-app) – Kia Kaha Jan 28 '21 at 15:32

2 Answers2

0

I do not have a good way to ensure that the module I am requiring came from the current folder's node_modules, and not the parent folder's node_modules.

For that, you can use require.resolve and check the path it returns.

fardjad
  • 20,031
  • 6
  • 53
  • 68
  • I don't believe this will work based on their documentation: "Each of these paths is used as a starting point for the module resolution algorithm, meaning that the node_modules hierarchy is checked from this location." I cannot check this atm but I will check it later today – andykais Oct 12 '19 at 15:50
  • @andykais Looks like you've read the description of `options.paths` field. The method description is written after the bullet points: `Use the internal require() machinery to look up the location of a module, but rather than loading the module, just return the resolved filename.` – fardjad Oct 12 '19 at 17:32
  • Thank you, you are right that this will get me partway there, but I think the issue I am trying to solve is not possible. Say I want to be sure my real deps are not devDeps. Even if I know that `testing/integration/test-install.js` is using the `integration/node_modules` folder, any dependencies of my library will still look in both `integration/node_modules` and `library-folder/node_modules` because each require location is just a starting point. I think my real solution is to just test in a sibling folder. – andykais Oct 14 '19 at 14:00
-1

I resolved this issues by creating a file in the child's root project folder.

File's name should be called like this: .env

The content: SKIP_PREFLIGHT_CHECK=true

Kia Kaha
  • 1,565
  • 1
  • 17
  • 39