node.js - 如何从 node_modules 导入本地依赖项
问题描述
我已经做到了npm install ./pathtolocalfolder --save
。我package.json
有一个名称的依赖项:file: ../pathtolocalfolder
。
如何在我的项目中导入文件夹?npm 无法解析./node_modules/folder
或folder
.
解决方案
我刚刚对此进行了测试,它工作正常。
你甚至不需要使用npm install ./testmodule --save
,但你的模块必须有 package.json
Main ./package.json 参考 testmodule
{
"name": "npmtest",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"testmodule": "file:testmodule"
}
}
主 ./app.js
var testmod = require('testmodule');
testmod.run();
./testmodule/package.json
{
"name": "testmodule",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
./testmodule/index.js
exports.run = function() { console.log('testmodule'); }
推荐阅读
- ios - 不能使用 String 类型的索引为 NSDictionary 类型的值下标。从 Swift 2.3 -> 3.2 转换时
- html - 根据我的第 3 段,我该如何将图像设置在左中?
- java - 循环消息/任务队列现有解决方案
- php - 在徽标旁边添加另一张图片 - Wordpress
- node.js - 为 Let's encrypt 设置 certbot 时选择哪个软件和系统?
- c# - GridView - 选择第一行不起作用
- javascript - GraphQLError:语法错误:无法解析意外字符“;”
- json.net - Json.NET - 根据值改变架构/合同的正确方法?
- vue.js - 如何通过lodash映射并在prop值中返回false
- r - 插入符号训练预处理中的相关截止