npm init doesn't get all packages in node_modules folder - npm

I have a project with a node_modules folder. Something unexpected happened and i had to delete my package.json and package-lock.json files.
Now i need to get my project's packages and dependencies etc.. into a new package.json file.
Unfortunately, running npm init doesn't get all packages that are in the node_modules directory.
For example, react and react-dom are installed and present in node_modules but don't get included in the generated package.json file by npm init; alongside other packages.
What can i do to make all my packages appear in package.json and also if possible in package-lock.json ?

Related

How does react-scripts install extra dependencies?

To reproduce what I'm talking about
Create an empty directory
cd into the directory and run npm init
run npm install react-scripts
look at the node_modules directory. react-scripts exists inside node_modules, but it also installs many other dependencies required to run a project with create-react-app.
Looking at react-scripts directory in node_modules, I don't see any pre or post install scripts. I do see a react-scripts/bin/react-scripts.js script that im assuming is the entrypoint for the code installing these extra dependencies.
How is that file being run?

Versioning for private repository GitLab

When I do npm install, in node_modules I see the same files, in shared folder, I only get new files when I delete node_modules folder and package-lock.json file and do npm install. How can I make it work properly and get the last version of the files after each npm install?
package.json
dependencies: {
"shared": "git+ssh://git#git.domain.com:group/shared.git#master",
}

Bitbucket NPM private package not install dependencies

I have my own NPM package in bitbucket as private repository which I installed in my main project as following:
"devDependencies": {
"my-package": "git+ssh://git#bitbucket.org/{name}/my-package.git"
}
This works like a charm, but there's a problem with the package itself. It contains a package.json with its own dependencies, but my main NPM is not installing this, it does not seem to keep in consideration what my package's package.json contains.
E.g: I am now missing packages that my own packages requires.
What can I do to make NPM always install my package packages defined in package.json?
Structure wise:
MyApp
- package.json (I run npm install on this one)
- some other php files..
- node_modules
- my-package
- package.json <-- This contains dependencies, which are not installed
Solved it, problem was that we defined our packages in the devDependencies, which should be in the dependencies.

Load an npm package from local directory without copying unnecessary files/folders such as node_modules

Let's imagine I have to develop an npm package, my-package, which needs to be imported and used by my-project.
While developing my-package I need to install and use other packages from npm. At the end of the development of my-package, I bundle all the relevant files in the dist directory. Since I do not want to have all the files in the node_modules folder as part of the package published, I specify in .gitignore (or .npmignore) to exclude node_modules folder. So the final structure of the folder of my-package looks something like
my-package
dist
... // all the code of the package to be distributed
node_modules
... // all the node modules
src
... // all source files
package.json
.gitignore
Now I publish my-package on npm repository and then import it in my-project, with the command npm i my-package.
As a result of such import, the structure of the directory hosting my-project is something like
my-project
... // stuff
node_modules
my-package
dist
... // all the code of the package to be distributed
src
... // all source files
package.json
As expected, no node_modules folder is imported under my-package folder.
Since I am the developer of both my-package and my-project, during development I would like to import my-package from the local path with a command which could look like
npm -i ../my-package
If I proceed like this, the result I see is that all the files and folders stored under my-package directory are copied under the node_modules folder of my-project, i.e. .gitignore (or .npmignore) exclusions are not considered. The net result is a structure such as
my-project
... // stuff
node_modules
my-package
dist
... // all the code of the package to be distributed
node_modules
... // all stuff imported by my-package
src
... // all source files
package.json
... // any other file under my-package
Is there a way to install from local path only the relevant files of my-package as it happens if the package is installed from the npm repository?
We had the same problem today. We figure it out by adding to the package.json of the local package
“build”: “yarn run clean && yarn build-components && yarn pack”
And then to the project that is using the local package we added to the dependencies of the package.json
“yourpackage”: “file:../your-tgz.tgz”,
hope it helps
Note: if you are using yarn you might have problems with the cache.

npm 5 install folder without using symlink

Before publishing my node library, I could use the advice the npm documentation wrote about:
To test a local install, go into some other folder, and then do:
cd ../some-other-folder
npm install ../my-package
Prior to version 5 of npm, I had no problem as it produce what I expected, ie a folder with the output of what I will publish.
However, using npm 5, it now creates a symlink to my local project as described in the npm documentation:
npm install :
Install the package in the directory as a symlink in the current
project. Its dependencies will be installed before it's linked. If
sits inside the root of your project, its dependencies may be
hoisted to the toplevel node_modules as they would for other types of
dependencies.
How can I use the "old" way to install local project? Or is there a new way to check if my library is correct?
Thank you.
Use npm pack + npm install (as suggested by install-local package)
npm pack <path-to-local-package>
npm install <package-version.tgz>
This will effectively copy your local package to node_modules.
Note that this will package only production relevant files (those listed in the files section of your package.json). So, you can install it in a test app under the package own directory. Something like this:
my-package
package.json
test
test-app
package.json
node_modules
my-package
Assuming that test dir is not included in the files in my-package/package.json.
This works the same way with npm 5 and older versions.
I wrote npm-install-offline which allows you to install npm packages from a local repository or folder. By default it copies the folder on install but you can also choose to symlink.
https://www.npmjs.com/package/npm-install-offline
npx npm-install-offline ../some-package
Or
npx npm-install-offline my-npm-package --repo ./my-offline-npm
It also will install the package dependencies which npm does not do with local packages.