I have a package that itself has a script in its package.json that I would like to be able to run in my top-level project. This package is one of my top-level projects dependencies. I'm looking for a way to directly or indirectly call the dependency packages script.
Let us assume the module name I'm working with is named foo and the script I want to run is updateFooData.
I tried using the syntax npm run-script <package> <...> to run it, but this appears to be deprecated functionality as I can't find it in the current official documentation but I see it in other (very old) search results.
npm run-script foo updateFooData
# npm ERR! missing script: foo
I also looked into the npm api and while npm.commands.run-script(args, callback) will do what I want, I can't figure out how to load the module into npm
{
...
"scripts":{
"foo:updateFooData": "node --eval \"... ??; npm.commands.run-script('updateFooData', callback)\""
}
}
npm run foo:updateFooData
# Obviously fails
The only thing I've found that works so far is to CD into the submodule directory and run npm from there. This is not the preferred solution for me.
cd node_modules/foo
npm run updateFooData
I ran into this trying to run the updatedb script for geoip-lite. You should use the npm explore command which will spawn a new shell in a dependencies' directory.
So for your use case, try npm explore foo -- npm run updateFooData
Note:
This isn't a very good idea. You have no guarantee which node_modules folder module will be installed in as NPM will attempt to optimise space by installing shared packages at the highest level possible. – #superluminary
Something I've found that does work:
If the script you are running runs a script file, you can look at the path of the file it's running and run the script using a require:
# if node_modules/foo/package.json looks like this
{
"scripts": {
"updateFooData":"scripts/updateFooData.js"
}
}
# then package.json can look like this
{
"scripts": {
"foo:updateFooData":"node --eval \"require('foo/scripts/updateFooData.js')\""
}
}
# or package.json can look like this
{
"scripts": {
"foo:updateFooData":"node node_modules/foo/scripts/updateFooData.js"
}
}
# and you can run it like this
npm run foo:updateFooData
I don't like this solution because it only works if the npm script you are running is a file. It won't apply in all
Related
I'm trying to understand and follows https://github.com/DMPRoadmap/roadmap/wiki/Installation
But I don't understand something they use.
What does these do?
1) npm run bundle
I know it equals to npm run-script bundle as according to npm doc about run script but I don't really understand where the bundle come from; in other words, I don't understand what npm doc about run script mean by
an arbitrary command from a package's script object
2) npm run bundle -- -p
Since I don't know where the bundle come from, I don't know how to work out the meaning of -- -p option. I want to find its documentation and see the details.
I'm not sure if npm doc about bundle is related, but it seems to be replaced by install as documented in npm doc about install.
And why is this option got so many - characters (3 in this case) before p? I normally see 2 - for long option name and 1 - for abbreviated option name
Any time you see npm run [x] anywhere it means that it's executing a command located in the scripts section of the package.json file. Therefore npm run bundle runs the bundle command located here: https://github.com/DMPRoadmap/roadmap/blob/master/lib/assets/package.json#L8 which in this case looks like all it's doing is running webpack
"scripts": {
"test": "./node_modules/.bin/karma start",
"bundle": "./node_modules/.bin/webpack",
"lint": "./node_modules/.bin/eslint --ext .js --cache ./javascripts/ || true"
}
I installed sequelize-cli:
npm install sequelize-cli --save
I have only installed it locally, not globally. My local project is at C:/git/craft
I try to execute it by typing sequelize-cli, but this doesn't work; I forget I must type node_modules/.bin/sequelize-cli
I want to use npm run sequelize-cli, expecting this will be a shortcut. The npm-run-script documentation says:
In addition to the shell's pre-existing PATH, npm run adds
node_modules/.bin to the PATH provided to scripts. Any binaries
provided by locally-installed dependencies can be used without the
node_modules/.bin prefix
And yet, when I try to run the script:
"scripts": {
"sequelize-cli":"sequelize-cli"
},
The result fails:
NND#usrem-nnd MINGW64 /c/git/craft (master)
$ npm run sequelize-cli -- --help
> craft#0.0.0 sequelize-cli C:\git\craft
> sequelize-cli "--help"
'sequelize-cli' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! Windows_NT 10.0.16299
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "sequelize-cli" "--" "--help"
I couldn't find similar questions/issues online (specifically re: node_modules/.bin/____)
Some Github issues mention that yarn should work, (but also say that npm should work as I expect!.) As a workaround I will just install sequelize-cli globally. I can install sequelize-cli globally, but then running sequelize-cli still produces "command not found". This led me to see my silly mistake...
I had the same question as you. You may have the same problem
Finally, I found it's caused by my project folder has invalid letter.
Change your project folder name and try again.
Ensure you are referencing a script that exists! I wasn't! In fact, npm run was just revealing the obvious problem: the script sequelize-cli doesnt exist anywhere on the computer!
... The script is called sequelize, even though it comes from a project called sequelize-cli. package.json "scripts" value will determine which scripts can be run; the correct package.json should map the script "sequelize" to the nodejs command sequelize:
"scripts": {
"start": "node ./bin/www",
"sequelize":"sequelize"
},
npm run sequelize now works...
My mistake... this answer can be deleted if it's unhelpful.
I am new to ReactJS, I was following one of the tutorials in "TutorialsPoint".
In Step 2: After the folder is created we need to open it and create empty package.json file inside by running npm init from the command prompt and follow the instructions.
C:\Users\username\Desktop\reactApp>npm init
By running the above command I am unable to create and package.json
Only this thing is coming up after running init.
In Step 4: it asks to add dependencies and plugins
In Step 5: it asks to open the package.json and delete "test": "echo \"Error: no test specified\" && exit 1" inside "scripts" object.
Now how can I open package.json, if I was not been created from the beginning?
Complete the npm init prompts
The command npm init "will ask you a bunch of questions, and then write a package.json for you."
The first prompt, as shown in your screen capture, asks you to enter a package name. You can just hit Enter to skip through the prompts and accept the default values if you are unsure of what information to provide.
You can also use -y/--yes to skip the questionnaire altogether. For example, npm init -y. (Note: -f/--force also work.)
Each prompt corresponds to a field in the package.json file. Consequently, you can construct this file yourself or copy and modify an existing one from another project.
A package.json must have:
A name all lowercase one word, no spaces, dashes and underscores allowed;
A version in the form of x.x.x;
There are two ways to create a empty package.json file:
Run a CLI questionnaire to create a package.json with values that you supply. For that run the command:
npm init
Create a default package.json. For that you just need to run the same command but with the --yes or -y flag.
npm init --yes
and it will create the package.json file in the dir where you run the command and will display the code shown bellow:
{
"name": "reactApp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Generally, it is not required for installing packages. If you don't use npm init for the folders, all the package can work without it.
Also for solving this problem, you should use
npm init --yes
when used with --yes then it is available the .json file in the directory. Also, the information on the .json file is dependent on the package that you had installed.
I am new on npm. I got confused just like you when i was trying to follow the npm tutorial.
You just need to press the 'Enter' key on your keyboard after your step2.
When you press 'Enter', you give default value to it. So it's gonna generate the json file based on its default information... hope it helps.
I had same problem in my project directory. when i run npm init it gives Error that package.json not found, complete log found in something like this " c:/user/node_modules/...".
This is because it overwrites the existing package.json file or the file path is not correct.
Also Admin permission is not granted to create such file. so try VS Code or IDE you all use to run as Administrator.
So, i put my project in the other Drive **(E: drive) and then run npm init** it works fine and create package.json file.
I fall onto this problem, when I typed npm init and press enter:
D:\React\booking>npm init
npm ERR! code MODULE_NOT_FOUND
npm ERR! Cannot find module './util.js'
npm ERR! Require stack:
npm ERR! - C:\Users\Sabbir\AppData\Roaming\npm\node_modules\npm\node_modules\libnpx\index.js
npm ERR! - C:\Users\Sabbir\AppData\Roaming\npm\node_modules\npm\lib\init.js
npm ERR! - C:\Users\Sabbir\AppData\Roaming\npm\node_modules\npm\lib\npm.js
npm ERR! - C:\Users\Sabbir\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Sabbir\AppData\Roaming\npm-cache\_logs\2020-07-13T05_58_52_734Z-debug.log
Here is the answer that helps a lot about this problem
Follow these steps, closely:
http://nodejs.org/download/ download the 64 bits version, 32 is for hipsters
Install it anywhere you want, by default: C:\Program Files\nodejs
Control Panel -> System -> Advanced system settings -> Environment Variables
Select PATH and choose to edit it.
If the PATH variable is empty, change it to this: C:\Users{YOUR USERNAME HERE}\AppData\Roaming\npm;C:\Program Files\nodejs
If the PATH variable already contains C:\Users{YOUR USERNAME HERE}\AppData\Roaming\npm, append the following right after: ;C:\Program Files\nodejs
If the PATH variable contains information, but nothing regarding npm, append this to the end of the PATH: ;C:\Users{YOUR USERNAME HERE}\AppData\Roaming\npm;C:\Program Files\nodejs
Now that the PATH variable is set correctly, you will still encounter errors. Manually go into the AppData directory and you will find that there is no npm directory inside Roaming. Manually create this directory.
Re-start the command prompt and npm will now work.
Installing Node.js (and npm) on Windows 10
For example, to launch locally installed gulp, I have to run the following command from inside of my project:
node_modules/gulp/bin/gulp.js
To be able to launch npm packages only by their name, I want to include node_modules relatively to project's root dir. Is this possible?
P.S
I know how to install npm packages globally, but I'm trying to avoid doing that.
I hope I understand you correctly: You are trying to execute programs like gulp from your local install.
You can set up a npm script like so in your package.json:
package.json
...
"scripts": {
"build": "./node_modules/.bin/gulp"
}
...
Then, you can run gulp via npm run build from your command line. (Or optionally you can type ./node_modules/.bin/gulp)
Hi I'm trying to install Grunt on Windows 7 64 bit. I have installed Grunt using commands
npm install -g grunt
npm install -g grunt-cli
but now if I try to do grunt init, it is throwing me an error -
A valid Gruntfile could not be found. Please see the getting started
guide for more information on how to configure grunt:
http://gruntjs.com/getting-started Fatal error: Unable to find
Gruntfile.
But when I look inside the grunt folder on my system the Gruntfile.js is there. can someone please guide me how to install this grunt properly and how to write built Script using the grunt. I have one HTML page and java script if i wants built a script using Grunt how can i do it?
To setup GruntJS build here is the steps:
Make sure you have setup your package.json or setup new one:
npm init
Install Grunt CLI as global:
npm install -g grunt-cli
Install Grunt in your local project:
npm install grunt --save-dev
Install any Grunt Module you may need in your build process. Just for sake of this sample I will add Concat module for combining files together:
npm install grunt-contrib-concat --save-dev
Now you need to setup your Gruntfile.js which will describe your build process. For this sample I just combine two JS files file1.js and file2.js in the js folder and generate app.js:
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
concat: {
"options": { "separator": ";" },
"build": {
"src": ["js/file1.js", "js/file2.js"],
"dest": "js/app.js"
}
}
});
// Load required modules
grunt.loadNpmTasks('grunt-contrib-concat');
// Task definitions
grunt.registerTask('default', ['concat']);
};
Now you'll be ready to run your build process by following command:
grunt
I hope this give you an idea how to work with GruntJS build.
NOTE:
You can use grunt-init for creating Gruntfile.js if you want wizard-based creation instead of raw coding for step 5.
To do so, please follow these steps:
npm install -g grunt-init
git clone https://github.com/gruntjs/grunt-init-gruntfile.git ~/.grunt-init/gruntfile
grunt-init gruntfile
For Windows users: If you are using cmd.exe you need to change ~/.grunt-init/gruntfile to %USERPROFILE%\.grunt-init\. PowerShell will recognize the ~ correctly.
Some time we need to set PATH variable for WINDOWS
%USERPROFILE%\AppData\Roaming\npm
After that test with where grunt
Note: Do not forget to close the command prompt window and reopen it.
I got the same issue, but i solved it with changing my Grunt.js to Gruntfile.js
Check your file name before typing grunt.cmd on windows cmd (if you're using windows).
You should be installing grunt-cli to the devDependencies of the project and then running it via a script in your package.json. This way other developers that work on the project will all be using the same version of grunt and don't also have to install globally as part of the setup.
Install grunt-cli with npm i -D grunt-cli instead of installing it globally with -g.
//package.json
...
"scripts": {
"build": "grunt"
}
Then use npm run build to fire off grunt.