Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent inheriting packages from parent node_modules

First I will give some context to the problem.

I am developing an npm library. Inside the project folder, I have another folder called "example", for testing the library. The structure looks like below.

|- node_modules/
|- src/ 
|- example/
|   |- node_modules/
|   |- src/
|   |- package.json
|- package.json 

The root package.json has the dependency babel-jest. The example/package.json has the dependency react-scripts. When running react-scripts start inside example directory, it gives the following error,

Error screenshot


As far as I can understand, this is because, the package.json inside the example/ directory inherits (not sure if this is the right term) the dependencies of the root package.json.

That is, I can use a dependency installed in the root package.json, inside the src/ of the example/ This is convenient in some cases. But this is a blocker for my use case.

How can I prevent this behaviour? (without changing the directory structure)

Thank you.

like image 833
Pubudu Dodangoda Avatar asked Apr 27 '19 09:04

Pubudu Dodangoda


People also ask

Should I git ignore node_modules?

You should not include folder node_modules in your . gitignore file (or rather you should include folder node_modules in your source deployed to Heroku). If folder node_modules: exists then npm install will use those vendored libraries and will rebuild any binary dependencies with npm rebuild .

What is .staging folder in node_modules?

While doing npm install, inside node_modules . staging folder is getting created. Reasons: This is a temporary folder where the modules will be kept untill npm downloads all the modules specified in the package. json.

Does npm ci remove node_modules?

npm ci can only install entire projects at a time: individual dependencies cannot be added with this command. If a node_modules is already present, it will be automatically removed before npm ci begins its install. It will never write to package. json or any of the package-locks: installs are essentially frozen.

What is Package lock in node?

package.lock.json. It contains basic information about the project. It describes the exact tree that was generated to allow subsequent installs to have the identical tree. It is mandatory for every project. It is automatically generated for those operations where npm modifies either node_modules tree or package.


1 Answers

From what I understand, Dan Abramov suggests to use SKIP_PREFLIGHT_CHECK=true to work around this problem as there is no real fix.

like image 134
Matthieu Riegler Avatar answered Oct 08 '22 00:10

Matthieu Riegler