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?
You can easily run scripts using npm by adding them to the "scripts" field in package. json and run them with npm run <script-name> . Run npm run to see available scripts. Binaries of locally install packages are made available in the PATH , so you can run them by name instead of pointing to node_modules/.
Run “npm config get prefix” in your terminal. This will give the path of global node_modules: For ex: /usr/local. Change the user permissions for this folder by using following command: sudo chown -R <user_id> /usr/local/
npm install installs dependencies into the node_modules/ directory, for the node project you're working on. You can call install on another node. js project (module), to install it as a dependency for your project. npm run build does nothing unless you specify what "build" does in your package.
npm build [< package-folder >] <package-folder>: A folder containing a package.json file in its root. Description. This is the plumbing command called by npm link and npm install. It should generally be called during installation, but if you need to run it directly, run: npm run-script build. See Also.
These scripts happen in addtion to the "pre" and "post" script. NOTE: If a package being installed through git contains a prepare script, its dependencies and devDependencies will be installed, and the prepare script will be run, before the package is packaged and installed. Runs BEFORE the package is prepared and packed, ONLY on npm publish.
The neat thing about npm’s script commands is that we can automate tasks by prepending ‘pre’ or ‘post’ to them. So let’s make sure our tests always run before we build: Ok, so that’s a bit more fun. Of course, we need to do more with our code than test it.
While NPM is usually used for package installation with dependency management, we can also use it for building. This might seem kind of odd, using a package management tool for building our JavaScript applications, but I think it has some great advantages:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With