-
Notifications
You must be signed in to change notification settings - Fork 304
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
Comments
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}`);
} |
Thank you Andrew for the quick response. Possibly my message wasn't well worded (English is not my native language) I try to clarify
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 |
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(); (node:9244) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token < in JSON at position 1 |
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. |
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. |
Linked the issues. Closing this one. |
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. |
Category
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
The text was updated successfully, but these errors were encountered: