Pnpjs: Error: Cannot find module '@pnp/sp/files' -- after upgrade 2.0.8 -> 2.0.9

Created on 6 Sep 2020  路  5Comments  路  Source: pnp/pnpjs

Category

  • [x] Bug

Version

Please specify what version of the library you are using: [ 2.0.9 ]

Please specify what version(s) of SharePoint you are targeting: [ SP online ]

node.js: 12.18.0

typescript: 4.0.2

OS: latest Windows 10

Expected / Desired Behavior / Question

following code can be executed

import { SPFetchClient } from '@pnp/nodejs-commonjs';
import { sp } from '@pnp/sp-commonjs';

export function initializeSp(): void {
    sp.setup({
        sp: {
            fetchClientFactory: () => {
                return new SPFetchClient('APP_SP_SITE_URL', 'APP_LEGACY_SP_CLIENT_ID', 'APP_LEGACY_SP_CLIENT_SECRET');
            },
        },
    });
}

Observed Behavior

C:\test\src>npm start

> [email protected] start C:\test
> npx tsc && node dist/app.js

internal/modules/cjs/loader.js:969
  throw err;
  ^

Error: Cannot find module '@pnp/sp/files'
Require stack:
- C:\test\node_modules\@pnp\nodejs-commonjs\sp-extensions\stream.js
- C:\test\node_modules\@pnp\nodejs-commonjs\sp-extensions\index.js
- C:\test\node_modules\@pnp\nodejs-commonjs\index.js
- C:\test\dist\api\news\spConnector.js
- C:\test\dist\app.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)
    at Function.Module._resolveFilename (C:\test\node_modules\module-alias\index.js:49:29)
    at Function.Module._load (internal/modules/cjs/loader.js:842:27)
    at Module.require (internal/modules/cjs/loader.js:1026:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (C:\test\node_modules\@pnp\nodejs-commonjs\sp-extensions\stream.js:6:15)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\test\\node_modules\\@pnp\\nodejs-commonjs\\sp-extensions\\stream.js',
    'C:\\test\\node_modules\\@pnp\\nodejs-commonjs\\sp-extensions\\index.js',
    'C:\\test\\node_modules\\@pnp\\nodejs-commonjs\\index.js',
    'C:\\test\\dist\\api\\news\\spConnector.js',
    'C:\\test\\dist\\app.js'
  ]
}

Steps to Reproduce

On the PnPjs version 2.0.8 the procedure initializeSp() can be executed. After upgrading to v2.0.9 the code cannot be executed.

Dependencies:

    "dependencies": {
        "@pnp/nodejs-commonjs": "2.0.9",
        "@pnp/sp-commonjs": "2.0.9",
        "apollo-server-koa": "~2.17.0",
        "dotenv": "~8.2.0",
        "graphql-playground-middleware-koa": "~1.6.19",
        "koa-passport": "~4.1.3",
        "koa-router": "~9.4.0",
        "module-alias": "~2.2.2",
        "passport-azure-ad": "~4.2.1"
    },
    "devDependencies": {
        "@tsconfig/node12": "~1.0.7",
        "@types/dotenv": "~8.2.0",
        "@types/koa-passport": "~4.0.2",
        "@types/koa-router": "~7.4.1",
        "@types/passport-azure-ad": "~4.0.7",
        "@typescript-eslint/eslint-plugin": "^3.10.1",
        "@typescript-eslint/parser": "^3.10.1",
        "del": "~5.1.0",
        "eslint": "^7.7.0",
        "eslint-plugin-jsdoc": "~30.3.0",
        "gulp": "~4.0.2",
        "gulp-zip": "~5.0.2",
        "husky": "~4.2.5",
        "object-assign": "~4.1.1",
        "typescript": "^4.0.2"
    }

tsconfig.json:

{
    "extends": "@tsconfig/node12/tsconfig.json",
    "compilerOptions": {
        "esModuleInterop": false,
        "allowSyntheticDefaultImports": true,
        "incremental": true,
        "moduleResolution": "node",
        "listEmittedFiles": true,
        "locale": "en-us",
        "outDir": "dist",
        "baseUrl": ".",
        "paths": {
            "$/*": ["src/*"],
        },
        "sourceMap": true,
    },
}
code fixed bug

All 5 comments

P.S. excluding 'module-alias' from dependencies does not solve the problem:

C:\test>npm start

> [email protected] start C:\test
> npx tsc && node dist/app.js

internal/modules/cjs/loader.js:969
  throw err;
  ^

Error: Cannot find module '@pnp/sp/files'
Require stack:
- C:\test\node_modules\@pnp\nodejs-commonjs\sp-extensions\stream.js
- C:\test\node_modules\@pnp\nodejs-commonjs\sp-extensions\index.js
- C:\test\node_modules\@pnp\nodejs-commonjs\index.js
- C:\test\dist\api\news\spConnector.js
- C:\test\dist\app.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)
    at Function.Module._load (internal/modules/cjs/loader.js:842:27)
    at Module.require (internal/modules/cjs/loader.js:1026:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (C:\test\node_modules\@pnp\nodejs-commonjs\sp-extensions\stream.js:6:15)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Module.require (internal/modules/cjs/loader.js:1026:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\test\\node_modules\\@pnp\\nodejs-commonjs\\sp-extensions\\stream.js',
    'C:\\test\\node_modules\\@pnp\\nodejs-commonjs\\sp-extensions\\index.js',
    'C:\\test\\node_modules\\@pnp\\nodejs-commonjs\\index.js',
    'C:\\test\\dist\\api\\news\\spConnector.js',
    'C:\\test\\dist\\app.js'
  ]
}

patching file node_modules/@pnp/nodejs-commonjs/sp-extensions/stream.js, line 6 fixes this problem:
from

var files_1 = require("@pnp/sp/files");

to

var files_1 = require("../../sp-commonjs/files");

I have the same error. The patch from @yarick123 ist working for me.

Thanks, will have a look.

This is fixed in 2.0.10 which is being published currently. Issue was actually with a regex we used in the build pipeline to update the import paths to ensure they used the -commonjs suffix. That is resolved and you should no longer have issues. Thanks again for reporting it.

Was this page helpful?
0 / 5 - 0 ratings