Visual Studio Code global typings definitions - node.js

I was wondering if there is any way to save global typings definitions for VS Code. I know you can add them to your project but I think it would be much cleaner if I could install these globally, so it wouldn't show up under my project. Also, it would make it much faster to start a new project if VS Code already knew all these definition without me having to add them every time I start.
I'm not looking for much, having definitions for Node, JQuery and Express would suffice for now.
Thanks

I do not really think that local definitions are the problem. Sometimes you might get to the point when you do need to have different versions of the same definition files in different projects or do not want to have some of them due to the conflicts with systems ones in lib.d.ts. Therefore I believe local storage is a better choice.
I use it all the time and to overcome your issues I can recommend to do the following.
Add "files.exclude" to the .vscode/settings.json:
{
"files.exclude":
{
"typings": true
}
}
This will hide all typings so you do not see them in project.
To start project from scratch fast you can have a tsd.json with commonly used definitions. For example:
{
"version": "v4",
"repo": "borisyankov/DefinitelyTyped",
"ref": "master",
"path": "typings",
"bundle": "typings/tsd.d.ts",
"installed": {
"es6-promise/es6-promise.d.ts": {
"commit": "5a8fc5ee71701431e4fdbb80c506e3c13f85a9ff"
},
"jasmine/jasmine.d.ts": {
"commit": "5a8fc5ee71701431e4fdbb80c506e3c13f85a9ff"
}
}
}
And correspondingly in your package.json add postinstall action like this:
{
"scripts": {
"postinstall": "tsd update --save --overwrite"
}
}
After that all you have to do to initialize definitions for the new project is to run
npm install
This will fetch all definitions and you will not see them in the IDE.
Hope this will help.

Related

How to disable local package dependency for `npm install --prefix`?

I have a project with at its root the following simplified package.json file:
package.json
{
"name": "parent-project",
"dependencies": {
...
}
}
In a subfolder that we'll call child-project, another package.json resides:
child-project / package.json
{
"name": "child-project",
"dependencies": {
...
}
}
Some code that I depend on uses the command npm --prefix ./child-project install to install dependencies in child-project. However, this always has the undesirable side-effect of altering child-project/package.json like so:
{
"name": "child-project",
"dependencies": {
...
"parent-project": "file:.." // <- I don't want this!
}
}
When I execute cd ./child-project && npm install all is fine and child-project/package.json remains untouched, so my hunch is that it has to do with --prefix but documentation on --prefix is very obscure.
Is there a way to disable this behaviour and prevent NPM from altering the child-project/package.json?
Seems like there are some issues with the --prefix.
However, I couldn't find any issue while I was testing with different modules which have sub-packages.
Hence, according to one of the npm contributors, it is going to be refactored or removed in a future release, which means it is not the best choice to use it at least for now. So I recommend you go through the traditional way and avoid using --prefix.
I think we're gonna close this as a wontfix as --prefix is not the best flag to be promoting. We do eventually want to refactor or remove it but for now we should at least not be promoting it.

How to make user install same version of depency A as depency B is using?

I used the variables in the title of this topics, but I also have the live example.
The alpha version 1.5.0-alpha.0 of the package #yamato-daiwa/es-extensions-localization-japanese depends on version 1.5.1 of #yamato-daiwa/es-extensions:
{
"name": "#yamato-daiwa/es-extensions-localization-japanese",
"version": "1.5.0-alpha.0",
"dependencies": {
"#yamato-daiwa/es-extensions": "1.5.1"
},
"peerDependencies": {
"#yamato-daiwa/es-extensions": ">=1.5.0 <1.6.0"
},
// ...
}
If to install the correct versions of both packages, both distributables will be put directly below node_modules/#yamato-daiwa:
{
"private": true,
"dependencies": {
"#yamato-daiwa/es-extensions": "1.5.1",
"#yamato-daiwa/es-extensions-localization-japanese": "1.5.0-alpha.0"
}
}
Now let's assume that I made a mistake and installed the version 1.5.0 of #yamato-daiwa/es-extensions. In this case, the additional instance of #yamato-daiwa/es-extensions will be put below node_modules/#yamato-daiwa/es-extensions-localization-japanese/node_modules.
The localization will be applied anymore, but there also will be any error, so if it was not the experiment, I could not undestrand the cause.
How to make use install the appropriate version of #yamato-daiwa/es-extensions? Is more strict peerDependencies like "#yamato-daiwa/es-extensions": "1.5.1" will be enough?
As you noted - you have 2 installations #yamato-daiwa/es-extensions - in main app's node_modules and in #yamato-daiwa/es-extensions-localization-japanese node_modules. Your application uses the package from its node_modules folder, but the localization uses another instance from its node_modules. That's why localization is not working.
To prevent this, you should enable strict-peer-deps property in the project's .npmrc file. npm will treat non-compatible peer dependencies as a failure.

