如何使用我自己的节点模块 [英] How to use my own node module

查看:47
本文介绍了如何使用我自己的节点模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个节点模块,但似乎无法正确使用它.这是我用于所述模块的 index.js:

I created a node module and can't seem to use it properly. Here's my index.js for said module:

const thing = require('./thing.js');
exports.thing = thing;

这里是thing.js:

Here's thing.js:

module.exports = 'foobar';

这是 package.json:

Here's the package.json:

{
  "name": "thing-both",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

这是我想在其他模块中使用的 package.json:

Here's the package.json in my other module that I want to use it in:

{
  "name": "another-thing",
  "description": "",
  "dependencies": {
    "thing-both": "file:../both"
  },
  "private": true
}

我运行 npm install 并安装模块.当我尝试要求我在 anotherThing 模块中创建的模块时,我收到找不到模块"错误.这是我的要求声明:

I run npm install and the module installs. When I try to require the module that I made in anotherThing module, I get the 'Cannot find module' error. This is my require statement:

const thing = require('thing-both');

我注意到的一件事是安装的模块没有文件,并且文件夹在我的目录侧栏中有一个图标(使用 Sublime 3,该图标看起来像一个符号链接图标):

One thing I noticed is that the installed module has no files and the folder has an icon in my directory sidebar (using Sublime 3 and the icon looks like a symlink icon):

这是我正在使用的项目目录:

Here's the project directory I'm working with:

both
  thing.js
  index.js
  package.json
other
  // stuff

我可能做错了什么?

推荐答案

使用 npm link 在你的库模块中,然后在你的消费模块中使用 npm link MODULE_NAME .您收到此错误是因为您的库模块可能未托管在 npm 注册表中,这是 npm install 查找模块的地方.

Use npm link in your library module, then use npm link MODULE_NAME in your consuming module. You're getting this error because your library module is probably not hosted on the npm registry which is where npm install looks for modules.

这是一种进行快速本地开发的方法,而无需在每次进行更改并希望在本地进行测试时将库模块一直提交到 npm 注册表.

This is a way to do rapid local development without having to commit your library module all the way up to the npm registry every time you make a change and want to test locally.

这篇关于如何使用我自己的节点模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