Gulp installs outside project root - node.js

I've encountered a weird issue when installing Gulp in a new project.
Let's say I have the following path:
c:/development/myproject
When I run npm install gulp in that directory, the node_modules folder actually gets created in
c:/development/node_modules
instead of
c:/development/myproject/node_modules
And all of gulp plugins also get installed in that directory outside my project root.
I also have an earlier project where gulp was already installed before, and when I tried to rerun gulp installation in that project directory it was installed correctly in the project root (for example: c:/development/myolderproject/node_modules), not outside.
I don't think it has anything to do with the case, but the new project is using Laravel 4, while the other one is on Laravel 5.
I don't recall having to set any specific configuration before, so I'm totally confused why it behaves differently.

When you did npm install it found package.json from parent directory and thought it was the package root.
Related docs: https://www.npmjs.org/doc/files/npm-folders.html#more-information
Starting at the $PWD, npm will walk up the folder tree checking for a folder that contains either a package.json file, or a node_modules folder. If such a thing is found, then that is treated as the effective "current directory" for the purpose of running npm commands. (This behavior is inspired by and similar to git's .git-folder seeking logic when running git commands in a working dir.)
If no package root is found, then the current folder is used.

I run Ubuntu 15 and I had a similar issue where gulp was installing the node_module folder somewhere I couldn't find. gulp would say ../../node_modules was the location but it was NOT in my project folder.
I figured out from the link above and some more research I just needed to run npm init to create a project.json in my project folder. gulp was installing the node_modules in another folder because it searches for a project.json file to install the folder node_modules into.
Hope this helps anyone else solve this silly problem.

Related

Node.js will not install packages in project folder

I'm not sure what happened, but I've wasted six hours trying to fix Node.js as it just stopped working out of nowhere. Running Windows 11 with Powershell & Git Bash.
When I try to run npm install in my local git repository with a Next.js application (which I already initialized and even deleting and initializing again), it does not work; rather, it installs my project as a package WTFF??
Whenever I run create-next-app, it also installs the dependencies to the global folder.
I've tried:
Reinstalling Node.js (via Winget and Scoop)
Removing node_modules folder globally
Deleting package-lock.json
Creating a new project (doesn't work on any repository)
Configure --location flag at default startup to project, but it still did not fix it.
Deleting old package.json and initalizing a new one
Creating a new project
See below for screenshots.
What it says every time I run npm install in any repository
Result of npm list
Modified Configuration file
Please let me know if anyone has any idea, whether right or wrong. I tried looking online for related posts but couldn't find any like mine; if I overlooked any, please link them below. Thanks to anyone that helps, I have been losing my mind over this.
EDIT: When I set the flag for npm in the npm shim files, it does not work. But the workaround is to add --location=project every time I run an npm or npx command. I know you can set an alias to make life easier. How do I fix the configuration files?
I edited the shims located in: C:\Program Files\nodejs\node_modules\npm\bin and C:\Program Files\nodejs
I found out how to fix this problem; it had to do with a .npmrc already existing in my User folder, which automatically added it to the npm config, or I'm assuming. All I had to do with remove the .npmrc file in my home directory and run npm config set location=project, and it configured itself to install to the project directory by default.
Problem
.npmrc file existed when it should not have been there, so even though I reinstalled Node, the .npmrc never got deleted, so it always defaulted to the old configuration file and never worked.
Solution
Run npm config ls to see your configuration file
For me, there was a line in the configuration that looked like; "global" config from C:\Users\Ashwi\.npmrc
I deleted the file, then updated the configuration by setting the default location to project npm config set location=project
Then I restarted Powershell, and everything worked perfectly. However, to install global packages, you must add the --location=global flag every time you run the command.

npm prepare script not building folder in node modules

I am trying to use npm's prepare script to run a build step when npm installing from a different project.
The script does run during the npm install however, it doesn't build out the dist folder inside node modules.
Refer to this article for more details http://jim-nielsen.com/blog/2018/installing-and-building-an-npm-package-from-github/
I also had the same problem. My prepare script wasn't creating the build directory in the node_modules folder when installing as dependency.
Finally I found out that my .gitignore was the problem, which was setup to ignore the build directory for version control. NPM is inheriting the .gitignore file when no .npmignore can be found, which was the case here.
As stated on https://docs.npmjs.com/misc/developers:
If there’s no .npmignore file, but there is a .gitignore file, then npm will ignore the stuff matched by the .gitignore file
So I solved the problem by simply adding an empty .npmignore in the root.
I hate to provide such a simple answer, but my solution was to use npm install rather than yarn, which apparently doesn't run the prepare script properly.

Why does npm install keep saving under node modules folder even though i changed directory to another folder?

In terminal, I entered the command cd /Users/MyUserName/Google/Google Drive/Coding then entered the command npm install underscore. So I figured the underscore module would be saved under my Coding folder; however, it keeps saving under the directory: /Users/MyUserName/node_modules/
How do I change the setting so that when ever I change my directory in terminal and enter a npm install command, the module gets installed in the respective changed directory?
From the documentation:
Starting at the $PWD, npm will walk up the folder tree checking for a folder that contains either a package.json file, or a node_modules folder. If such a thing is found, then that is treated as the effective "current directory" for the purpose of running npm commands. (This behavior is inspired by and similar to git's .git-folder seeking logic when running git commands in a working dir.)
If no package root is found, then the current folder is used.
So it seems the package is installed under your home folder because it already contains a node_modules folder. If you remove that the install should go into the current folder.
Alternatively: Add a node_modules folder to the current folder.

npm not creating node_modules folder in project directory

I am doing a small Sinatra project and I want to use Gulp.
I have node(v0.12.0), npm(2.13.1), gulp(3.9.0) installed. I am in my project directory but when I try install a package like "npm install gulp-sass --save-dev", it doesn't create any "node_modules" folder in my project directory. It seems to be installing the package in my user home directory. Anything I am doing wrong?
From the npm documentation:
Starting at the $PWD, npm will walk up the folder tree checking for a folder that contains either a package.json file, or a node_modules folder. If such a thing is found, then that is treated as the effective "current directory" for the purpose of running npm commands. (This behavior is inspired by and similar to git's .git-folder seeking logic when running git commands in a working dir.)
If no package root is found, then the current folder is used.

Migrating from Gruntfile.js to gulpfile.js

I am trying to use the tool grunt2gulp.js to migrate multiple projects that were set up with grunt into gulp. However, I am unsure how to proceed.
The tool grunt2gulp.js seems to be a simple way to start, but the problem I am having is that I cannot get it to work. As there is no npm install -g grunt2gulp, I am somewhat unsure what I need to do to install the project globally and be able to access it.
The readme.md on the github repo doesn't specify how to install the project.
Download the grunt2gulp.js file to the same directory as your Gruntfile.js file. Navigate to that path using terminal and paste this command:
node grunt2gulp.js Gruntfile.js > gulpfile.js
This will create a gulpfile.js file in the same directory.

Resources