Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs app - node 10.15 complaining about JSON and global$1.fetch #472

Closed
3 tasks
dannyfoncke opened this issue Jan 22, 2019 · 7 comments
Closed
3 tasks

Comments

@dannyfoncke
Copy link

dannyfoncke commented Jan 22, 2019

Category

  • Enhancement
  • Bug
  • [x ] Question
  • Documentation gap/issue

Version

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

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

Following the setup and code in https://github.com/SharePoint-NodeJS/pnp-auth I have a working version on a windows server using nodejs v 8.11 with @pnp/js v1.2.7
This is my code
`import { bootstrap } from 'pnp-auth';
import { sp, Web } from '@pnp/sp';
import { AuthConfig } from 'node-sp-auth-config';

const authConfig = new AuthConfig({
configPath: './src/config/private.config.json',
encryptPassword: true,
saveConfigOnDisk: true
});
bootstrap(sp, authConfig);
export const init = () => {
let web = new Web("https://xxxxxxxxxx")
web.get()
.then(data => {
console.log(Your web title: ${data.Title});
})
.catch(function(error) {
console.log(error);
});
}`

Trying this same code on another server with node 10.15 I run into problems
I installed all packages anew (this is not a copy/paste of the working project)

1st error :
message:"Unexpected token < in JSON at position 1"
stack:"SyntaxError: Unexpected token < in JSON at position 1\n at JSON.parse ()\n at c:\Services\nodetest\node_modules@pnp\odata\dist\odata.es5.umd.js:129:93\n at process._tickCallback (internal/process/next_tick.js:68:7)"

adding the following snippet (which I thought N/A) I get past that error but I get another
sp.setup({ sp: { headers: { "Accept": "application/json; odata=verbose" } } })
2nd error :
TypeError: global$1.fetch is not a function
sp.ts:30
message:"global$1.fetch is not a function"
stack:"TypeError: global$1.fetch is not a function\n at FetchClient.fetch (c:\Services\nodetest\node_modules@pnp\common\dist\common.es5.umd.js:310:29)\n .....

Clearly I'm doing something wrong - question is what

All help greatly appreciated

@koltyakov
Copy link
Member

Hi @dannyfoncke,

Combining pnp-auth's bootstrap method and sp.setup might be not a good idea. It's better using [object].configure for custom headers or fetchClientFactory:

import { bootstrap } from 'pnp-auth';
import { sp, Web } from '@pnp/sp';
import { AuthConfig } from 'node-sp-auth-config';

const authConfig = new AuthConfig({
  configPath: './config/private.json',
  encryptPassword: true,
  saveConfigOnDisk: true
});

bootstrap(sp, authConfig);

export const init = async () => {
  const { siteUrl } = await authConfig.getContext();
  const web = new Web(siteUrl).configure({
    headers: {
      'Accept': 'application/json; odata=verbose'
    }
  });
  const { Title } = await web.select('Title').get();
  console.log(`Your web title: ${Title}`);
}

or

import NodeFetchClient from 'pnp-auth/lib/NodeFetchClient';
import { sp, Web } from '@pnp/sp';
import { AuthConfig } from 'node-sp-auth-config';

const authConfig = new AuthConfig({
  configPath: './config/private.json',
  encryptPassword: true,
  saveConfigOnDisk: true
});

const configure = async (): Promise<{ siteUrl: string; }> => {
  const { siteUrl, authOptions } = await authConfig.getContext();
  const fetchClient = new NodeFetchClient(authOptions, siteUrl);
  sp.setup({
    sp: {
      headers: {
        'Accept': 'application/json; odata=verbose'
      },
      baseUrl: siteUrl,
      fetchClientFactory: () => fetchClient
    }
  });
  return { siteUrl };
}

export const init = async () => {
  const { siteUrl } = await configure();
  const web = new Web(siteUrl);
  const { Title } = await web.select('Title').get();
  console.log(`Your web title: ${Title}`);
}

@dannyfoncke
Copy link
Author

dannyfoncke commented Jan 22, 2019

Thank you Andrew for the quick response.

Possibly my message wasn't well worded (English is not my native language)

I try to clarify

  • The code (see at the top) works (using Node 8.11, pnp packages version 1.2.7)
  • When using the exact same code with node 10.15, pnp package versions 1.2.8 it doesn't work
  • I added the sp.setup code because I knew the JSON error can be fixed that way (however I thought this was when connecting to SP2013) - but it shouldn't be needed ?! (and I'm connecting to SP2016)
  • When I add the sp.setup code I get the second error - which for me looks like the code thinks it is running in a browser ?

So bottom line of my thinking is that I don't need to change the code, but that there is somewhere a misconfiguration and/or misuse of pnp packages.....

I can't give any more details because this is really simple code which is (except for the use of node-sp-auth-config) identical to the code found in https://github.com/SharePoint-NodeJS/pnp-auth

I will check out your code though - keep you posted if that works

thanks again for your interest and replies

@dannyfoncke
Copy link
Author

dannyfoncke commented Jan 23, 2019

As promised I tried the code provided by Andrew : solution 1 and solution 2 give the same result

on execution of line : const { Title } = await web.select('Title').get();
this is the error:

(node:9244) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token < in JSON at position 1
warning.js:18
at JSON.parse ()
at c:\Services\nodetest\node_modules@pnp\odata\dist\odata.es5.umd.js:129:93
at process._tickCallback (internal/process/next_tick.js:68:7)

@koltyakov
Copy link
Member

koltyakov commented Jan 23, 2019

That’s strange. Both samples work for me in Node 8 and 10. And we use similar setup on a pretty large number of different environments in production.
Are you sure you didn’t apply any changes while testing? Should be something else which is missing.
Also, I consider this issue to be more relevant to pnp-auth library rather that PnPjs. How about moving the discussion to its repository keeping two issues linked?

@dannyfoncke
Copy link
Author

I have learned that the server has been installed with IIS and IISNode

In my code there is nothing linking the project to IISNode, nor is there any config in IISNode linking it to my project. Still, might there be a influence ?

As far as moving this discussion, I have no problem with that. I have no idea how to do that however.

@koltyakov
Copy link
Member

Linked the issues. Closing this one.

@github-actions
Copy link

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants