npm run cannot run an script found in node_modules/.bin/___ - npm

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.

Related

What is the correct way of installing firebase-tools (a npm package) in NixOS/Nix?

I am watching this Udemy course on Firebase. The course is a bit dated, initially made maybe 6 years ago.
At some point the author indicates how to use firebase CLI, hence he indicates the command:
~/projects/chatlive]$ npm install -g firebase-tools
I added sudo to avoid permission problems:
~/projects/chatlive]$ sudo npm install -g firebase-tools
Unfortunately, this error message is retrieved:
npm WARN checkPermissions Missing write access to /nix/store/nm7vm9d7xbvibazz7kl7xkqgjddqgiby-nodejs-14.18.3/lib/node_modules
npm ERR! code EROFS
npm ERR! syscall access
npm ERR! path /nix/store/nm7vm9d7xbvibazz7kl7xkqgjddqgiby-nodejs-14.18.3/lib/node_modules
npm ERR! errno -30
npm ERR! rofs EROFS: read-only file system, access '/nix/store/nm7vm9d7xbvibazz7kl7xkqgjddqgiby-nodejs-14.18.3/lib/node_modules'
npm ERR! rofs Often virtualized file systems, or other file systems
npm ERR! rofs that don't support symlinks, give this error.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2022-07-28T15_33_55_544Z-debug.log
I am using NixOS and this is my config file. Since the error message mentions the nix/store I thought it could be related.
Usually, I install things in NixOS changing my configuration.nix and doing sudo nixos-rebuild switch. But I thought it would not be necessary to do it considering npm was managing the installation.
Searching on Nix packages, I can see firebase-tools: link. Now, it seems that tweaking the config file is the way...
In situations like this, what is the best practice?
Should I force trying to install via npm (how?)? Should I go for a new generation of NixOS and a full system rebuild just because of this package?
Is there another possible path? Am I missing something?
Thanks.
If you just want to play around with firebase-tools in a development environment, create a shell.nix file in your working directory and add the following lines:
{ pkgs ? import <nixpkgs> {}}:
pkgs.mkShell {
nativeBuildInputs = [ pkgs.nodePackages.firebase-tools ];
}
Then run nix-shell to start a shell with firebase-tools included. This is considered best practice for local development environments, see https://nixos.wiki/wiki/Development_environment_with_nix-shell .
If you want to try out packages not in nixpkgs repo you should use npm and install a package locally (without -g ). See https://nixos.wiki/wiki/Node.js#Using_npm_install_-g_fails.

On Mac M1, can't run scripts in package.json: - sh: <dependency>: command not found

I just got a Mac Mini M1 for personal use, and I'm trying to run a preexisting React app. I installed nodejs and npm successfully, and running npm install does add the node_modules folder correctly as far as I can tell; but whenever I run npm start or npm run <script>, I get an error. It seems that npm can't access any of the project's dependencies. I've tried this using the rosetta terminal as well with the same results.
For an example, I initialized a new React project with npx create-react-app test_app, then cded into it and ran npm start. I got:
test_repo#0.1.0 start
> react-scripts start
sh: react-scripts: command not found
How do I get these commands to run properly and launch the app?
Here's what I'm using for node and npm:
➜ test_repo npm -v
7.6.0
➜ test_repo node -v
v15.11.0
I found a (very) hacky solution for now. I'm no expert with npm, but what I discovered is that npm scripts refer to dependencies indirectly - for example, having a command that says
"test": "jest"
tells npm to look in node_modules/.bin for a file called jest and to run that.
the issue is something to do with npm understanding this. But it's possible to get around that by putting the address of each dependency in the script, for example:
"test" "node_modules/.bin/jest"
I was able to get things to build this way. If someone comes along with a better answer, please show me up :P

how to solve:npm run build/dev: missing script?

I'm trying to run node, but for some reason the local npm install of node isn't working.
The package is there:
$ npm run dev npm ERR! Darwin 15.4.0
npm ERR! argv "/usr/local/Cellar/node/5.6.0/bin/node" "/usr/local/bin/npm" "run" "jshint"
npm ERR! node v5.6.0
npm ERR! npm v3.6.0
npm ERR! missing script: dev
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR! /Users/me/workspace/testapp/npm-debug.log
I can work with npm install, but run npm dev is not correct.
You saw that error because there was probably not a script named dev in the "scripts" section of your package.json
npm install and npm run dev are two completely different ideas
npm install will run through dependencies section of the package.json and fetch/install the modules in that list
npm run dev will check the scripts section of the package.json and try to find a script titled "dev" and if there is no script named "dev" it will error out as you experienced (Dev is absolutely not a special word by the way, if you ever need to use the scripts section in a future project you can name the scripts anything you want.)
As an example, make a new folder and copy the following into a file named package.json inside of it
{
"name": "testapp",
"version": "1.0.0",
"description": "",
"scripts": {
"dev": "echo This is the DEV script",
"abc": "echo This is the abc script",
"xyz": "echo This is the xyz script",
"start":"echo This is the special start script"
}
}
From your terminal, cd into the directory you made containing that sample package.json and try the following commands and see what happens:
npm run dev you should see on your screen "This is the dev script"
npm run abc you see on your scree "This is the abc script"
npm run xyz you should see on your screen "This is the xyz script"
npm run linkxu1989 you should see on your screen a similar error to what you saw above since there's no script named "linkxu1989" in the scripts part of the package.json
npm start you should see on your screen "This is the special start script" (Note that start IS a special name. You can run with just npm start or with npm run start like all the others`
Bottom line: Check the "scripts" section of package.json and to run any of them just enter npm run SCRIPT_NAME
Hope that helps and good luck w/ NPM!
See here for more details
https://docs.npmjs.com/getting-started/using-a-package.json
http://browsenpm.org/package.json (don't worry about understanding everything in it, all you should think about at this point is what's in "dependencies"
http://jsonlint.com/ (If you ever manually edit a package.json, run it through this checker to help catch any formatting mistakes. The package.json is a "json" file so it needs to be in a PERFECT format which means no trailing commas, double quotes only, etc etc)
http://www.w3schools.com/js/js_json_syntax.asp
It means in your “package.json” (in the folder in which you run “npm run build”), there’s NO “build” script. A quick check: run “npm run lalala” in your terminal/command prompt. It will show “missing script: lalala”.
So if it’s your package, add the “build” script. Just go to the package.json in your code editor and add the key value entry as JSON.
If it’s a module/package you downloaded from npmjs.org, then refer to the documentation to see what command they support.
Note: “npm build” is a completely different command from “npm run build”. All “scripts” commands except start and test need to be run with “run”.

npm init doesn't create package.json

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

How do I run an npm script of a dependent package

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