Javascript 在 Node 模块名称中使用 @ 符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36293481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of @ symbol in Node module names
提问by Derek Janni
I'm looking at some code from a coworker wrote and she's using the @
symbol in require statements. This is the first line of one of these files:
我正在查看一位同事编写的一些代码,她@
在 require 语句中使用了该符号。这是其中一个文件的第一行:
var restServer = require('@company/config')
When I try to run this code, I get an error:
当我尝试运行此代码时,出现错误:
Error: Cannot find module '@company/config'
Error: Cannot find module '@company/config'
Which I frankly expect, there's nothing that looks like this in my directory for require to recognize! It seems like there's some magic going on here, which I hate.
我坦率地说,我的目录中没有任何看起来像这样的东西需要识别!似乎这里发生了一些魔法,我讨厌它。
All I can guess is that either this is some obscure npm or Node trick that I haven't been exposed to, or maybe that there's some other dark art of configuration that I'm not getting. Any info appreciated, even if it's just an explanation of how @
works with require.
我所能猜测的是,这要么是我没有接触过的一些晦涩难懂的 npm 或 Node 技巧,要么可能是我没有了解其他一些黑暗的配置艺术。任何信息都值得赞赏,即使它只是对如何@
使用 require的解释。
Other ideas: Chef is involved somewhere in this whole thing, so that might be relevant.
其他想法:厨师参与了整个事情的某个地方,所以这可能是相关的。
Update: 99% certain this is an issue with the way npm config
works at this point, but still unsure of how to go about fixing it.
更新:99% 确定这是目前工作方式的问题npm config
,但仍然不确定如何解决它。
Update2based on some stuff I uncovered:
Update2基于我发现的一些东西:
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.
采纳答案by Derek Janni
So I solved this one myself.
所以我自己解决了这个问题。
Turns out @company/config
is one of our private NPM repositories, hosted on npm and defined by this alias to an internal GitHub repository: it had nothing to do with how require
works.
原来@company/config
是我们的私有 NPM 存储库之一,托管在 npm 上,并由这个别名定义为内部 GitHub 存储库:它与require
工作原理无关。
Using @
may or may not be a protocol that I was unaware of for private NPM repos, keep that in mind if you run into this.
使用@
可能是也可能不是我不知道的用于私有 NPM 存储库的协议,如果您遇到这种情况,请记住这一点。
回答by vaer-k
Scoped packages in npm are preceded by an '@' symbol: https://docs.npmjs.com/misc/scope
npm 中的作用域包以“@”符号开头:https: //docs.npmjs.com/misc/scope
The docs include additional information on requiring scoped packages: https://docs.npmjs.com/misc/scope#requiring-scoped-packages
文档包括有关需要范围包的其他信息:https: //docs.npmjs.com/misc/scope#requiring-scoped-packages
Requiring scoped packages
Because scoped packages are installed into a scope folder, you have to include the name of the scope when requiring them in your code, e.g.
require('@myorg/mypackage')
There is nothing special about the way Node treats scope folders, this is just specifying to require the module mypackage in the folder called @myorg.
需要作用域包
因为作用域包安装在作用域文件夹中,所以在代码中需要它们时必须包含作用域的名称,例如
require('@myorg/mypackage')
Node 处理范围文件夹的方式没有什么特别之处,这只是指定在名为@myorg 的文件夹中需要模块 mypackage。
回答by Pransh Tiwari
Apart from scoped packages, the '@' can arise due to module-aliaspackage in npm. Through module aliasing you can use frequently used modules without requiring its entire path. Also its effective when directory structure is long. e.g.) require('../../../../some/very/deep/module')
除了作用域包之外,'@' 也可能由于npm 中的模块别名包而出现。通过模块别名,您可以使用常用的模块而无需其完整路径。当目录结构很长时,它也很有效。例如)require('../../../../some/very/deep/module')
Instead you can use: var module = require('@deep/module')
相反,您可以使用: var module = require('@deep/module')
In package.json you can provide the modules for which you are providing alias:
在 package.json 中,您可以提供您为其提供别名的模块:
"_moduleAliases": {
"@root" : ".", // Application's 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 file of the app use this:
在应用程序的主文件中使用这个:
require('module-alias/register');
Refer here for detailed info: module-alias
有关详细信息,请参阅此处:module-alias
回答by user5402942
When you call require()
it reads a route. Since there seems to be no problem if you name a folder as @company
, you should be able to require something with an @
.
当您调用require()
它时,它会读取一条路线。由于将文件夹命名为 似乎没有问题@company
,因此您应该能够要求带有@
.
Your coworker may have wanted to keep @company/config.js
for herself because configurations usually are personal and could not be the same for another user.
您的同事可能希望@company/config.js
为自己保留,因为配置通常是个人的,对于其他用户来说不可能相同。
require
will call files inside your project folder, with a detail:
require
将调用项目文件夹中的文件,详细信息:
- If you call files inside your project folders you must add
./
in front of your route. - If you call any global package such as
http
or anynpm
modules (which are installed atnode_modules
), you can omit the./
.
- 如果您在项目文件夹中调用文件,则必须
./
在路径前添加。 - 如果您调用任何全局包,例如
http
或任何npm
模块(安装在node_modules
),则可以省略./
.
I created a route @company/config
inside my test project folder. It only allowed me to require it using ./@company/config
. Only when i moved the folder inside node_modules
, it allowed me to require('@company/config');
.
我@company/config
在我的测试项目文件夹中创建了一个路由。它只允许我使用./@company/config
. 只有当我将文件夹移到里面时node_modules
,它才允许我require('@company/config');
。
I wouldn't recommend to put any module inside node_modules
, it's just a 'container' for npm
packages. Try to create a new config file and change the require route or simply delete the require and create a config object in your main file.
我不建议在里面放任何模块node_modules
,它只是一个npm
包的“容器” 。尝试创建一个新的配置文件并更改 require 路由,或者简单地删除 require 并在主文件中创建一个配置对象。