How does NPM resolve & run scripts from package-json? - node.js

For curiosity's sake, I want to create a folder in node_modules and be able to run it as a script in package.json's "scripts", but I have not been able to find out how to.
I've noticed with other package.json scripts like "react-scripts" that they wont work in the command line, but will when NPM runs them. How does npm resolve scripts?
Does it have to be a legit package for NPM to run it? (Like, will it check against the NPM registry for if that package exists, or contains scripts?)

If a module has a ["bin" entry in its package.json]1, then npm will put the executable script specified by that "bin" entry into a .bin folder in node_modules.
When running a script with npm run, npm will put that node_modules/.bin directory first in the PATH so they are found with npm run but not when run directly from the shell.

Related

npm start error nodejs

I give the command npm start. Terminal responds:
Make sure you are in correct directory when you run npm install, your current directory should have package.json file. for example based on the project you uploaded, you should be inside diplom folder and then run npm install.

How do I get spectacle-code-slide working on Windows?

I have installed Node on a Windows 10 machine. Then did npm install spectacle from the node command prompt. Finally downloaded the spectacle-code-slide zip file. Moved into the folder, did npm install and npm start. I get the following:
npm ERR: missing script: start.
Obviously, I am missing something. What is it? Note: I am a complete noob!
Edit: I tried the process outlined here without success.
This error means that you don't have start script in your package.json. Read about scripts here.
When you run npm instal spectacle you install spectacle as a package into node_modules folder. So you don't have any scripts in your package.json except those you add there yourself.
I've actually never used spectacle, but I suppose you better use their spectacle-boilerplate (instead of doing npm instal spectacle). You need to clone (via git) or download that repository locally. And then you will have their package.json with start script in it.

Run npm command line without add node to path

I have a folder with last version of node and npm (on ubuntu) :
node
node_modules
node
npm
npm.cmd
And would like to run node/npm without add node/node in classpath
Because node/npm returns :
node/npm: node: not found
Idea ?
What you can do is to call the npm-cli.js using node. So in the node folder, you can run npm commands by running:
./node node_modules/npm/bin/npm-cli.js <command> <args>
where and are the npm command and arguments.
This is impossible.
Any executable that you run by writing its name (not including the path) is looked in some pre-defined paths.
If you'd like to run an executable located in some directory, you can execute it only by specifying its full or relative paths.
If you change dir to the containing directory, you can run it by
./executable-name
In your case
./node

Why executable is found

I do npm install grunt -g and the folder is created C:\Users\myname\AppData\Roaming\npm\node_modules\grunt-cli. In my path variable only the C:\Users\myname\AppData\Roaming\npm part is added. How then when I write grunt in the command line the grunt executable inside \grunt-cli folder is found?
That's because when you install grunt-cli, npm creates a file grunt (and grunt.cmd) inside C:\Users\myname\AppData\Roaming\npm so directly in your the folder in your path
If you look into this file you can see it calls node_modules\grunt-cli\bin\grunt

How can I run a script from an already installed NPM package?

Example:
npm install my-tools
npm run my-tools tool1
This doesn't work - it looks for a "package.json in my current directory. The documentation for "npm run" says:
If no package name is provided, it will search for a package.json in
the current folder
(https://docs.npmjs.com/cli/run-script)
So how can I provide a package name?
It looks like you're trying to run a script defined in the package.json of my-tools. This functionality is not supported as of npm#2.0.0.
The reference to npm run-script taking a package name is a documentation error; I have created an issue for that on the npm issue tracker: https://github.com/npm/npm/issues/7440
Some alternatives might include:
using npm explore to temporarily set current directory (etc.) into the package, e.g., npm explore my-tools -- npm run tool1
publishing tool1 etc. as bin entries in package.json instead of as scripts; this will make them accessible via the command line
depending on your needs, you may find a more heavyweight task runner such as grunt is a better approach than npm scripts and binaries

Resources