Module not found: Can't resolve 'aws-sdk' when import nodejieba to react - node.js

I run into this error when my script import nodejieba.
It named 「Module not found: Can't resolve 'aws-sdk' in 'C:\Users\user\Documents\cchatbigdata\node_modules#mapbox\node-pre-gyp\lib\util'」
At first, the react run well and my script work fine.
then I npm install nodejieba --save-dev
and add import nodejieba from 'nodejieba' in my TestedData.js.
even if I don't use nodejieba's function, my react ran into this error.
I start to search related question about Can't resolve 'aws-sdk'.
most of them encountering this error when using webpack, but I don't use webpack, I just import nodejieba.
I try to understand node-pre-gyp\lib\util under the node_modules.
but it created by React(I using npx create-react-app), so I think if changing the file react may shutdown.
I think something wrong between react and nodejieba.
My browser show those errors, I found they all related to node-pre-gyp\lib\util.
./node_modules/#mapbox/node-pre-gyp/lib/util/s3_setup.js
Module not found: Can't resolve 'aws-sdk' in 'C:\Users\kevin\Documents\cchatbigdata\node_modules\#mapbox\node-pre-gyp\lib\util'
console.<computed> # index.js:1
handleErrors # webpackHotDevClient.js:174
push../node_modules/react-dev-utils/webpackHotDevClient.js.connection.onmessage # webpackHotDevClient.js:213
index.js:1
./node_modules/#mapbox/node-pre-gyp/lib/util/s3_setup.js
Module not found: Can't resolve 'mock-aws-s3' in 'C:\Users\kevin\Documents\cchatbigdata\node_modules\#mapbox\node-pre-gyp\lib\util'
console.<computed> # index.js:1
handleErrors # webpackHotDevClient.js:174
push../node_modules/react-dev-utils/webpackHotDevClient.js.connection.onmessage # webpackHotDevClient.js:213
index.js:1
./node_modules/#mapbox/node-pre-gyp/lib/util/s3_setup.js
Module not found: Can't resolve 'nock' in 'C:\Users\kevin\Documents\cchatbigdata\node_modules\#mapbox\node-pre-gyp\lib\util'
console.<computed> # index.js:1
handleErrors # webpackHotDevClient.js:174
push../node_modules/react-dev-utils/webpackHotDevClient.js.connection.onmessage # webpackHotDevClient.js:213
Maybe node-pre-gyp under the node_modules must to be fixed.
Here is my TestData.js
import {useState, useEffect} from 'react';
import nodejieba from 'nodejieba'; //this line make me run into this error!
...
Thank for your reading and help!

Run npm install util
I don't believe this error has anything to do with nodejieba. You used npx create-react-app and it must be using Webpack 5 now, which does not include polyfills for node.js anymore. An alternate solution is to use https://www.npmjs.com/package/node-polyfill-webpack-plugin but I liked installing just the single util package for my use case.

Related

"Cannot use import statement outside a module" without "type: module"

I've NodeJS app whit many file like this:
import '#babel/polyfill'
import app from './app'
./app is a js file: app.js and #babel/polyfill is a npm package
When I try to start my app with npm run dev i got this error:
SyntaxError: Cannot use import statement outside a module
I've seen that you can use "type": "module" on package.json to solve the problem
But this causes another problem:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module './app' imported from /boot.js
I have many file that import modules and other files like that so i can't change them all
How do I keep the two type fo import?
If you only import babel like that you can setup babel for global import
This is .babelrc file on root folder:
{
"presets": [
"#babel/preset-env"
]
}

Config package in Vue 3 nodejs project

I'm building a project using Vue 3 and nodejs. I need to include some configurations that should change depending on where I run the code. I found the config package that seems very promising, but I started to get a huge number of errors during the start of the project.
The first issue regards some missing packages.
Module not found: Error: Can't resolve 'util' ...
Module not found: Error: Can't resolve 'path' ...
Module not found: Error: Can't resolve 'fs' ...
Module not found: Error: Can't resolve 'os' ...
I solved them by adding this code in the config file:
resolve: {
fallback: {
fs: false,
os: false,
util: false,
path: false
}
}
Now the project starts, but I get these warnings:
warning in ./node_modules/config/lib/config.js
Critical dependency: the request of a dependency is an expression
warning in ./node_modules/config/parser.js
require.extensions is not supported by webpack. Use a loader instead.
When I surf to localhost:8080 I get:
Uncaught ReferenceError: process is not defined
Instead of using resolve.fallback, I also tried to install the dependencies:
npm install path fs os util
but I continue to get the error for fs:
Module not found: Error: Can't resolve 'fs' ...
Is there a way to use the config package easily along with Vue 3?

TypeScript #types modules fail to resolve each other

When I use npm to install TypeScript definitions like this
npm install --save #types/express
I cannot use the installed modules as they fail to resolve each other. For example, #types/express requires #types/express-static-server-core, but as #types/express/index.d.ts contains a relative path to express-static-server-core, the module cannot be resolved:
node_modules/#types/express/index.d.ts(16,30): error TS2307: Cannot find module 'serve-static'.
node_modules/#types/express/index.d.ts(17,23): error TS2307: Cannot find module 'express-serve-static-core'.
node_modules/#types/serve-static/index.d.ts(15,26): error TS2307: Cannot find module 'express-serve-static-core'.
node_modules/#types/serve-static/index.d.ts(16,20): error TS2307: Cannot find module 'mime'.
How can I solve this issue? What is the best way to install TypeScript definitions?
As far as I know, typings is deprecated, so I tried to just install the type definitions from #types and then use
tsc --target ES5 --module commonjs index.ts
but it does not work yet. What am I doing wrong?
On the chance this is related, in one project, I was using generic types with express:
import { ParamsDictionary, Request, ... } from 'express-serve-static-core';
// ...
app.get<ParamsDictionary, UserDetails>(...);
That worked. When I tried to do the same thing in another project that wasn't using generic types:
import { Request, Response } from 'express-serve-static-core';
I was getting 'Unable to resolve path to module 'express-serve-static-core'.
When I changed my import to:
import express, { Request, Response } from 'express';
The problem went away.

Install imdb-api and use it in Angular2 Webpack

I am trying to use the imdb-api repo (https://github.com/worr/node-imdb-api) in my angular2 project, but can't seem to get it working.
I'm working on the newest angular2 webpack (Angular2 Webpack Starter). I usually know how to get libs working, but this one is just not working :(
Have cloned the Angular2 Webpack starter, and made the Npm install and npm start.. everything works great.
Then I have installed version 2.0.0 of imdb-api like this:
npm install --save imdb-api
It installed with no problems.
In a component, (lets say the about component) , I have imported the module like:
import * as imdb from 'imdb-api';
In the constructor, I am trying to console.log(imdb) to see if it's available.
constructor(public route: ActivatedRoute) {
// var imdb = require('imdb-api');
console.log(imdb);
}
As you see, I also tried using the var imdb = require ...
The problem is all this gives me errors I just can't seem to solve. I am getting the following errors:
ERROR in ./~/request/lib/har.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\Mikkel\Documents\Programmering\angular2-webpack-starter\node_modules\request\lib'
ERROR in ./~/forever-agent/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\Mikkel\Documents\Programmering\angular2-webpack-starter\node_modules\forever-agent'
ERROR in ./~/forever-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\Mikkel\Documents\Programmering\angular2-webpack-starter\node_modules\forever-agent'
ERROR in ./~/tough-cookie/lib/cookie.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\Mikkel\Documents\Programmering\angular2-webpack-starter\node_modules\tough-cookie\lib'
ERROR in ./~/tunnel-agent/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\Mikkel\Documents\Programmering\angular2-webpack-starter\node_modules\tunnel-agent'
ERROR in ./~/tunnel-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\Mikkel\Documents\Programmering\angular2-webpack-starter\node_modules\tunnel-agent'
I have found a post about the request problem. The imdb-api is using the request-promise, thats why I'm getting the error.
The post you find here: https://github.com/request/request/issues/1529
The suggested to add the following:
console: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty'
to the node object in the webpack.config.js file. Have added it to the webpack dev config, but now I don't see any errors at all, but the application wont load.

Why am I getting a "Cannot find module" err when compiling Typescript?

I am just getting started using typescript on the server and I'm pretty much stuck trying to import 3rd party npm modules. Here is what I have declared:
import mongodb = require('mongodb');
import assert = require('assert');
import Q = require('q');
... and I am getting the following errors when compiling:
src/Databse.ts(1,26): error TS2307: Cannot find module 'mongodb'.
src/Databse.ts(2,25): error TS2307: Cannot find module 'assert'.
src/Databse.ts(3,21): error TS2307: Cannot find module 'q'.
What is the correct way to import 3rd party modules?
It doesn't feel the correct way to import 3rd party's library. You can simply use only import
import "library";
OR
import {module} from "library";
Note:- when you are using above syntax then you need to make your that
the module you are importing exist otherwise you will get the error
that it can't find the module you are trying to import.
If you want to use webpack then
require("library");
Or like this
var module = require("library");
Have you installed the typing declarations ?
typings install mongodb --save
typings install dt~assert --save
typings install dt~q --save
Then you will need to reference the typings.
Either using the triple slash-directive
/// <reference path="..." />
More about it here Triple-Slash Directives
Or add the typings index.d.ts file in files array in tsconfig.json
"files": [
"./typings/index.d.ts",
]

Resources