javascript 如何在节点中要求 npm 主文件以外的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33016714/
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
How to require a file other than the npm main file in node?
提问by Mike Sherov
package.json:
包.json:
...
"name": "mypackage",
"main": "src/index.js"
...
Directory structure:
目录结构:
|- src/
|--- index.js
|--- other.js
I can require src/index.js
with require('mypackage');
, but how can I require src/other.js
?
我可以要求src/index.js
with require('mypackage');
,但我怎么可以要求src/other.js
?
If the answer is require('mypackage/src/other');
, is there a way to make it so I can require it with require('mypackage/other');
(i.e. teaching node what the source file directory is of your module?
如果答案是require('mypackage/src/other');
,有没有办法让它这样我可以要求它require('mypackage/other');
(即教节点你的模块的源文件目录是什么?
回答by Creynders
AFAIK You'll have to explicitly expose it in the root:
AFAIK 您必须在根目录中明确公开它:
Directory structure:
目录结构:
|- src/
|--- index.js
|--- other.js
|- other.js
Then in /other.js
然后在 /other.js
module.exports = require('src/other.js');
Now you can do require('mypackage/other')
现在你可以做 require('mypackage/other')
回答by Norbert
I'm currently looking into the exact same thing.
我目前正在研究完全相同的事情。
Package.json
has a property called 'files':
Package.json
有一个名为“文件”的属性:
http://blog.kewah.com/2014/npm-as-a-front-end-package-manager/
http://blog.kewah.com/2014/npm-as-a-front-end-package-manager/
https://docs.npmjs.com/files/package.json
https://docs.npmjs.com/files/package.json
The "files" field is an array of files to include in your project. If you name a folder in the array, then it will also include the files inside that folder.
“文件”字段是要包含在项目中的文件数组。如果您在数组中命名一个文件夹,那么它也将包含该文件夹中的文件。
But I have yet to find how to do a import/require of such a file.
I don't really see another point in listing these files other then to be able to import/require
them?
但是我还没有找到如何导入/要求这样的文件。除了能够访问这些文件之外,我真的没有看到列出这些文件的另一点import/require
?
I was able to import a file from a package if it was listed in this files array.
如果文件列在此文件数组中,我可以从包中导入文件。
{
"name": "local-ui-utilities",
"version": "0.0.1",
"description": "LOCAL UI Utilities",
"main": "index.jsx",
"author": "Norbert de Langen",
"license": "none",
"dependencies": {
},
"files": [
"/colors/sets/variables.css"
]
}
I'm able to import the css file from the package using postcss-import:
我可以使用 postcss-import 从包中导入 css 文件:
@import "local-ui-utilities/colors/sets/a.css";
This probably isn't your use-case, but postcss-import just uses npm under the hood. So This should work for your use-case as well, I would think.
这可能不是您的用例,但 postcss-import 只是在幕后使用 npm。因此,我认为这也适用于您的用例。
This question and accepted answer seem related: Node/NPM: Can one npm package expose more than one file?
这个问题和接受的答案似乎相关: Node/NPM:一个 npm 包可以公开多个文件吗?
回答by Patrick
You'll have to explicitly expose the file in the root folder, but many projects (including older versions of lodash) do this as part of a pre-publish step. In fact there's a packagethat does exactly what @Creynders suggests, adding module.exports = require('./path/to/file')
files in your root folder. A while back I wrote up a guideon getting started, but the gist is pretty simple.
您必须在根文件夹中显式公开该文件,但许多项目(包括旧版本的 lodash)都将此作为预发布步骤的一部分。事实上,有一个包完全符合@Creynders 的建议,module.exports = require('./path/to/file')
在您的根文件夹中添加文件。不久前,我写了一份入门指南,但要点非常简单。
Install
安装
npm install --save-dev generate-export-aliases
npm install --save-dev generate-export-aliases
Configure
配置
{
"name": "my-package",
"scripts": {
"prepublish": "generate-export-aliases"
},
"config": {
"exportAliases": {
"other": "./src/other"
}
}
}
Use
利用
const other = require('my-package/other')
const other = require('my-package/other')
DISCLAIMER: I'm the author of the package
免责声明:我是包的作者