标签: node-modules

为什么 Jest 仍然需要一个模拟模块?

我正在使用 Jest 模拟一个模块,因为它包含不应在测试中运行的代码。不过,我可以从输出模块中的代码中看到的是正在运行。

// foo.js
console.log('Hello')

// test.js
jest.mock('./foo')
const foo = require('./foo')

test.todo('write some tests')
Run Code Online (Sandbox Code Playgroud)

控制台输出

PASS  test.js
  ? todo 1 test

console.log foo.js:1
Hello
Run Code Online (Sandbox Code Playgroud)

那是怎么回事?

javascript mocking node-modules jestjs

5
推荐指数
1
解决办法
265
查看次数

如何防止从父节点模块继承包

首先,我将给出问题的一些背景。

我正在开发一个 npm 库。在项目文件夹中,我有另一个名为“example”的文件夹,用于测试库。结构如下所示。

|- node_modules/
|- src/ 
|- example/
|   |- node_modules/
|   |- src/
|   |- package.json
|- package.json 
Run Code Online (Sandbox Code Playgroud)

根 package.json 具有依赖项babel-jest。example/package.json 具有依赖项react-scripts。react-scripts start在示例目录中运行时,会出现以下错误,

错误截图


据我所知,这是因为 example/ 目录中的 package.json 继承了(不确定这是否正确)根 package.json 的依赖项。

也就是说,我可以使用安装在根 package.json 中的依赖项,在 example/ 的 src/ 里面,这在某些情况下很方便。但这对我的用例来说是一个障碍。

我怎样才能防止这种行为?(不改变目录结构)

谢谢你。

npm node-modules react-scripts

5
推荐指数
1
解决办法
244
查看次数

错误:部署到 docker 容器后找不到模块“./build/Debug/addon”

我正在使用以下版本。节点-v v8.16.0 堆转储:0.3.12

一旦我将代码部署到 docker 容器,就会出现以下错误。本地设置工作正常,没有这个问题。

错误:找不到模块“./build/Debug/addon”

请帮助解决这个问题。

node.js heap-dump node-modules

5
推荐指数
0
解决办法
1030
查看次数

没有规则来制作目标`node_modules/node-gyp/addon.gypi`,`Makefile'需要

我在尝试安装我正在处理的软件包的要求时遇到错误,它到达integer@2.1.0然后由于node-gyp.

  • 节点版本:10.10.0 / 6.4.1
  • 平台:Darwin Matts-MBP-3.local 19.0.0 Darwin Kernel Version 19.0.0:2019 年 5 月 24 日星期五 17:36:10 PDT;根:xnu-6041.0.0.111.5~1/RELEASE_X86_64 x86_64
  • 编译器:Apple clang version 11.0.0 (clang-1100.0.20.17) Target: x86_64-apple-darwin19.0.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin
> integer@2.1.0 install /Users/mattcowley/WebstormProjects/MagicCap/node_modules/integer
> node-gyp rebuild

make: *** No rule to make target `../../../../../../../usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi', needed by `Makefile'.  Stop.
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:262:23)
gyp ERR! …
Run Code Online (Sandbox Code Playgroud)

makefile node.js npm node-gyp node-modules

5
推荐指数
1
解决办法
744
查看次数

node_modules 包如何读取项目根目录中的配置文件?

我正在创建一个需要能够从项目根目录读取配置文件的 npm 包。我不知道该怎么做。

例如,

  • Next.js 能够从项目根目录中读取./pages/和读取./next.config.js
  • Jest 能够./jest.config.js从项目根目录读取
  • ESLint 能够./.eslintrc.json从项目根目录读取
  • Prettier 能够./.prettierrc.js从项目根目录读取
  • 打字稿能够./tsconfig.json从项目根目录读取
  • Babel 能够./.babelrc从项目根目录读取

我试过查看他们的源代码以了解他们是如何做到的,但项目太大了,我找不到相关部分。

他们是如何做到这一点的?

javascript node.js npm node-modules

5
推荐指数
1
解决办法
284
查看次数

如何解决“找不到模块‘请求上下文’的声明文件。”?

我目前正在处理三个文件,分别是 index.js、index.main.js 和 app.js。我正在使用请求上下文从 index.main.js 中获取一个变量并将其传递给 index.js。

在 app.js(我在服务器文件夹中创建的文件)中,我有以下代码

    //full code in app.js

    const contextService = require("request-context");
    const app = express();

    app.use(contextService.middleware("request"));
Run Code Online (Sandbox Code Playgroud)

我试过运行以下命令

npm install --save typescript @types/node @types/react @types/react-dom @types/jest

npm install -D @types/request-context
Run Code Online (Sandbox Code Playgroud)

并在导入前尝试使用

// @ts-ignore
Run Code Online (Sandbox Code Playgroud)

无济于事。

当我检查我的 app.js 时,我注意到“require”这个词上有三个点,它显示:

找不到模块“请求上下文”的声明文件。'/home/servertest/Desktop/folder/folder1/src/component_NodeJS/server/node_modules/request-context/lib/index.js' 隐式具有 'any' 类型。尝试npm install @types/request-context它是否存在或添加一个包含declare module 'request-context';ts(7016)的新声明 (.d.ts) 文件

