I have a NodeJS project that was started long ago. It has many packages that were installed before I understood the --save flag. A few versions ago npm decided that it would delete packages that are not mentioned in package.json (an insane thing to do). This causes a terrible problem.
Now I understand --save and use it properly. However, I cannot figure out a way to update package.json with packages that are not listed.
Is there some way to cause npm or some other program to look at a project and add missing dependencies? I'd be happy enough if it completely recreated the dependency structure.
Suggestions?
UPDATE : npm 3.5+
Remove everything from package.json and run: npm init --yes.
This will recreate the package.json with dependencies, but not the devdependencies.
npm 3
If your're on Unix based systems, from inside your project root folder, with a package.json file already created (npm init, as you mentioned), run:
npm install $(ls node_modules/) --save
and it will reinstall the packages, and save them into package.json as dependencies
Related
I am new to React js, While using npm start after install the react not working,I tried every process but no use. Thanks in advance
Error
cricdost#0.1.0 start C:\xampp\htdocs\cd-web
react-scripts start
There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.
The react-scripts package provided by Create React App requires a dependency:
"webpack": "4.41.2"
Don't try to install it manually: your package manager does it automatically.
However, a different version of webpack was detected higher up in the tree:
C:\xampp\htdocs\node_modules\webpack (version: 4.39.2)
Manually installing incompatible versions is known to cause hard-to-debug issues.
If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.
To fix the dependency tree, try following the steps below in the exact order:
Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
Delete node_modules in your project folder.
Remove "webpack" from dependencies and/or devDependencies in the package.json file in your project folder.
Run npm install or yarn, depending on the package manager you use.
In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:
If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
This may help because npm has known issues with package hoisting which may get resolved in future versions.
Check if C:\xampp\htdocs\node_modules\webpack is outside your project directory.
For example, you might have accidentally installed something in your home folder.
Try running npm ls webpack in your project folder.
This will tell you which other package (apart from the expected react-scripts) installed webpack.
If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That would permanently disable this preflight check in case you want to proceed anyway.
P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! cricdost#0.1.0 start: react-scripts start
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the cricdost#0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\XSC-10261\AppData\Roaming\npm-cache_logs\2020-02-20T04_56_31_650Z-debug.log
I had faced the same error. The solution that I found is to do the step-6 as described in Error message. That is to delete "webpack" folder of node_module folder under your root projects directory. For example
Assume that you installed "npm install create-react-app" in D:\Projects folder.
And then you ran "npx create-react-app firstproject" in D:\Projects folder. So, your actual react project folder is "D:\Projects\firstproject".
So, what I want to tell is don't delete "webpack" folder in "D:\Projects\firstproject\node_module" directory. Instead, delete "webpack" folder in "D:\Projects\node_module" directory.
And then, you can run "npm start" command in your actual react project directory "D:\Projects\firstproject".
I hope, this will work for you.
I've recently upgraded to node8 (using nvm) and a weird things started to happen in one of the project I'm working on.
Node v8.11.1
Npm 5.6.0
I cd inside my project having a packages.json.
For dev purposes I wanted to remove some packages previously installed but I didn't want to modify my packages.json.
Whenever I run either npm uninstall package-name or npm install packages-name they both try to install all the missing packages listed in packages.json!
I am explicitly NOT passing the --save or --save-dev flag but still npm ignores that.
I think it might have to do with package-lock.json file?
Q: How to tell npm I do not want it to mess up with other packages or modify my packages.json?
Does Webpack install -g, automatically install to nodeJS's package.json? Or is this only for local installs?
So I'm tired of trying to find workarounds for require() is not defined. Meaning I would need a module loader for my project to include modules client side. Well, I've downloaded the famous 'webpack' module loader, globally
npm install -g webpack
and I noticed it didn't install to "devDependencies" in my package.json file. But I also install webpack-dev-server, but locally,
npm install webpack-dev-server --save-dev
and it was saved into my package.json. Was this saved because I used --save-dev or because I installed locally?
I'm getting an error stating my webpack module I downloaded doesn't have a configuration file, so I'm assuming I install webpack wrong, and maybe it shouldn't have been installed globally. Please help with the understanding of globally and locally, as well as why this -g webpack install didnt get saved to the package.json?
The command npm install --gloabl will install a package in global scope and make it's bin command available to you globally. This has nothing to do with the folder or project your are in right now. That means a global install will not leave anything in any package.json files.
Learn more about npm install: https://docs.npmjs.com/cli/install
npm install -g webpack will save your files in your OS file system.
npm install webpack-dev-server --save-dev will save your package in your project directory inside a folder called node_modules.
The later one will make an entry in package.json file so that next time you can install all dependdencies with just npm install command. This command installs all your packages listed in package.json.
Missing Config file: Its looking for a file called webpack.config.js. More info can be found here: https://webpack.js.org/configuration/
So I installed some modules with npm and others I just copied and pasted into the node_modules folder. One module I had to hack.
Now when I perform npm install for a new module, it deletes some of the modules and overwrites the hacked one to its original state.
It feels like I didn't run npm init early enough in development to properly set my package.json file from the start and now when I do run npm init it only picks up some of the modules.
How can I get everything synced up again? Is there a way to take a node_modules folder and get npm to become familiar with all the modules again? Also, is there any way to prevent the hacked one from going back to its original state?
Thank you!
Here's an answer to a similar question
You can fork the repo of the "hacked" module and include the forked repo in your package.json file. As for the "copied" modules, you can either copy them outside your node_modules folder and import using relative paths, or install it as a local npm package.
I added a couple of libraries to my package.json file, but now I found out I no longer need them
Is there some command line parameter to pass to npm install to tell it to remove no longer needed packages? that is, those packages in node_modules that are no longer in package.json
Or shall I just remove the node_modules folder and run npm install once again... (I was hoping for a smarter solution)
--
I found out that npm ls correctly recognizes no longer needed libraries as extraneous
You can do npm prune to remove extraneous packages. Link to docs.