npm install - how to run build scripts with sufficient permissions? - node.js

I've created a node-module that has a build script that gets called after the installation.
The build script clones a git repository and copies some files of it to another folder.
The problem: on npm install, the script does not get sufficient permissions and I get the following error:
sh: ./build.js: Permission denied
How can I give the build script sufficient permissions to do its job?
I want that the users just can do npm install mymodule and the build-script then does its job on any system.
Any ideas?

Do you have the x flag on build.js?
chmod +x build.js
And the first line of your script should tell how to execute the script from the shell:
#!/usr/bin/env node

Related

Why can't install npm to create package.json on AWS EC2?

I'm trying use EC2 to execute command npm install will get bellow error.
(Node.js had already install)
Dose anybody know why?
Is that the reason that I execute this command chmod 400 before login EC2 by ssh?
chmod 400 seems only could read, isn't it?
npm install only works if there is an existing package.json file. Your ls command shows that the directory is empty.
If you want to create your initial package.json file, you want to use the command npm init and answer the questions.

npm - run scripts in directory other than cwd

I have a script which scaffolds an app. This involves making a new directory in the current directory inside which the app's scaffold files will be unpacked.
At the end of my script I run a series of npm/node commands as follows (appDir is a reference to the child dir created to host the app; it exists by the time these commands run):
const postInstCmds = [
'cd '+appDir,
'npm init -y',
'npm install add-npm-scripts --save-dev',
'npm install http-server --save-dev',
'add-npm-scripts server "http-server"',
'npm run server'
];
const execSync = require('child_process').execSync;
postInstCmds.forEach(cmd => execSync(cmd));
However it seems the first cd command is being ignored; the subsequent commands are being executed in the parent, not child, directory.
I've read this, which explicitly says that npm supports cd * operations, but perhaps I'm misunderstanding something (I'm no npm wizard). I've also tried using npm's --prefix argument to specify path, but no joy.
[UPDATE]
The linked dup suggests npm start <dir> but this seems to work only if <dir> already contains a package.json. As per my sequence of commands, I want to move to the directory and then create the package.json (via npm init). Another answer there suggests npm run --prefix, but run again requires an extant pacakge.json, since it reads from the scripts object.
I have also tried npm init --prefix <dir> but the arg doesn't seem to be supported for npm init.
Your code is running different child processes for each command. So the cd command runs in a subprocess and exits. Then the next command runs in a new subprocess that is in the original directory you started in and not in the directory that the cd command navigated to.
One fix would be to get rid fo the cd command and instead pass the directory as a cwd option for each execSync() call.
postInstCmds.forEach(cmd => execSync(cmd, {cwd: appDir}));

katacoda command not found

I'm trying to install the katacoda client. I used NPM to install it:
npm install -g katacoda-cli
The installation was successful but when I'm trying to run the katacoda help I've got the following error: command not found: katacoda. NPM installed katacoda here: /usr/local/Cellar/node/10.7.0/lib/node_modules/katacoda-cli. If I run the following: /usr/local/Cellar/node/10.7.0/lib/node_modules/katacoda-cli/bin/run help katacoda is executed correctly. I guess my problem is coming from my $PATH, which is set like this: export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
You can go to one of the directories, say /usr/local/bin, and then run ln -s /usr/local/Cellar/node/10.7.0/lib/node_modules/katacoda-cli/bin/run katacoda. Then you should be able to run katacoda directly. If you don't have permissions to do so in /usr/local/bin, choose another directory where you have permissions, do the same and add the directory to your $PATH

How run npm on deploy project via git? (hooks/post-receive: npm: command not found)

Problem
I created a project where configure a server in DigitalOcean with Apache and Git.
For init project on server, I run the following command:
cd /var/repo
mkdir project-example.git && cd project-example.git
git init --bare
I set up file post-receive with this code:
#!/bin/bash
git --work-tree=/var/temp/project-example --git-dir=/var/repo/project-example.git checkout -f
cd /var/temp/project-example
npm install
npm run build
rm -rf /var/www/project-example/*
mv -f /var/temp/project-example/build/* /var/www/project-example/
When I make a push to server remote via git on machine local, occurs followings errors:
remote: hooks/post-receive: line 4: npm: command not found
remote: hooks/post-receive: line 5: npm: command not found
However, accessing server via SSH and execute command:
# it works standard
cd /var/repo/project-example.git
source hooks/post-receive
Comments
System Server: Ubuntu 14.04
I installed node via nvm.
When a git hook runs, it does not necessarily have the same PATH variable set as when you log in via SSH. Try putting the full path to npm in your script when you call it; that should fix things.
UPDATE (June 7 2019):
A couple of commenters had issues with node not being found when using the above solution, which made me realize it is not very robust. Rather than put the path in the call to npm itself, you'd be better off adding the necessary path to the script's environment, e.g. if npm (and node) happen to be in /usr/bin/, add the following to your script before calling npm:
export PATH=$PATH:/usr/bin
In fact, a more robust way to make sure the hook works the same as it does when you are logged in via SSH is to find out what the path is when you are logged in (i.e. the output of echo $PATH) and set the path in your hook script accordingly (of course you may need to omit some user-specific paths, etc.)

NodeJS and npm without sudo on CentOS

On my box I have the Node and NPM binaries installed under /opt/node/bin. And the path is added properly.
node -v
and
npm -v
work fine. When I create a file and run it with node all works as expected. However, when I run:
npm init
the program fails to write package.json because it does not have write permission. I use:
sudo npm init
I get a file with owner and group of 0 0 and so any regular users cannot modify this file. I don't want to have to chown every file node/npm generates.
Is there way to get node/npm to run as a user in the same group as my other users and have write permissions to the same directories?
You could simply chown your /opt/ directory and future calls to npm init will be owned by you. Better yet, work in your /home folder and make sure npm and node are in your $PATH. This way you don't need to worry about permissions for initializing a new node module. You will, however, need to use sudo to install packages globally. This is bad practice according to the maintainer of Nodejs:
http://howtonode.org/introduction-to-npm
I would follow along with his setup there. As he mentions, its very dangerous to give root access to a package manager.

Resources