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.
Related
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.
I want to install a local project by using "file:../project-name" in the package.json. However, when I run this command it only installs the dependencies of the local project (node_modules) and not the source files itself.
I have project-name/src for example and I expect it to appear in node_modules/project-name/src, but it doesn't.
I checked the .gitignore (I don't have a .npmignore file) and src as a folder is not excluded.
What am I not seeing here?
If you refer to the files property, this does not indicate files to copy to the node_modules on npm install, but it indicates the files to publish on npm publish.
Edit
When including a dependency with file:... in a project, the directory has to:
have a package.json
Declare exported files in the "files" property.
I am writing a library and need to know what will happen if a user tries to run npm install -S X in their project before a package.json file exists.
I just tried this on Windows, and sure enough, NPM did not barf and went along with it's business but when the install command completed, there was still no node_modules folder nor a package.json file.
Does anyone know what is expected to happen? I assume I should require the users of my library to run "npm init" before running "npm install X" ?
Looks NPM does throw an error at the end of the install -
ENOENT: no such file or directory, open 'package.json'
but I wonder if that prevents the install process from create a node_modules dir, and actually putting the dependency in there.
npm install without the -g flag will walk up the folder tree checking for folders that contain either a package.json or a node_modules folder.
If either of those conditions are met, then that folder will be treated as the current directory for the purpose of the npm commands you are running. If no such folder is found, then the current folder is used.
As you noted, a node_modules folder will be created and after the package is loaded into the cache it will be unpacked into that 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.
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.