如何从node_modules导入本地依赖项

时间:2020-06-04 10:03:34

标签: node.js npm

我已经完成npm install ./pathtolocalfolder --save。 我的package.json有一个名为file: ../pathtolocalfolder的依赖项。

如何将文件夹导入项目中? npm无法解析./node_modules/folderfolder

1 个答案:

答案 0 :(得分:0)

我刚刚测试了一下,效果很好。

您甚至不需要使用npm install ./testmodule --save,但是您的模块必须具有package.json

主要./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'); }