Using the @ Symbol in Node Module Names

I look at the code written by a colleague and she uses the @ symbol in requests. This is the first line of one of these files:

 var restServer = require('@company/config') 

When I try to run this code, I get an error message:

Error: Cannot find module '@company/config'

I honestly expect nothing like this to be recognized in my catalog! It seems like some kind of magic is happening here that I hate.

All I can guess is that either this is some kind of obscure npm or Node trick that I have not been exposed to, or maybe there is another dark configuration art that I don’t get. Any information appreciated, even if it's just an explanation of how @ works, is required.

Other ideas: the chef is involved somewhere in all of this, so this may be relevant.

Update : 99% are sure that this is a problem with how npm config works, but still not sure how to do it.

Update2 based on some things that I revealed:

 Dereks-MacBook-Pro:project-dir derekjanni$ npm config set //registry.npmjs.org/:authtoken $SECRET_TOKEN Dereks-MacBook-Pro:project-dir derekjanni$ npm install npm ERR! Darwin 15.0.0 npm ERR! argv "/usr/local/Cellar/node/5.5.0/bin/node" "/usr/local/bin/npm" "install" npm ERR! node v5.5.0 npm ERR! npm v3.5.3 npm ERR! code E404 npm ERR! 404 Not found : @company/config npm ERR! 404 '@company/config' is not in the npm registry. 
+62
javascript npm chef
Mar 29 '16 at 19:31
source share
5 answers

So, I decided it myself.

Turns off @company/config - this is one of our private NPM repositories hosted on npm and defined by this alias for the internal GitHub repository: it has nothing to do with how require works.

Using @ may or may not be a protocol that I did not know about private NPM repositories, remember this if you come across this.

+12
Apr 14 '16 at 23:48
source share

Packages in npm are preceded by the @ symbol: https://docs.npmjs.com/misc/scope

The docs include additional information on area requirements: https://docs.npmjs.com/misc/scope#requiring-scoped-packages

Required Packages

Since the copied packages are installed in the scope folder, you must specify the name of the scope when they are required in your code, for example

 require('@myorg/mypackage') 

There is nothing special about the way Node handles folder areas; it just indicates the need to use the mypackage module in a folder named @myorg.

+36
Oct 21 '16 at 22:35
source share

When you call require() , it reads the route. Since there seems to be no problem if you name the folder as @company , you may require something with @ .

Perhaps your colleague wanted to keep @company/config.js for himself, because the configurations are usually personal and cannot be the same for another user.

require will call files inside the project folder with detailed information:

  • If you call files inside project folders, you must add ./ in front of your route.
  • If you call any global package, such as http or any npm modules (which are installed in node_modules ), you can omit ./ .

I created the @company/config route inside my test project folder. This allowed me to request it using ./@company/config . Only when I moved the folder inside node_modules did this allow me to require('@company/config'); .

I would not recommend placing any module inside node_modules , it is just a "container" for npm packages. Try creating a new configuration file and changing the desired route, or simply delete the request and create a configuration object in the main file.

+2
Apr 14 '16 at 23:18
source share

In addition to packages with scope, "@" can arise from a package of modules with an alias in npm. With modular anti-aliasing, you can use frequently used modules without requiring a full path. It is also effective when the directory structure is long. e.g.) require('../../../../some/very/deep/module')

Instead, you can use: var module = require('@deep/module')

In the .json package, you can provide modules for which you provide an alias:

 "_moduleAliases": { "@root" : ".", // Application root "@deep" : "src/some/very/deep/directory/or/file", "@my_module" : "lib/some-file.js", "something" : "src/foo", // Or without @. Actually, it could be any string } 

And in the main application file use this:

 require('module-alias/register'); 

See here for more details: module-alias

+2
Aug 08 '18 at 6:25
source share

@Scope stands for package ownership

The main advantage of the areas I've seen so far is that each area is controlled by the npm account of the organization / user, just like the usernames / names of GitHub organizations.

In this way, you can easily determine if the package you are viewing belongs to an organization that you trust, or is it a third-party tool.

For example, if you see:

 @angular/cli 

then you know that it comes from the Angular team and you can be trusted.

On the other hand, the same cannot be said for:

 angular-cli 

See also: What does the prefix "at" (@) mean in npm packages?

+2
Feb 03 '19 at 22:00
source share



All Articles