首页 > 解决方案 > 如何从 node_modules 导入本地依赖项

问题描述

我已经做到了npm install ./pathtolocalfolder --save。我package.json有一个名称的依赖项:file: ../pathtolocalfolder

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

标签: node.jsnpm

解决方案


我刚刚对此进行了测试,它工作正常。

你甚至不需要使用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'); }

推荐阅读