This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Provide a useful error message when Node stdlib module is imported (#…

…1001)

* [xdl] Provide a useful error message when Node stdlib module is imported

* Clean up error

* Add docs section in (faq) to explain Node.js deps and update url in error message

* Add a note about installing some node stdlib apis as packages from npm

fbshipit-source-id: 3905171
  • Loading branch information...
brentvatne authored and expbot committed Sep 13, 2017
1 parent 99660a1 commit 1d62eca293dfb867fc0afc920c3dad94b7209987
Showing with 89 additions and 0 deletions.
  1. +89 −0 src/Project.js
View
@@ -825,6 +825,89 @@ async function _restartWatchmanAsync(projectRoot: string) {
);
}
function _parseModuleResolutionError(
projectRoot: string,
errorMessage: string
) {
let parts = errorMessage.split(' from ');
let moduleName = parts[0]
.replace(/.*?Unable to resolve module /, '')
.replace(/`/g, '')
.trim();
let path = parts[1].replace(/`: Module .*/, '').replace(/`/, '').trim();
let relativePath = path.replace(projectRoot, '').trim();
return {
moduleName,
relativePath,
path,
};
}
const NODE_STDLIB_MODULES = [
'assert',
'async_hooks',
'buffer',
'child_process',
'cluster',
'crypto',
'dgram',
'dns',
'domain',
'events',
'fs',
'http',
'https',
'net',
'os',
'path',
'punycode',
'querystring',
'readline',
'repl',
'stream',
'string_decoder',
'tls',
'tty',
'url',
'util',
'v8',
'vm',
'zlib',
];
function _logModuleResolutionError(projectRoot: string, errorMessage: string) {
let { moduleName, relativePath, path } = _parseModuleResolutionError(
projectRoot,
errorMessage
);
const DOCS_PAGE_URL =
'https://docs.expo.io/versions/latest/introduction/faq.html#can-i-use-nodejs-packages-with-expo';
if (NODE_STDLIB_MODULES.includes(moduleName)) {
if (path.includes('node_modules')) {
ProjectUtils.logError(
projectRoot,
'packager',
`The package at ".${relativePath}" attempted to import the Node standard library module "${moduleName}". It failed because React Native does not include the Node standard library. Read more at ${DOCS_PAGE_URL}`
);
} else {
ProjectUtils.logError(
projectRoot,
'packager',
`You attempted attempted to import the Node standard library module "${moduleName}" from ".${relativePath}". It failed because React Native does not include the Node standard library. Read more at ${DOCS_PAGE_URL}`
);
}
} else {
ProjectUtils.logError(
projectRoot,
'packager',
`Unable to resolve ${moduleName}" from "./${relativePath}"`
);
}
}
function _logPackagerOutput(projectRoot: string, level: string, data: Object) {
let output = data.toString();
if (output.includes('─────')) {
@@ -837,6 +920,11 @@ function _logPackagerOutput(projectRoot: string, level: string, data: Object) {
if (!output) {
return;
}
if (output.includes('Unable to resolve module')) {
_logModuleResolutionError(projectRoot, output);
}
// Temporarily hide warnings about duplicate providesModule declarations
// under react-native
if (_isIgnorableDuplicateModuleWarning(projectRoot, level, output)) {
@@ -922,6 +1010,7 @@ function _handleDeviceLogs(
let groupDepth = log.groupDepth;
let shouldHide = log.shouldHide;
let includesStack = log.includesStack;
ProjectUtils.logWithLevel(
projectRoot,
level,

0 comments on commit 1d62eca

Please sign in to comment.