NodeJS: add options to npmrc - node.js

I am currently running my script as
node --no-deprecation main.js
I was wondering if there is a way to add this option to ~/.npmrc or otherwise (environment variable), such that I do not need to add the option to the command line. I would like to just run my script as node main.js and to be shown no deprecation warnings.
To make this question more general, is there a general way in NodeJS of setting flag values in a config file rather than adding it to the command (say I am interested in running with --trace-warnings as well.
I am looking for something cleaner than say alias node="node --no-deprecation".
I already tried adding this line inside ~/.npmrc
no-deprecationss=true
Reference:
https://nodejs.org/dist/latest-v7.x/docs/api/cli.html#cli_no_deprecation

If you're looking for a global configuration, setting the --no-deprecation flag in the NODE_OPTIONS environment variable in your .bashrc or other shell config file is what you're looking for, as long as you're using node v8 or above.
https://nodejs.org/docs/latest-v10.x/api/cli.html#cli_node_options_options

You can do it via package.json. e.g
"scripts": {
"main": "node --no-deprecation main.js"
},
and run it via
npm run main
or
yarn main

I know this is an old question, but for anyone new who comes across it, one reproducible way to do this without having to add it to every npm script and without affecting other projects (which is what would happen if you added it to .bashrc or .profile), is to add a .npmrc file in your project with node-options set in it.
It accepts the same flags as NODE_OPTIONS except applied to every npm script in your project without affecting others and keeping your scripts DRY.
e.g.
# Sets the flags in NODE_OPTIONS when running `npm run myScript`
node-options='--no-deprecation'

Related

NodeJS Switch between multiple Nodemon Config files

I have a nodejs project which is using nodemon for providing environment variables when running locally. I have a need to have the developers easily switch between two different nodemon config files (two different sets of environment variables). But I can't see a way to specify a config file for nodemon. How can I accomplish this.
You can specify the config file using the --config flag:
https://github.com/remy/nodemon#config-files

Can I run "npm ci" with a custom npmrc file?

I want to have different paths for different situations for npmrc. For example I want to have a "npmrc.dev" for devs and one for CI "npmrc.ci".
Is this possible? I would be happy with any hack, param, environment variable or some npm module.

Replace env variables in the .npmrc with dotenv before `npm install`

I have to work with some packages in the private registry. So, in my package.json in the dependencies section I have a lines like this one:
...
"dependencies": {
"#myco/my-awesome-package": "^0.4.5",
...
}
...
There is authentication required for the private registry, so I have to create the .npmrc file in my project:
registry=https://registry.npmjs.org/
#myco:registry=https://myco-registry-path/
//myco-registry-path/:username=${MYCO_REGISTRY_USER}
//myco-registry-path/:_password=${MYCO_REGISTRY_PASSWORD_BASE64}
Yes, I know about _authToken, but in my case it is easier to use user and password.
Anyway, here you can see two env variables: ${MYCO_REGISTRY_USER} and ${MYCO_REGISTRY_PASSWORD_BASE64} which I have to replace before npm install.
I know the very simple solution for this problem: put them to the "global" env variables for example to my .bash_profile (or any terminal profile of your choice).
But I do not want to keep variables like this in the "global" scope because the are important only for the current project. What I want to do is to use dotenv. I want to create a .env file in the root of my project:
MYCO_REGISTRY_USER=myco-registry-username-value
MYCO_REGISTRY_PASSWORD_BASE64=myco-registry-password-value-base64
I want that this values replace env variables in my .npmrc on the install action. But when I try npm install I get an error: Error: Failed to replace env in config: ${MYCO_REGISTRY_USER}. I can understand why it happens. Possibly because npm reads .npmrc values first and try to replace env variables and fails, because in this moment it know nothing about dotenv.
My question is how to deal with it?
Short summary:
I do not want to keep env variables in the terminal profile, instead I want to put it in the .env file inside my project.
I have to replace env variables in the .npmrc file with dotenv before npm install
I know this answer might come too late, but in case anyone else is looking for answers, here's a solution:
You need to prepend your scripts with dotenv-cli as so:
dotenv npm install
or in my case where the file was not .env:
dotenv -e .env.local npm install
The problem is that you cannot save this anywhere so that someone can use it with "npm install" somehow. Definitely npm preinstall is run after reading .npmrc so it fails too.
You will need to either document it well or just include a small shell script, but if you're supporting different OSs then it can get funny really fast...
Happily so, CD platforms like Netlify allow you to set environment variables manually.
But I guess this must not be the nicest of starts if someone clones your repo and the first they've got is a failing npm install 🤷‍♂️
Also, check this one out: locking-the-vault-on-font-awesome-npm-tokens

What is meaning of the following line in NPM?

npm init -y
I'm using above line to create node package.json file for my node project using Node Package Manager. But I want to know about -y.
What does it do?
If you will run init command with this flag you will get default package.json file. Take a look at documentation.
If the initializer is omitted (by just calling npm init), init will fall back to legacy init behavior. It will ask you a bunch of questions, and then write a package.json for you. It will attempt to make reasonable guesses based on existing fields, dependencies, and options selected. It is strictly additive, so it will keep any fields and values that were already set. You can also use -y/--yes to skip the questionnaire altogether. If you pass --scope, it will create a scoped package.
read doc here. here already mentioned all the things about npm init
npm init doc

set custom PATH for npm

Is there a way that I can add a directory to the PATH of npm. I DON'T want to add this directory to the machine PATH, just the one npm uses when running scripts.
I know that npm adds node_modules/.bin in addition to any pre-existing machine PATH (see here)
To give more detail on my specific case. I have a project with nested directories, each with its own package.json. When running a script on a sub-directory which depends on a parent binary, the binary won't be found because it's not on the local node_modules/.bin but inside a parent node_modules/.bin.
I could specify the path to the binary inside the script but this is cumbersome and makes the scripts less readable.
So, is there a way to tell npm to export PATH before running every script? It's already doing something like this to add the local node_modules/.bin
I can't think of any simple way to accomplish what you are describing.
You can set/modify environment variables right before a script like:
{
"scripts": {
"parent-script": "PATH=$PATH:/path/to/parent/node_modules/.bin parent-script"
}
}
But as you mentioned this is cumbersome to do this on every script. Also you might as well just do as you described:
{
"scripts": {
"parent-script": "/path/to/parent/node_modules/.bin/parent-script"
}
}
A complicated, but possibly more maintainable approach could be to build yourself a search-script Node module that will traverse parent directories for a script passed as an argument, then run it:
{
"dependencies": {
"search-script": "^0.0.1"
},
"scripts": {
"parent-script": "search-script parent-script"
}
}
Unfortunately NPM does not provide a lot of flexibility for things like this.
Only thing I can see is creating a shell script called npm, place it in a folder in your PATH and remove npm from your PATH and have your shell script set the parent binary dir in your PATH and call the npm binary passing the rest of args. It's not really worth it and might cause other issues.
I would just add all those binaries your scripts in nested folders depend on in their own package.json, which in some ways is worth it, specially if you want to deploy them independently.
This is a node/npm issue itself as it kind of forces you to download the same package multiple times, but at least it's easy to know which version your package uses.

Resources