44

I am trying out VSCode on an existing project that uses npm and has a package.json file with a corresponding "name:" key that reads "SpecPro-File-Management-UI". VSCode is objecting to this line with "String does not match the pattern ...", apparently because of the upper-case characters in the name.

This problem is described in a VSCode issue which is closed. Which leaves me with advice to setup a custom schema for my package.json file. This is pretty unfriendly, and a barrier to adopting VSCode. I don't want to spend my time on custom schemas. I don't want to rename my project. I just want to edit my code and take advantage of the many VSCode goodies without distracting messages that are wrong.

Considering that using uppercase characters for npm packages is a VERY common practice, it seems most reasonable that VSCode should adopt either a more friendly schema or an easy way to override the standard schema. As far as I can tell, I have to make my own personal schema to resolve this problem. That's a lot of work and future maintenance for such a simple issue.

Is there an easy way to banish this erroneous error message?

5
  • 26
    Your name violates the NPM rules for that field. docs.npmjs.com/files/package.json. Make it all-lower-case, or you will have other problems. Commented Feb 8, 2018 at 21:14
  • 1
    That's pretty restrictive. I'm using npm and related technologies in a context where the reasons given in that reference do not apply. And I'm creating this code in the context of an existing project where all the other components use capitalized names. Everything works, but this VSCode message is unnecessary and annoying. Commented Feb 9, 2018 at 22:22
  • I agree the restriction is annoying, but you will run into issues with other tools (NPM/JSPM/Yarn/Bower/random devDependencies) sooner or later. Just follow the convention because in this case it doesn't hurt. Commented Feb 9, 2018 at 23:58
  • 1
    Since I am not using most of those tools, those potential issues do not apply. So this VSCode warning message is an irritation that I want to suppress. I don't want to have to go against the existing decades-old conventions of existing code just to please VSCode. Overall, I really like VSCode. But this issue is definitely a negative. Commented Feb 12, 2018 at 17:18
  • 1
    VS Code is behaving correctly. Why should it tolerate an invalid manifest? Note that you can run into issues even locally with npm link or if you have a private NPM registry and package server. NPM has a registry, you are wasting your time trying to maintain a convention that does not matter. Commented Feb 12, 2018 at 17:48

4 Answers 4

60

This behavior is by design to enforce NPM conventions for the package.json file (to paraphrase, "lower-case only"). I agree it is a nuisance, especially since the project name is often pre-filled, e.g. by "create-react-app". As you point out, it is possible to create a custom schema to ignore this, but it's really not recommended. There isn't any alternative at this time. Myself, I just change the value to lower-case.

Sign up to request clarification or add additional context in comments.

Comments

28

Use name field value in lowercase separated by hyphen (-).

3 Comments

It does not work for me. I tried removing the "name" prop, then it worked.
Can you please provide the name value you are using ?
Would it matter if you use "-" or "_" ?
1

In this case, you want to use your custom name without VSCode warning you about the name, unfortunately there is not much to do but to write your custom Schema and use it in your package.json.

For example, you can take the package.json schema in https://json.schemastore.org/package.json and see this part of code that specifies a pattern to follow for the name of packages:

        "name": {
            "description": "The name of the package.",
            "type": "string",
            "maxLength": 214,
            "minLength": 1,
            "pattern": "^(?:(?:@(?:[a-z0-9-*~][a-z0-9-*._~]*)?/[a-z0-9-._~])|[a-z0-9-~])[a-z0-9-._~]*$"
        },

This is the Schema that Visual Studio Code uses by default in a package.json.


You'll need to write your own schema in a JSON file, for example MyOwnSchema.json.

Now, I want to suppress the pattern warning, then I do the follow, just remove the pattern property:

        "name": {
            "description": "The name of the package.",
            "type": "string",
            "maxLength": 214,
            "minLength": 1
        },

Or if you want, you can use your custom pattern...

        "name": {
            "description": "The name of the package.",
            "type": "string",
            "maxLength": 214,
            "minLength": 1,
            "pattern": "^MyOwnCustomName$"
        },

Now, in your package.json you need to specify the schema you modified to use it in your file like this:

{
    "$schema": "./MyOwnSchema.json",
    "name": "MyOwnCustomName",
}

This will suppress the warning... Just consider some things, you're using a custom schema and not the official provided by Node.js or NPM packages, so you need to be sure of what you are doing... Check that everything is correct, this will change the behaviour of the name but not of the other properties, you can change it too if you want... Also consider that if the official schema is updated, you will not have the recent changes.

Or if you don't want to complicate the things too much, just rename the package as it says with the pattern that they want (my-package).

Comments

-3

In tsconfig.json add this line or set its value to false. This worked for me.

"forceConsistentCasingInFileNames": false, // Fix for name regex pattern

1 Comment

this is not ts only problem. it's linter releated. so a js npm project can has this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.