在 index.main.js 我有以下内容

 async function listFilesInDepth()

    {
      const {Storage} = require('@google-cloud/storage');

      const storage = new Storage();

      const bucketName = 'probizmy';

      const [files] = await …
Run Code Online (Sandbox Code Playgroud)

requestcontext node.js node-modules typescript

5
推荐指数
1
解决办法
2万
查看次数

导入带有导出枚举的模块时找不到模块错误

我正在尝试导入一个导出枚举类型的自定义模块。但是,当我尝试使用导出的枚举时,出现此错误Error: Cannot find module 'Runescape'

如果我不使用枚举,则代码有效,因此导入语句确实有效,或者如果我将枚举移动到我的主 ts 文件中。我试过编译器选项“preserveConstEnums”

配置文件

{
    "compilerOptions": {
      "resolveJsonModule": true,
      "moduleResolution": "node",
      "esModuleInterop": true,
      "sourceMap": true,
      "outDir": "out",
      "target": "es6"
    }
}
Run Code Online (Sandbox Code Playgroud)

Runescape.ts

declare module 'Runescape' {
...
    /**
    * @description Enum of all Runescape skills
    * @enum
    * @default
    */
    export enum SkillsEnum {
        ATTACK = 'attack',
        STRENGTH = 'strength',
        DEFENSE = 'defense',
        RANGED = 'ranged',
        PRAYER = 'prayer',
        MAGIC = 'magic',
        RUNECRAFT = 'runecraft',
        CONSTRUCTION = 'construction',
        HITPOINTS = 'hitpoints',
        AGILITY = 'agility', …
Run Code Online (Sandbox Code Playgroud)

import enums node.js node-modules typescript

5
推荐指数
1
解决办法
3165
查看次数

npm install 在构建 docker 镜像时返回连接超时

我正在尝试制作一个 docker 图像,包括newman. 我尝试使用 构建图像node:10-alpine,但在npm install -g newman. 这是我的 docker 命令:

RUN set http_proxy= && \
    set https_proxy= && \
    yarn config delete proxy && \
    npm config rm https-proxy && \
    npm config rm proxy && \
    npm config set registry http://registry.npmjs.org/ && \
    npm config set strict-ssl false && \
    npm cache clean --force && \
    npm cache verify && \
    npm install -g newman
Run Code Online (Sandbox Code Playgroud)

完整的错误是:

npm ERR! code ECONNREFUSED
npm ERR! …
Run Code Online (Sandbox Code Playgroud)

npm node-modules docker npm-install

5
推荐指数
1
解决办法
2735
查看次数

如何在 TypeScript npm 模块中导出类型

在 TypeScript 中,假设我想让用户使用我模块的“内部”类型,以便他们在使用我的模块时可以正确键入他们自己的变量 - 我是否只是从我的 index.ts 文件中导出所有内容来完成此操作?

// index.ts

export * from './file1' // uses types/interfaces defined in file1types
export * from './file2' // uses types/interfaces defined in file2types
export * from './types/file1types'
export * from './types/file2types'
Run Code Online (Sandbox Code Playgroud)

我曾尝试阅读 TypeScript 声明文件,但有关这方面的信息非常零散。.d.ts文件是否可以帮助我完成此任务,还是仅适用于非 TS 项目?tsconfig.json的选项是否declaration: true通过.d.ts为每个 TS 文件生成一个来帮助我完成此操作?这是从单个 index.ts 文件导出所有内容的替代方法吗?

如果declaration: true确实可以帮助我完成此操作,用户将如何使用.d.ts文件build夹中所有生成的文件?

我非常感谢您对 TS 项目中通常如何导出类型的一些说明。提前致谢。

node.js node-modules typescript

5
推荐指数
2
解决办法
6980
查看次数

打包的电子应用程序找不到本地模块

我有一个基于电子反应样板模板项目的电子应用程序。在我的应用程序中,我在一个myClass.js文件中有一个类

app/utils/myClass.js

class MyClass {
// A bunch of stuff...
}
module.exports = MyClass;
Run Code Online (Sandbox Code Playgroud)

我在渲染器进程预加载脚本中引用了这个类。

app/utils/preload.js

const MyClass = require('./myClass.js');
Run Code Online (Sandbox Code Playgroud)

我像这样在渲染器后台工作进程中加载

const workerWindow = new BrowserWindow({
    show: false,
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true,
      preload: require('path').join(__dirname, 'utils', 'preload.js')
    }
  });
Run Code Online (Sandbox Code Playgroud)

当我在开发/调试模式下运行应用程序时,MyClass发现模块很好,所有代码都可以正常工作。但是,当我打包应用程序(使用电子生成器)并安装在 Windows 上时,运行代码会导致以下错误:

{"code":"MODULE_NOT_FOUND","requireStack":["C:\\Users\\Cooper\\AppData\\Local\\Programs\\my-app\\resources\\app\\utils\\myClass.js","C:\\Users\\Cooper\\AppData\\Local\\Programs\\my-app\\resources\\app\\utils\\preload.js"]}

出于某种原因,该应用程序找不到我的 MyClass 模块,即使我可以确认这些文件位于打包的 .asar 存档中的相应目录中。我已经尝试了许多不同的方法require来调用 MyClass 但无济于事。

为什么我打包的应用程序找不到我的 MyClass 模块?我该如何解决这个问题?值得注意的是,我的 preoload.js 脚本与任何其他依赖项都没有问题。require('electron')例如,我可以毫无问题地做到。

javascript node.js node-modules electron electron-builder

5
推荐指数
1
解决办法
764
查看次数