如何使用我自己的节点模块

时间:2021-01-21 07:16:13

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

我创建了一个节点模块,似乎无法正常使用它。这是我所说的模块的index.js:

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

Here's thing.js:

这是thing.js:

module.exports = 'foobar';

Here's the package.json:

这是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"
}

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

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

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

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:

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

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

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):

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

如何使用我自己的节点模块

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

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

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

What might I be doing wrong here?

我可能在这里做错了什么?

1 个解决方案

#1


0  

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链接,然后在使用模块中使用npm链接MODULE_NAME。您收到此错误是因为您的库模块可能未托管在npm安装程序上,而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.

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

#1


0  

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链接,然后在使用模块中使用npm链接MODULE_NAME。您收到此错误是因为您的库模块可能未托管在npm安装程序上,而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.

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