NPM Install, move files with a build script - node.js

I have built a custom npm package that is essentially a list of dependencies, a gulp file, and a configuration file for it. The idea is that I can run npm install <path-to-git-repo> to pull in gulp whenever I start a new development project.
However, I would like the gulp file and config file to be moved into the current directory, and not remain inside of node_modules. I have tried to accomplish this with the install script in my package.json file listed below, but it didn't seem to do anything. The files I'm trying to move are included in the files section of package.json.
"scripts":
{
"install": "mv gulpfile.js ./ && mv gulp.config.json ./"
},
Thanks for your help

For something like this I think it would be much easier to have 1 source gulp file and specify the dependent gulp file(s) wherever you're installing them. example blog
Also, just to point out, install is typically considered an anti-pattern
But to answer your exact question, this command mv gulpfile.js ./ && mv gulp.config.json ./ looks to me like you're saying move gulpfile.js (which is already in the root directory), into ./ (which is the same directory). Check your source directory actually contains the gulpfile.js by running the command yourself on the command line

Related

npm link only dist folder

Is there a way to restrict npm link to create the symlink only to point at ./dist?
I am struggling so far as I want to test my library (react component) locally before publishing.
In my package.json I have included the main (entry point):
"main": "./dist/index.js"
But still when I run npm link it links to the parent folder which has node_modules and so on.
This is causing problems cause now I have 2 react apps installed.
I thought of a way to exclude node_modules but I couldn't even find a way to do that.
This is actually simpler than expected. First run ng build to build your library project, then go under the dist folder and run npm link from there instead of the library root project. The symlink will be created pointing to the dist folder instead.
Using "main" parameter doesn't make sense because npm link use 'npm prefix' for create symlink
see here
You can use next hack:
Cd to project directory
Copy package.json from package to ./dist/package.json directory
Cd to dist directory
Run 'npm link'
It looks dirty but seem to only way to do it with npm link

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.

Track yarn's node_modules directory properly in git

We've got a weird dependency in our CI that requires our git repo to have a node_modules directory (but obviously not the packages in it).
Since git doesn't track empty directories, the way we achieve it is to have a .gitignore inside:
node_modules/
.gitignore
With the following configuration
*
!.gitignore
This works, until we re-install our modules using yarn install, which will clear everything in the directory, including our .gitignore file, messing up the repository.
We can also add something like:
node_modules/
.empty
.gitignore
With .gitignore:
/node_modules/*
!/node_modules/.empty
But yarn install will still remove the file, and any tracking of the node_modules directory.
Our question, is there anything that we're missing that can:
Allow us to track node_modules/ in git
Won't break whenever we yarn install
I'm aware the actual answer is to fix our CI, but that's a bit out of scope for us right now.
Create a post install script that recreates the .empty file in node_modules.
More specifically, you can have certain scripts added in your package.json that run after install is called. In this case:
{
// ...
"scripts": {
// ...
"postinstall": "touch node_modules/.empty"
}
// ...
}
Should make sure there is a file in node_modules named .empty whenever install has completed.
Maybe you can prevent deletion of the "placeholder" file if you remove the write permission from the file for the user running yarn install. But maybeyarn install fails if it cannot clear the directory.

Running "npm install --save X" in folder without package.json file

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.

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.

Resources