Run script that is in package.json of a dependency - javascript

I have this app structure:
root
whatever
node_modules
module_A
package.json
The package.json of module_A has some scripts. Let's say:
"scripts": {
"first_script": whatever
}
How to run first_script from the terminal?

One solution is:
npm explore module_A -- npm run first_script

Related

Difference between npm run dev and parcel index.html

I can use parcel index.html to create a local development server, bundling and hot module replacement. But it have come to my attention that using npm run dev does kind of the same think, so my question is:
what is the difference between the two? and how npm run dev is making the bundling process?
NPM vs Parcel isn't a valid comparison. They are two separate things. You can use Parcel with both NPM and Yarn.
Parcel is a web application bundler that is comparable to Webpack
NPM is a package management system for node.
npm run * is a command that will execute any npm script specified within your package.json and has no exclusivity to Parcel. You can of course make an npm script that will execute Parcel commands.
If you go into your package.json file, you will see a scripts property. Within this object, you can define arbitrary scripts to run. There are reserved script names such as start, install, build among others, but for the most part, this is a "free-for-all" that enabled the developer to specify any arbitrary scripts to run. A few common scripts that you'll typically see scripts to bundle your project or run a linter.
Example of package.json
Webpack Example:
{
"scripts": {
"build": "webpack --config <your entry file>"
}
}
Parcel Example:
{
"scripts": {
"build": "parcel build <your entry file>"
}
}

ESLint is reading other configurations

I have a simple .eslintrc:
{
"extends": [
"twilio"
]
}
But when I run my eslint, I get
The config "standard" was referenced from the config file in
"/Users/MyAccount/Projects/my-sample-app/node_modules/cipher-base/.eslintrc".
I even tried to make the eslint to ignore and used the following:
➜ ./node_modules/.bin/eslint --config .eslintrc --ignore-pattern node_modules/ ./jest.setup.js
but it's still giving me the same error. Why is eslint trying to read this other config file?
You can run eslint with --debug flag to see why a config is being loaded. You can also run with --print-config flag to see how your config looks like once all of the extends and cascading completes. ESLint by default will cascade configs (as in, it will walk directory structure from the current folder to the top to find all configs) and combines them all for linting. Most likely, one of the parent folders have eslint config that extends standard. --debug flag should tell you where it's coming from.
Try running it from an npm script, like this
"scripts": {"eslint": "eslint --config .eslintrc --ignore-pattern node_modules/ ./jest.setup.js"} and then running npm run eslint
What I think is happening is, when running it from the terminal like ./node_modules/.bin/eslint --config .eslintrc --ignore-pattern node_modules/ ./jest.setup.js, eslint looks for the first .eslintrc file it can find relative to it's own location node_modules/.bin in this example. When you run it in an npm script, it should look for the first .eslintrc file relative to the package.json file.

Npm module not found

I'm running an Angular app built with Grunt and using Bower and NPM.
I tried installing my npm module locally. The files are in the main app directory in node_modules folder.
The module docs ask me to load the module with <script type="text/javascript" src="node_modules/moment/moment.js"></script>, but I get 404.
Am I missing something? Do I have to tell Grunt that I want these NPM modules?
Can you provide more information on what your app is built with? If node serves your app, you need to make the directory you link to public. Assuming you're using express, this would look something like this in your app.js file:
app.use('/node_modules', express.static(__dirname + '/node_modules/moment/moment.js'));
Edit:
Or if you just want to make it work, try to load moment.js from CDN like this:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
Link to moment on CDN
Basically, npm is the package manager for all the javaScript frameworks such as Nodejs, angularjs etc. npm should be installed globally in the machine.You can install it from https://nodejs.org/en/ .
Next,you need check for the package.json file in your project.
If there is a package.json already existing in your project folder, then from command line you need to go to your project folder and type npm start.
If package.json file does not exist, then in the command line type npm init,then there will be a package.jsonfile created in your project folder.Then edit the package.json . and add the node packages into the package.json as similar way to this
{
"name": "shoppingkart",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www" //If you have any scripts.
},
"dependencies": {
"mongoose": "^4.9.0", // here you should have all your node_modules listed
"passport": "^0.3.2",
"stripe": "^4.15.1"
}
}
if you are not able to add the dependencies to json file, there is also another way to do it.
just go to your project directory in the command line and type
npm install --save grunt // And you need to do for all the node_modules, by replacing the **grunt**.
Automatically the dependency will be added to your package.json file.
If you installed your npm packages locally then your node_modules folder should found at the root of your project.
If you installed all your packages globally you may not see an npm_modules folder in your project.
To see where your global modules are located you can run
npm list -g
I faced the same issue just install the package globally and save at the end.
Like:
npm install -g <package> --save
Even the above doesn't work then use -f / --force at the end to force install.

Why store dist in github repo and npm?

I saw many repo, that contains dist folder. Why? I think repo should store only source code, without any builds and so on.
Let's look at follow example with ES6 code.
package.json
{
"files": [
"dist",
"lib"
],
"scripts": {
"build:lib": "<transform ES6 to ES5 and put it to ./lib folder>",
"build:umd": "<make a umd module and put it ./dist folder>",
"build": "npm run build:lib && npm run build:umd",
"postbuild": "<make minify code>"
"prepublish": "npm run build"
}
}
I think this is a good way. And I saw several repo that do the same. In this situation github repo would store only src and npm repo only lib and dist.
And now another question. Why store dist and lib in npm? Most libraries can be installed by executing npm install command. And how we know prepublish script runs on local npm install (npm-scripts).
So after package have installed we have lib and dist folders.
I don't understand why store this code in npm if only source code is enough?
It appears that repositories include dist directories in order to enable other projects to use them as direct dependencies via git. Compare package.json docs, though as of now this doesn't mention that artefacts need to be present.
Compare NPM: Missing dist and src directories when trying to install directly from a github url for the problem that arises if the dist directory is not checked in.
Alternatively, it is also possible to install from a Git repository directly as long as a prepare stage which builds the package is provided:
{
...
"scripts": {
...
"prepare": "npm run build"
},
...
}
This way, you won't have to store /dist folder in the repository itself

run bower install for target bower.json file

Assume that my working directory is c:\foo\ during the script execution. I would like run bower from there for c:\foo\bar\bower.json file. This is available on npm by running npm install --prefix c:\foo\bar.
Is there any equivalent command in bower?
Add a .bowerrc file to c:\foo\ with the following contents:
{
"cwd": "bar
}
this will set the working directory for Bower to c:\foo\bar\.

Categories

Resources