How to update a dependency within a dependency?

I would like to update a 'handlebars' node module that is a dependency of Vue-Cli (see screenshot).
What's the correct way to do this?
Thank you
Add a resolutions field to your package.json file and define your version overrides.
It will look like this
{
...
"dependencies": {
...
},
"devDependencies": {
...
},
"resolutions": {
"EXAMPLE_PACKAGE": "EXAMPLE_PACKAGE_NEEDED_VERSION"
}
}
It shouldn't update the package in the dependency. But your application will use the needed version. It can be useful for example if some dependencies in your dependency have important security updates, and your dependency has not updated the version yet.

Using babel with preset-env on a small node project, unable to convert import/export statements to commonjs

I'm trying to set up a node project so I can use the common import/export syntax, and for compatibility, use babeljs to convert the import and export statements to requires.
When I try transpiling using babel though, I'm not seeing any changes and I don't know why.
Here's my babel.config.js
// babel.config.js
module.exports = {
// I thought this was what I would need to add for making the changes, based on the docs
plugins: ["#babel/plugin-transform-modules-commonjs"],
// env takes care of the majority of changes I might need to convert code to make
// easy to run on a wider range of machines
presets: [
[
"#babel/preset-env",
{
targets: {
// target an older version where I know there is no ESM support
node: "10"
}
}
]
],
// this was in the docs too - I'm not sure if I need it TBH
sourceType: "module"
}
I have a bunch of code using import/export, and async/await in a directory, that I wanted to transpile and put into another directory, lib.
Here's the command I am using:
npx babel src --out-dir lib
My package.json file online too, and there's nothing too esoteric there. I've tried to follow the documentation faithfully.
I would expect to see changes in the code to replace the import/export calls with requires in the lib, but no joy.
What am I doing wrong here? I worked on a related project 6-7 months ago, where I am using babel like this and it does make the changes for me.
You can see the branch with the problem code on github, with the package.json, and here's the source code for other project mentioned where it is working.
Please be gentle, fellow SO commenters - I'm trying my best, I really am.
It looks like you're using "babel-cli": "^6.26.0" instead of "#babel/cli": "^7.11.6" that should fix it.

How to specify/enforce a specific node.js version to use in package.json?

I am searching for a way to break the build, if a user is using a different node.js version as defined in the project.
Ideally to put some checks in grunt or bower or npm to stop, if a certain npm/node version is not used to run the current build.
Even though engineStrict is deprecated, you can still accomplish this behavior without needing to use an additional script to enforce a Node version in your project.
Add the engines property to your package.json file. For example:
{
"name": "example",
"version": "1.0.0",
"engines": {
"node": ">=14.0.0"
}
}
Create a .npmrc file in your project at the same level as your package.json.
In the newly created .npmrc file, add engine-strict=true.
engine-strict=true
This will enforce the engines you've defined when the user runs npm install. I've created a simple example on GitHub for your reference.
You can use the "engineStrict" property in your package.json
Check the docs for more information: https://docs.npmjs.com/files/package.json
Update on 23rd June 2019
"engineStrict" property is removed in npm 3.0.0.
Reference : https://docs.npmjs.com/files/package.json#enginestrict
"engineStrict" has been removed and "engines" only works for dependencies. If you want to check Node's runtime version this can work for you:
You call this function in your server side code. It uses a regex to check Node's runtime version using eremzeit's response It will throw an error if it's not using the appropriate version:
const checkNodeVersion = version => {
const versionRegex = new RegExp(`^${version}\\..*`);
const versionCorrect = process.versions.node.match(versionRegex);
if (!versionCorrect) {
throw Error(
`Running on wrong Nodejs version. Please upgrade the node runtime to version ${version}`
);
}
};
usage:
checkNodeVersion(8)
You can use the engines property in the package.json file
For example, if you want to make sure that you have a minimum of node.js version 6.9 and a maximum of 6.10, then you can specify the following
package.json
{
"name": "Foo",
....
"engines": {
"node": ">=6.9 <=6.10"
}
}
If you want to enforce a specific version of npm, you might use:
https://github.com/hansl/npm-enforce-version
If you want to enforce a version of node when executing you can read the version of node that is currently running by checking against:
process.versions
For more info: https://nodejs.org/api/process.html#process_process_versions

Resources