2

What's the correct way to import a module from node_modules into a react app?

When I do: import MyModule from '../node_modules/my_module'

I get: Module not found: You attempted to import ../node_modules/my_module which falls outside of the project src/ directory. Relative imports outside of src/ are not supported. You can either move it inside src/, or add a symlink to it from project's node_modules/

Don't tell me I'm supposed to copy node_modules into src/ everytime?

Note: I'm on windows which has no symlinks.

1
  • What's the problem with import MyModule from 'my_module' ? Commented Nov 23, 2017 at 6:53

2 Answers 2

8

If you want to import some modules from node_modules, you just need to say, for example, import the whole exported module:

import React from "react";

or import part of the exported module:

import { Router } from "react-router"

"react", and "react-router" are modules in your node_modules folder.

Sign up to request clarification or add additional context in comments.

1 Comment

I had tried that, apparently I needed to restart the dev server.
0

import syntax should use {export name} from './';

but if you use export default, you are possible to just use export name.

and also, The module installed from npm doesn't need path. Just use module name

e.g) import {React, Component} from 'react';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.